@Override public int getState() { switch (module.getState()) { case INSTALLED: return Bundle.RESOLVED; case STARTING: return Bundle.STARTING; case ACTIVE: return Bundle.ACTIVE; case STOPPING: return Bundle.STOPPING; case UNINSTALLED: return Bundle.UNINSTALLED; } return Bundle.UNINSTALLED; }
@Override public <A> A adapt(Class<A> type) { return module.adapt(type); }
@Override public String toString() { return "Bundle[" + module.getIdentity() + "]"; } }
/** * Uninstalls the webapp's module. */ @Override public void contextDestroyed(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); Module module = (Module) servletContext.getAttribute(Module.class.getName()); if (module != null && module.getState() != Module.State.UNINSTALLED) { module.uninstall(); } }
protected void doShutdown() { for (Module module : getModules()) { if (!module.getIdentity().equals(systemIdentity)) { module.uninstall(); } } }
@Test public void testModuleLifecycle() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module modA = runtime.getModule(getClass().getClassLoader()); Assert.assertEquals(Module.State.ACTIVE, modA.getState()); String symbolicName = modA.getHeaders().get("Bundle-SymbolicName"); Assert.assertEquals("simple-bundle", symbolicName); ModuleContext context = modA.getModuleContext(); Module sysmodule = runtime.getModule(0); symbolicName = sysmodule.getHeaders().get("Bundle-SymbolicName"); Assert.assertNotNull("System bundle symbolic name not null", symbolicName); ServiceReference<String> sref = context.getServiceReference(String.class); String service = context.getService(sref); Assert.assertEquals("Hello", service); modA.stop(); Assert.assertEquals(Module.State.INSTALLED, modA.getState()); modA.uninstall(); Assert.assertEquals(Module.State.UNINSTALLED, modA.getState()); try { modA.start(); Assert.fail("IllegalStateException expected"); } catch (IllegalStateException ex) { // expected } }
@Override public BundleContext getBundleContext() { ModuleContext context = module.getModuleContext(); return context != null ? new BundleContextAdaptor(context) : null; }
/** * Installs/starts the webapp as a module. */ @Override public void contextInitialized(ServletContextEvent event) { Runtime runtime = RuntimeLocator.getRequiredRuntime(); ServletContext servletContext = event.getServletContext(); Module module = runtime.getModule(servletContext.getClassLoader()); // Install the module if (module == null) { module = installWebappModule(servletContext); } // Start the module if (module != null) { servletContext.setAttribute(Module.class.getName(), module); try { module.start(); } catch (ModuleException ex) { throw new IllegalStateException(ex); } // HttpService integration BundleContext bundleContext = module.adapt(Bundle.class).getBundleContext(); servletContext.setAttribute(BundleContext.class.getName(), bundleContext); } }
return true; if (module.getState() == Module.State.UNINSTALLED) return false; ClassLoader moduleClassLoader = module.adapt(ClassLoader.class); if (moduleClassLoader == null) { LOGGER.info("No ClassLoader for: {}", module); ClassLoader ownerClassLoader = ownerModule.adapt(ClassLoader.class); if (ownerClassLoader == null) { LOGGER.trace("Registrant module [{}] has no class loader for: {}", ownerModule, className);
protected void startPluginModules(List<Module> pluginModules) { for (Module module : pluginModules) { try { module.start(); } catch (ModuleException ex) { LOGGER.error("Cannot start plugin: " + module, ex); } } }
@Override public long getBundleId() { return module.getModuleId(); }
@Override public Dictionary<String, String> getHeaders(String locale) { // Get the raw (unlocalized) manifest headers Dictionary<String, String> rawHeaders = module.getHeaders(); // If the specified locale is the empty string, this method will return the // raw (unlocalized) manifest headers including any leading "%" if ("".equals(locale)) return rawHeaders; Dictionary<String, String> result = new Hashtable<String, String>(); Enumeration<String> e = rawHeaders.keys(); while (e.hasMoreElements()) { String key = e.nextElement(); String value = rawHeaders.get(key); if (value.startsWith("%")) value = value.substring(1); result.put(key, value); } return new CaseInsensitiveDictionary<String>(result); }
@Test public void testModuleLifecycle() throws Exception { // Verify that the Runtime service is registered Runtime runtime = ServiceLocator.getRequiredService(Runtime.class); Module modA = runtime.getModule(getClass().getClassLoader()); Assert.assertEquals(Module.State.ACTIVE, modA.getState()); String symbolicName = modA.getHeaders().get("Bundle-SymbolicName"); Assert.assertEquals("simple-module", symbolicName); ModuleContext context = modA.getModuleContext(); Module sysmodule = runtime.getModule(0); symbolicName = sysmodule.getHeaders().get("Bundle-SymbolicName"); Assert.assertNotNull("System bundle symbolic name not null", symbolicName); ServiceReference<String> sref = context.getServiceReference(String.class); String service = context.getService(sref); Assert.assertEquals("Hello", service); modA.stop(); Assert.assertEquals(Module.State.INSTALLED, modA.getState()); modA.uninstall(); Assert.assertEquals(Module.State.UNINSTALLED, modA.getState()); try { modA.start(); Assert.fail("IllegalStateException expected"); } catch (IllegalStateException ex) { // expected } }
ServiceListenerRegistration(final Module module, final ServiceListener listener, final Filter filter) { assert module != null : "Null module"; assert listener != null : "Null listener"; assert filter != null : "Null filter"; this.module = module; this.listener = listener; this.filter = filter; this.moduleContext = module.getModuleContext(); this.info = new ListenerInfo(moduleContext, this); }
/** * Uninstalls the webapp's module. */ @Override public void contextDestroyed(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); Module module = (Module) servletContext.getAttribute(Module.class.getName()); if (module != null && module.getState() != Module.State.UNINSTALLED) { module.uninstall(); } }
/** * Installs/starts the webapp as a module. */ @Override public void contextInitialized(ServletContextEvent event) { Runtime runtime = RuntimeLocator.getRequiredRuntime(); ServletContext servletContext = event.getServletContext(); Module module = runtime.getModule(servletContext.getClassLoader()); // Install the module if (module == null) { module = installWebappModule(servletContext); } // Start the module if (module != null) { servletContext.setAttribute(Module.class.getName(), module); try { module.start(); } catch (ModuleException ex) { throw new IllegalStateException(ex); } // HttpService integration BundleContext bundleContext = module.adapt(Bundle.class).getBundleContext(); servletContext.setAttribute(BundleContext.class.getName(), bundleContext); } }
protected void uninstallModule(Module module) { modules.remove(module.getModuleId()); LOGGER.info("Uninstalled: {}", module); }
public String toString() { return "ModuleContext[" + module.getIdentity() + "]"; } }
@Override public String getModuleState() { return context.getModule().getState().toString(); } };
@Override public ModuleContext getModuleContext() { Module sysmodule = getModule(0); return sysmodule != null ? sysmodule.getModuleContext() : null; }