@Test public void givingDifferentKeysButSameClassYieldsTwoSeparateObjects() { Configuration.getInstance().setProperty( CoreConfig.DISCOVERY_MODULES, "com.rackspacecloud.blueflood.io.DummyDiscoveryIO"); Object loadedModule1 = ModuleLoader.getInstance(DiscoveryIO.class, CoreConfig.DISCOVERY_MODULES); Assert.assertNotNull(loadedModule1); Assert.assertEquals(DummyDiscoveryIO.class, loadedModule1.getClass()); Assert.assertEquals(DummyDiscoveryIO.class, loadedModule1.getClass()); }
public static Object getInstance(Class c, CoreConfig moduleName) { Object moduleInstance = loadedModules.get(moduleName.name().toString()); if (moduleInstance != null) return moduleInstance; List<String> modules = Configuration.getInstance().getListProperty(moduleName); if (modules.isEmpty()) return null; if (modules.size() != 1) { throw new RuntimeException("Cannot load service with more than one "+moduleName+" module"); } String module = modules.get(0); log.info("Loading the module " + module); try { ClassLoader loader = c.getClassLoader(); Class genericClass = loader.loadClass(module); moduleInstance = genericClass.newInstance(); loadedModules.put(moduleName.name().toString(), moduleInstance); log.info("Registering the module " + module); } catch (InstantiationException e) { log.error(String.format("Unable to create instance of %s class for %s", c.getName(), module), e); } catch (IllegalAccessException e) { log.error("Error starting module: " + module, e); } catch (ClassNotFoundException e) { log.error("Unable to locate module: " + module, e); } catch (RuntimeException e) { log.error("Error starting module: " + module, e); } catch (Throwable e) { log.error("Error starting module: " + module, e); } return moduleInstance; }
ModuleLoader { public static Object getInstance(Class c, CoreConfig moduleName) { Object moduleInstance = loadedModules.get(moduleName.name().toString()); if (moduleInstance != null) return moduleInstance; List<String> modules = Configuration.getInstance().getListProperty(moduleName); if (modules.isEmpty()) return null; if (modules.size() != 1) { throw new RuntimeException("Cannot load service with more than one "+moduleName+" module"); } String module = modules.get(0); log.info("Loading the module " + module); try { ClassLoader loader = c.getClassLoader(); Class genericClass = loader.loadClass(module); moduleInstance = genericClass.newInstance(); loadedModules.put(moduleName.name().toString(), moduleInstance); log.info("Registering the module " + module); } catch (InstantiationException e) { log.error(String.format("Unable to create instance of %s class for %s", c.getName(), module), e); } catch (IllegalAccessException e) { log.error("Error starting module: " + module, e); } catch (ClassNotFoundException e) { log.error("Unable to locate module: " + module, e); } catch (RuntimeException e) { log.error("Error starting module: " + module, e); } catch (Throwable e) { log.error("Error starting module: " + module, e); } return moduleInstance; } }
ModuleLoader { public static Object getInstance(Class c, CoreConfig moduleName) { Object moduleInstance = loadedModules.get(moduleName.name().toString()); if (moduleInstance != null) return moduleInstance; List<String> modules = Configuration.getInstance().getListProperty(moduleName); if (modules.isEmpty()) return null; if (modules.size() != 1) { throw new RuntimeException("Cannot load service with more than one "+moduleName+" module"); } String module = modules.get(0); log.info("Loading the module " + module); try { ClassLoader loader = c.getClassLoader(); Class genericClass = loader.loadClass(module); moduleInstance = genericClass.newInstance(); loadedModules.put(moduleName.name().toString(), moduleInstance); log.info("Registering the module " + module); } catch (InstantiationException e) { log.error(String.format("Unable to create instance of %s class for %s", c.getName(), module), e); } catch (IllegalAccessException e) { log.error("Error starting module: " + module, e); } catch (ClassNotFoundException e) { log.error("Unable to locate module: " + module, e); } catch (RuntimeException e) { log.error("Error starting module: " + module, e); } catch (Throwable e) { log.error("Error starting module: " + module, e); } return moduleInstance; }  }
ModuleLoader { public static Object getInstance(Class c, CoreConfig moduleName) { Object moduleInstance = loadedModules.get(moduleName.name().toString()); if (moduleInstance != null) return moduleInstance; List<String> modules = Configuration.getInstance().getListProperty(moduleName); if (modules.isEmpty()) return null; if (modules.size() != 1) { throw new RuntimeException("Cannot load service with more than one "+moduleName+" module"); } String module = modules.get(0); log.info("Loading the module " + module); try { ClassLoader loader = c.getClassLoader(); Class genericClass = loader.loadClass(module); moduleInstance = genericClass.newInstance(); loadedModules.put(moduleName.name().toString(), moduleInstance); log.info("Registering the module " + module); } catch (InstantiationException e) { log.error(String.format("Unable to create instance of %s class for %s", c.getName(), module), e); } catch (IllegalAccessException e) { log.error("Error starting module: " + module, e); } catch (ClassNotFoundException e) { log.error("Unable to locate module: " + module, e); } catch (RuntimeException e) { log.error("Error starting module: " + module, e); } catch (Throwable e) { log.error("Error starting module: " + module, e); } return moduleInstance; }  static Object getInstance(Class c, CoreConfig moduleName); @VisibleForTesting static void clearCache(); }
ModuleLoader { public static Object getInstance(Class c, CoreConfig moduleName) { Object moduleInstance = loadedModules.get(moduleName.name().toString()); if (moduleInstance != null) return moduleInstance; List<String> modules = Configuration.getInstance().getListProperty(moduleName); if (modules.isEmpty()) return null; if (modules.size() != 1) { throw new RuntimeException("Cannot load service with more than one "+moduleName+" module"); } String module = modules.get(0); log.info("Loading the module " + module); try { ClassLoader loader = c.getClassLoader(); Class genericClass = loader.loadClass(module); moduleInstance = genericClass.newInstance(); loadedModules.put(moduleName.name().toString(), moduleInstance); log.info("Registering the module " + module); } catch (InstantiationException e) { log.error(String.format("Unable to create instance of %s class for %s", c.getName(), module), e); } catch (IllegalAccessException e) { log.error("Error starting module: " + module, e); } catch (ClassNotFoundException e) { log.error("Unable to locate module: " + module, e); } catch (RuntimeException e) { log.error("Error starting module: " + module, e); } catch (Throwable e) { log.error("Error starting module: " + module, e); } return moduleInstance; }  static Object getInstance(Class c, CoreConfig moduleName); @VisibleForTesting static void clearCache();  }