SystemManagementService extends BaseManagementService { private SystemManagementService(InternalCacheForClientAccess cache, Function<ManagementResourceRepo, NotificationHub> notificationHubFactory, Function<SystemManagementService, LocalManager> localManagerFactory, FederatingManagerFactory federatingManagerFactory, BiFunction<DistributionConfig, InternalCacheForClientAccess, ManagementAgent> managementAgentFactory) { this.cache = cache; system = cache.getInternalDistributedSystem(); this.localManagerFactory = localManagerFactory; if (!system.isConnected()) { throw new DistributedSystemDisconnectedException( "This connection to a distributed system has been disconnected."); } statisticsFactory = system.getStatisticsManager(); statisticsClock = cache.getStatisticsClock(); jmxAdapter = new MBeanJMXAdapter(system.getDistributedMember()); repo = new ManagementResourceRepo(); notificationHub = notificationHubFactory.apply(repo); if (system.getConfig().getJmxManager()) { agent = managementAgentFactory.apply(system.getConfig(), cache); } else { agent = null; } FunctionService.registerFunction(new ManagementFunction(notificationHub)); proxyListeners = new CopyOnWriteArrayList<>(); this.federatingManagerFactory = federatingManagerFactory; } private  SystemManagementService(InternalCacheForClientAccess cache,
      Function<ManagementResourceRepo, NotificationHub> notificationHubFactory,
      Function<SystemManagementService, LocalManager> localManagerFactory,
      FederatingManagerFactory federatingManagerFactory,
      BiFunction<DistributionConfig, InternalCacheForClientAccess, ManagementAgent> managementAgentFactory); @Override void close(); @Override void federate(ObjectName objectName, Class<T> interfaceClass,
      boolean notificationEmitter); @Override CacheServerMXBean getLocalCacheServerMXBean(int serverPort); @Override long getLastUpdateTime(ObjectName objectName); @Override DiskStoreMXBean getLocalDiskStoreMBean(String diskStoreName); @Override LockServiceMXBean getLocalLockServiceMBean(String lockServiceName); @Override RegionMXBean getLocalRegionMBean(String regionPath); @Override MemberMXBean getMemberMXBean(); @Override Set<ObjectName> queryMBeanNames(DistributedMember member); @Override Set<ObjectName> getAsyncEventQueueMBeanNames(DistributedMember member); @Override ObjectName registerMBean(Object object, ObjectName objectName); @Override void unregisterMBean(ObjectName objectName); @Override boolean isManager(); @Override void startManager(); @Override void stopManager(); @Override boolean isClosed(); @Override DistributedLockServiceMXBean getDistributedLockServiceMXBean(String lockServiceName); @Override DistributedRegionMXBean getDistributedRegionMXBean(String regionName); @Override DistributedSystemMXBean getDistributedSystemMXBean(); @Override ManagerMXBean getManagerMXBean(); @Override ObjectName getCacheServerMBeanName(int serverPort, DistributedMember member); @Override ObjectName getDiskStoreMBeanName(DistributedMember member, String diskName); @Override ObjectName getDistributedLockServiceMBeanName(String lockService); @Override ObjectName getDistributedRegionMBeanName(String regionPath); @Override ObjectName getDistributedSystemMBeanName(); @Override ObjectName getGatewayReceiverMBeanName(DistributedMember member); @Override ObjectName getGatewaySenderMBeanName(DistributedMember member, String gatewaySenderId); @Override ObjectName getAsyncEventQueueMBeanName(DistributedMember member, String queueId); @Override ObjectName getLockServiceMBeanName(DistributedMember member, String lockServiceName); @Override ObjectName getManagerMBeanName(); @Override ObjectName getMemberMBeanName(DistributedMember member); @Override ObjectName getRegionMBeanName(DistributedMember member, String regionPath); @Override GatewayReceiverMXBean getLocalGatewayReceiverMXBean(); @Override GatewaySenderMXBean getLocalGatewaySenderMXBean(String senderId); @Override AsyncEventQueueMXBean getLocalAsyncEventQueueMXBean(String queueId); @Override ObjectName getLocatorMBeanName(DistributedMember member); @Override LocatorMXBean getLocalLocatorMXBean(); @Override T getMBeanInstance(ObjectName objectName, Class<T> interfaceClass); @Override void addMembershipListener(MembershipListener listener); @Override void removeMembershipListener(MembershipListener listener); LocalManager getLocalManager(); FederatingManager getFederatingManager(); MBeanJMXAdapter getJMXAdapter(); ManagementAgent getManagementAgent(); T getMBeanProxy(ObjectName objectName, Class<T> interfaceClass); ObjectName registerInternalMBean(Object object, ObjectName objectName); boolean isManagerCreated(); boolean createManager(); void addProxyListener(ProxyListener listener); void removeProxyListener(ProxyListener listener); void afterCreateProxy(ObjectName objectName, Class interfaceClass, Object proxyObject,
      FederationComponent newVal); void afterRemoveProxy(ObjectName objectName, Class interfaceClass, Object proxyObject,
      FederationComponent oldVal); void handleNotification(Notification notification); @VisibleForTesting NotificationHub getNotificationHub();  }
@Test public void startManager_throws_ifIfNotWillingToBeJmxManager() { when(config.getJmxManager()).thenReturn(false); BaseManagementService service = systemManagementService(); assertThatThrownBy(service::startManager) .isInstanceOf(ManagementException.class); }
@Test public void startManager_throws_ifSystemIsNotConnected() { when(system.isConnected()).thenReturn(true); BaseManagementService service = systemManagementService(); when(system.isConnected()).thenReturn(false); assertThatThrownBy(service::startManager) .isInstanceOf(ManagementException.class); }
@Test public void startManager_reportsManagerStopped_ifRuntimeExceptionAfterStarting() { BaseManagementService service = systemManagementService(); doThrow(new RuntimeException("thrown for testing")).when(managementAgent).startAgent(); assertThatThrownBy(service::startManager) .isInstanceOf(RuntimeException.class); verify(system).handleResourceEvent(eq(MANAGER_STOP), any()); }