private Module mappedModule(Bundle bundle) { Module result = null; if (bundle != null) { Runtime runtime = RuntimeLocator.getRequiredRuntime(); result = runtime.getModule(bundle.getBundleId()); } return result; }
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; } }
@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 Bundle getBundle(long id) { Module module = getRuntime().getModule(id); return module != null ? new BundleAdaptor(module) : 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); } }
/** * 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); } }
@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 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 } }
@Test public void testServletThroughSystemContext() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(getClass().getClassLoader()); HttpService httpService = ServiceLocator.getRequiredService(HttpService.class); String reqspec = "/gravia/servlet?test=module"; // 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); }
for (ResourceHandle handle : handles) { ResourceIdentity identity = handle.getResource().getIdentity(); Assert.assertSame(handle.getModule(), runtime.getModule(identity)); Assert.assertEquals("ACTIVE " + identity, State.ACTIVE, handle.getModule().getState()); Module moduleB1 = runtime.getModule(identityB1); MBeanServer server = ServiceLocator.getRequiredService(MBeanServer.class); Assert.assertTrue("MBean registered", server.isRegistered(getObjectName(moduleB1)));
@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); } }
for (ResourceHandle handle : handles) { ResourceIdentity identity = handle.getResource().getIdentity(); Assert.assertSame(handle.getModule(), runtime.getModule(identity)); Assert.assertEquals("ACTIVE " + identity, State.ACTIVE, handle.getModule().getState()); Module module = runtime.getModule(identityG); MBeanServer server = ServiceLocator.getRequiredService(MBeanServer.class); Assert.assertTrue("MBean registered", server.isRegistered(getObjectName(module)));
/** * Provision a resource from an input stream. * * The client controlls the resource identity * The installed resource is self sufficient - no additional dependency mapping needed. */ @Test public void testProvisionStreamResource() throws Exception { Provisioner provisioner = ServiceLocator.getRequiredService(Provisioner.class); ResourceIdentity identityA = ResourceIdentity.fromString(RESOURCE_A); ResourceBuilder builderA = provisioner.getContentResourceBuilder(identityA, deployer.getDeployment(RESOURCE_A)); ResourceHandle handle = provisioner.installResource(builderA.getResource()); try { // Verify that the module got installed Runtime runtime = RuntimeLocator.getRequiredRuntime(); Module module = runtime.getModule(identityA); Assert.assertNotNull("Module not null", module); Assert.assertEquals("ACTIVE " + module, State.ACTIVE, module.getState()); // Verify that the module activator was called MBeanServer server = ServiceLocator.getRequiredService(MBeanServer.class); Assert.assertTrue("MBean registered", server.isRegistered(getObjectName(module))); Assert.assertEquals("ACTIVE", server.getAttribute(getObjectName(module), "ModuleState")); } finally { handle.uninstall(); } }
/** * Provision a resource from maven coordinates. * * The client controlls the resource identity * The installed resource is self sufficient - no additional dependency mapping needed. */ @Test public void testMavenResource() throws Exception { Provisioner provisioner = ServiceLocator.getRequiredService(Provisioner.class); Runtime runtime = RuntimeLocator.getRequiredRuntime(); // Tomcat does not support jar deployments Assume.assumeFalse(RuntimeType.TOMCAT == RuntimeType.getRuntimeType()); ResourceIdentity identityA = ResourceIdentity.fromString("camel.core.unshared"); MavenCoordinates mavenid = MavenCoordinates.parse("org.apache.camel:camel-core:jar:2.11.0"); ResourceBuilder builderA = provisioner.getMavenResourceBuilder(identityA, mavenid); ResourceHandle handleA = provisioner.installResource(builderA.getResource()); try { Assert.assertSame(handleA.getModule(), runtime.getModule(identityA)); Assert.assertEquals("ACTIVE " + identityA, State.ACTIVE, handleA.getModule().getState()); } finally { handleA.uninstall(); } }
ResourceHandle handleA = provisioner.installSharedResource(builderA.getResource()); try { Assert.assertSame(handleA.getModule(), runtime.getModule(identityA)); Assert.assertEquals("ACTIVE " + identityA, State.ACTIVE, handleA.getModule().getState());