ModuleManager implements ModuleDefineHolder { public void init( ApplicationConfiguration applicationConfiguration) throws ModuleNotFoundException, ProviderNotFoundException, ServiceNotProvidedException, CycleDependencyException, ModuleConfigException, ModuleStartException { String[] moduleNames = applicationConfiguration.moduleList(); ServiceLoader<ModuleDefine> moduleServiceLoader = ServiceLoader.load(ModuleDefine.class); ServiceLoader<ModuleProvider> moduleProviderLoader = ServiceLoader.load(ModuleProvider.class); LinkedList<String> moduleList = new LinkedList<>(Arrays.asList(moduleNames)); for (ModuleDefine module : moduleServiceLoader) { for (String moduleName : moduleNames) { if (moduleName.equals(module.name())) { module.prepare(this, applicationConfiguration.getModuleConfiguration(moduleName), moduleProviderLoader); loadedModules.put(moduleName, module); moduleList.remove(moduleName); } } } isInPrepareStage = false; if (moduleList.size() > 0) { throw new ModuleNotFoundException(moduleList.toString() + " missing."); } BootstrapFlow bootstrapFlow = new BootstrapFlow(loadedModules); bootstrapFlow.start(this); bootstrapFlow.notifyAfterCompleted(); }  void init(
        ApplicationConfiguration applicationConfiguration); @Override boolean has(String moduleName); @Override ModuleProviderHolder find(String moduleName);  }
@Test public void testInit() throws ServiceNotProvidedException, ModuleNotFoundException, ProviderNotFoundException, DuplicateProviderException, ModuleConfigException, ModuleStartException { ApplicationConfiguration configuration = new ApplicationConfiguration(); configuration.addModule("Test").addProviderConfiguration("TestModule-Provider", new Properties()); configuration.addModule("BaseA").addProviderConfiguration("P-A", new Properties()); configuration.addModule("BaseB").addProviderConfiguration("P-B", new Properties()); ModuleManager manager = new ModuleManager(); manager.init(configuration); BaseModuleA.ServiceABusiness1 serviceABusiness1 = manager.find("BaseA") .provider() .getService(BaseModuleA.ServiceABusiness1.class); Assert.assertTrue(serviceABusiness1 != null); }
@Test(expected = ModuleNotFoundException.class) public void testModuleMissing() throws ModuleConfigException, ModuleNotFoundException, ModuleStartException { ApplicationConfiguration configuration = new ApplicationConfiguration(); configuration.addModule("BaseA").addProviderConfiguration("P-A", new Properties()); configuration.addModule("BaseB").addProviderConfiguration("P-B2", new Properties()); ModuleManager manager = new ModuleManager(); manager.init(configuration); }
@Test(expected = CycleDependencyException.class) public void testCycleDependency() throws ModuleConfigException, ModuleNotFoundException, ModuleStartException { ApplicationConfiguration configuration = new ApplicationConfiguration(); configuration.addModule("BaseA").addProviderConfiguration("P-A2", new Properties()); configuration.addModule("BaseB").addProviderConfiguration("P-B3", new Properties()); ModuleManager manager = new ModuleManager(); manager.init(configuration); }