@Override public BundleContext getBundleContext() { ModuleContext context = module.getModuleContext(); return context != null ? new BundleContextAdaptor(context) : null; }
@Override public ModuleContext getModuleContext() { Module sysmodule = getModule(0); return sysmodule != null ? sysmodule.getModuleContext() : null; }
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); }
ModuleContext getModuleContext() { return getModule().getModuleContext(); }
private ModuleContext getModuleContext(BundleContext bundleContext) { Bundle bundle = bundleContext.getBundle(); Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(bundle.getBundleId()); return module != null ? module.getModuleContext() : null; } }
protected ConfigurationAdmin getConfigurationAdmin(Module module) { ModuleContext context = module.getModuleContext(); return context.getService(context.getServiceReference(ConfigurationAdmin.class)); } }
@Test public void testLogService() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(getClass().getClassLoader()); LogService log = getLogService(module.getModuleContext()); String msg = "LogServiceTest LogService "; log.log(LogService.LOG_ERROR, msg + " ERROR"); log.log(LogService.LOG_WARNING, msg + " WARNING"); log.log(LogService.LOG_INFO, msg + " INFO"); log.log(LogService.LOG_DEBUG, msg + " DEBUG"); }
@Override public void contextInitialized(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); BundleContext bundleContext = (BundleContext) servletContext.getAttribute(BundleContext.class.getName()); final Module module = bundleContext.getBundle().adapt(Module.class); final ModuleContext context = module.getModuleContext(); tracker = new ServiceTracker<HttpService, HttpService>(context, HttpService.class, null) { @Override public HttpService addingService(ServiceReference<HttpService> sref) { HttpService service = super.addingService(sref); try { RuntimeLogger.LOGGER.info("Register system HttpService with alias: " + SYSTEM_ALIAS); service.registerServlet(SYSTEM_ALIAS, new HttpServiceServlet(module), null, null); } catch (Exception ex) { throw new IllegalStateException(ex); } return service; } @Override public void removedService(ServiceReference<HttpService> reference, HttpService service) { RuntimeLogger.LOGGER.info("Unregister system HttpService with alias: " + SYSTEM_ALIAS); service.unregister(SYSTEM_ALIAS); super.removedService(reference, service); } }; tracker.open(); }
@Test public void testServiceAvailability() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module modA = runtime.getModule(getClass().getClassLoader()); ModuleContext ctxA = modA.getModuleContext(); ServiceReference<ServiceA> srefA = ctxA.getServiceReference(ServiceA.class); Assert.assertNotNull("ServiceReference not null", srefA); ServiceA srvA = ctxA.getService(srefA); Assert.assertEquals("ServiceA#1:ServiceA1#1:Hello", srvA.doStuff("Hello")); assertDisableComponent(ctxA, srvA); }
@Test public void testBasicModule() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(getClass().getClassLoader()); ModuleContext ctxA = module.getModuleContext(); ServiceReference<ServiceD> srefD = ctxA.getServiceReference(ServiceD.class); Assert.assertNotNull("ServiceReference not null", srefD); ServiceD srvD = ctxA.getService(srefD); Assert.assertEquals("ServiceD#1:ServiceD1#1:null:Hello", srvD.doStuff("Hello")); ConfigurationAdmin configAdmin = getConfigurationAdmin(module); Configuration config = configAdmin.getConfiguration(ServiceD1.class.getName()); Assert.assertNotNull("Config not null", config); Assert.assertNull("Config is empty, but was: " + config.getProperties(), config.getProperties()); Dictionary<String, String> configProps = new Hashtable<String, String>(); configProps.put("foo", "bar"); config.update(configProps); ServiceD1 srvD1 = srvD.getServiceD1(); Assert.assertTrue(srvD1.awaitModified(4000, TimeUnit.MILLISECONDS)); Assert.assertEquals("ServiceD#1:ServiceD1#1:bar:Hello", srvD.doStuff("Hello")); }
@Test public void testServletThroughModuleContext() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(getClass().getClassLoader()); ModuleContext context = module.getModuleContext(); ServiceReference<HttpService> sref = context.getServiceReference(HttpService.class); HttpService httpService = context.getService(sref); String reqspec = "/gravia/servlet?test=module"; try { // Verify that the alias is not yet available assertNotAvailable(reqspec); // Register the test servlet and make a call String servletAlias = getRuntimeAwareAlias("/servlet"); httpService.registerServlet(servletAlias, new HttpServiceServlet(module), null, null); Assert.assertEquals("http-service:1.0.0", performCall(reqspec)); // Unregister the servlet alias httpService.unregister(servletAlias); assertNotAvailable(reqspec); // Verify that the alias is not available any more assertNotAvailable(reqspec); } finally { context.ungetService(sref); } }
@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 } }
@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 } }