ManagementListener implements ResourceEventsListener { @Override public void handleEvent(ResourceEvent event, Object resource) { if (!shouldProceed(event)) { return; } try { if (event == ResourceEvent.CACHE_CREATE || event == ResourceEvent.CACHE_REMOVE) { readWriteLock.writeLock().lockInterruptibly(); } else if (event != ResourceEvent.SYSTEM_ALERT) { readWriteLock.readLock().lockInterruptibly(); } } catch (InterruptedException e) { cancelCriterion.checkCancelInProgress(e); throw new RuntimeException(e); } try { switch (event) { case CACHE_CREATE: InternalCache createdCache = (InternalCache) resource; adapter.handleCacheCreation(createdCache); break; case CACHE_REMOVE: InternalCache removedCache = (InternalCache) resource; adapter.handleCacheRemoval(); break; case REGION_CREATE: Region createdRegion = (Region) resource; adapter.handleRegionCreation(createdRegion); break; case REGION_REMOVE: Region removedRegion = (Region) resource; adapter.handleRegionRemoval(removedRegion); break; case DISKSTORE_CREATE: DiskStore createdDisk = (DiskStore) resource; adapter.handleDiskCreation(createdDisk); break; case DISKSTORE_REMOVE: DiskStore removedDisk = (DiskStore) resource; adapter.handleDiskRemoval(removedDisk); break; case GATEWAYRECEIVER_CREATE: GatewayReceiver createdRecv = (GatewayReceiver) resource; adapter.handleGatewayReceiverCreate(createdRecv); break; case GATEWAYRECEIVER_DESTROY: GatewayReceiver destroyedRecv = (GatewayReceiver) resource; adapter.handleGatewayReceiverDestroy(); break; case GATEWAYRECEIVER_START: GatewayReceiver startedRecv = (GatewayReceiver) resource; adapter.handleGatewayReceiverStart(startedRecv); break; case GATEWAYRECEIVER_STOP: GatewayReceiver stoppededRecv = (GatewayReceiver) resource; adapter.handleGatewayReceiverStop(); break; case GATEWAYSENDER_CREATE: GatewaySender sender = (GatewaySender) resource; adapter.handleGatewaySenderCreation(sender); break; case GATEWAYSENDER_START: GatewaySender startedSender = (GatewaySender) resource; adapter.handleGatewaySenderStart(startedSender); break; case GATEWAYSENDER_STOP: GatewaySender stoppedSender = (GatewaySender) resource; adapter.handleGatewaySenderStop(stoppedSender); break; case GATEWAYSENDER_PAUSE: GatewaySender pausedSender = (GatewaySender) resource; adapter.handleGatewaySenderPaused(pausedSender); break; case GATEWAYSENDER_RESUME: GatewaySender resumedSender = (GatewaySender) resource; adapter.handleGatewaySenderResumed(resumedSender); break; case GATEWAYSENDER_REMOVE: GatewaySender removedSender = (GatewaySender) resource; adapter.handleGatewaySenderRemoved(removedSender); break; case LOCKSERVICE_CREATE: DLockService createdLockService = (DLockService) resource; adapter.handleLockServiceCreation(createdLockService); break; case LOCKSERVICE_REMOVE: DLockService removedLockService = (DLockService) resource; adapter.handleLockServiceRemoval(removedLockService); break; case MANAGER_CREATE: adapter.handleManagerCreation(); break; case MANAGER_START: adapter.handleManagerStart(); break; case MANAGER_STOP: adapter.handleManagerStop(); break; case ASYNCEVENTQUEUE_CREATE: AsyncEventQueue queue = (AsyncEventQueue) resource; adapter.handleAsyncEventQueueCreation(queue); break; case ASYNCEVENTQUEUE_REMOVE: AsyncEventQueue removedQueue = (AsyncEventQueue) resource; adapter.handleAsyncEventQueueRemoval(removedQueue); break; case SYSTEM_ALERT: AlertDetails details = (AlertDetails) resource; adapter.handleSystemNotification(details); break; case CACHE_SERVER_START: CacheServer startedServer = (CacheServer) resource; adapter.handleCacheServerStart(startedServer); break; case CACHE_SERVER_STOP: CacheServer stoppedServer = (CacheServer) resource; adapter.handleCacheServerStop(stoppedServer); break; case LOCATOR_START: Locator loc = (Locator) resource; adapter.handleLocatorStart(loc); break; case CACHE_SERVICE_CREATE: CacheService service = (CacheService) resource; adapter.handleCacheServiceCreation(service); break; default: break; } } finally { if (event == ResourceEvent.CACHE_CREATE || event == ResourceEvent.CACHE_REMOVE) { readWriteLock.writeLock().unlock(); } else if (event != ResourceEvent.SYSTEM_ALERT) { readWriteLock.readLock().unlock(); } } } ManagementListener(InternalDistributedSystem system); @VisibleForTesting  ManagementListener(CancelCriterion cancelCriterion, InternalDistributedSystem system,
      ManagementAdapter adapter,
      ReadWriteLock readWriteLock); @Override void handleEvent(ResourceEvent event, Object resource);  }
@Test public void handleEventUsesWriteLockForCacheCreateEvent() throws InterruptedException { managementListener.handleEvent(CACHE_CREATE, null); writeLockInOrder.verify(writeLock).lockInterruptibly(); writeLockInOrder.verify(writeLock).unlock(); }
@Test public void handleEventUsesWriteLockForCacheRemoveEvent() throws InterruptedException { managementListener.handleEvent(CACHE_REMOVE, null); writeLockInOrder.verify(writeLock).lockInterruptibly(); writeLockInOrder.verify(writeLock).unlock(); }
@Test public void handleEventDoesNotUseLocksForSystemAlertEvent() { managementListener.handleEvent(SYSTEM_ALERT, null); verifyNoMoreInteractions(readWriteLock); verifyNoMoreInteractions(readLock); verifyNoMoreInteractions(writeLock); }
@Test public void handleEventUsesReadLockForOtherEvents() throws InterruptedException { for (ResourceEvent resourceEvent : ResourceEvent.values()) { if (resourceEvent != CACHE_CREATE && resourceEvent != CACHE_REMOVE && resourceEvent != SYSTEM_ALERT) { managementListener.handleEvent(resourceEvent, null); readLockInOrder.verify(readLock).lockInterruptibly(); readLockInOrder.verify(readLock).unlock(); } } }