@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public ServiceReference<?> getServiceReference(String className) { org.jboss.gravia.runtime.ServiceReference<?> sref = moduleContext.getServiceReference(className); return sref != null ? new ServiceReferenceAdaptor(sref) : null; }
@Override public <S> ServiceReference<S> getServiceReference(Class<S> clazz) { org.jboss.gravia.runtime.ServiceReference<S> sref = moduleContext.getServiceReference(clazz); return sref != null ? new ServiceReferenceAdaptor<S>(sref) : null; }
public static <T> T getService(ModuleContext moduleContext, Class<T> type) { ServiceReference<T> sref = moduleContext.getServiceReference(type); return sref != null ? moduleContext.getService(sref) : null; }
public static <T> T getService(ModuleContext moduleContext, Class<T> type) { ServiceReference<T> sref = moduleContext.getServiceReference(type); return sref != null ? moduleContext.getService(sref) : null; }
protected ConfigurationAdmin getConfigurationAdmin(Module module) { ModuleContext context = module.getModuleContext(); return context.getService(context.getServiceReference(ConfigurationAdmin.class)); } }
private LogService getLogService(ModuleContext context) { ServiceReference<LogService> sref = context.getServiceReference(LogService.class); Assert.assertNotNull("LogService not null", sref); return context.getService(sref); } }
public static <T> T getService(Class<T> type) { ModuleContext moduleContext = RuntimeLocator.getRequiredRuntime().getModuleContext(); ServiceReference<T> sref = moduleContext.getServiceReference(type); return sref != null ? moduleContext.getService(sref) : null; }
public static <T> T getService(Class<T> type) { ModuleContext moduleContext = RuntimeLocator.getRequiredRuntime().getModuleContext(); ServiceReference<T> sref = moduleContext.getServiceReference(type); return sref != null ? moduleContext.getService(sref) : null; }
private void assertDisableComponent(final ModuleContext ctxA, ServiceA srvA) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); ServiceListener listener = new ServiceListener() { @Override public void serviceChanged(ServiceEvent event) { int type = event.getType(); ServiceReference<?> sref = event.getServiceReference(); List<String> clazzes = Arrays.asList(((String[]) sref.getProperty(Constants.OBJECTCLASS))); if (type == ServiceEvent.UNREGISTERING && clazzes.contains(ServiceA1.class.getName())) { ctxA.removeServiceListener(this); latch.countDown(); } } }; ctxA.addServiceListener(listener); ComponentContext ccA1 = srvA.getServiceA1().getComponentContext(); ccA1.disableComponent(ServiceA1.class.getName()); Assert.assertTrue(latch.await(2, TimeUnit.SECONDS)); ServiceReference<ServiceA> srefA = ctxA.getServiceReference(ServiceA.class); Assert.assertNull("ServiceReference null", srefA); ServiceReference<ServiceA1> srefA1 = ctxA.getServiceReference(ServiceA1.class); Assert.assertNull("ServiceReference null", srefA1); } }
@Before public void setUp() throws Exception { Runtime runtime = RuntimeLocator.getRequiredRuntime(); ModuleContext syscontext = runtime.getModuleContext(); ServiceReference<Resolver> sref = syscontext.getServiceReference(Resolver.class); Assert.assertNotNull("Resolver reference not null", sref); resolver = syscontext.getService(sref); }
@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 { // 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 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 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); } }