@Override public synchronized ModuleClassLoader getModuleClassLoader() { if (moduleClassLoader == null && getBundle().isResolved()) { ModuleIdentifier identifier = getModuleIdentifier(); try { ModuleManager moduleManager = getFrameworkState().getModuleManager(); Module module = moduleManager.loadModule(identifier); moduleClassLoader = module.getClassLoader(); } catch (ModuleLoadException ex) { throw FrameworkMessages.MESSAGES.illegalStateCannotLoadModule(ex, identifier); } } return moduleClassLoader; }
@Test public void testServiceLoaderFails() throws Exception { // The {@link ModularURLStreamHandlerFactory} follows a pattern similar to this. SystemBundleState systemBundle = getBundleManager().getSystemBundle(); ModuleManager plugin = getFrameworkState().getModuleManager(); Module frameworkModule = plugin.loadModule(systemBundle.getModuleIdentifier()); assertNotNull("Framework module not null", frameworkModule); // Test resource access ModuleClassLoader classLoader = frameworkModule.getClassLoader(); URL resource = classLoader.getResource("META-INF/services/" + URLStreamHandlerFactory.class.getName()); assertNull("Resource URL null", resource); // Test ServiceLoader access Iterator<URLStreamHandlerFactory> iterator = frameworkModule.loadService(URLStreamHandlerFactory.class).iterator(); assertFalse("No more URLStreamHandlerFactory", iterator.hasNext()); }