@Override public <S> S getService(ServiceReference<S> reference) { ServiceReferenceAdaptor<S> adaptor = (ServiceReferenceAdaptor<S>) reference; return moduleContext.getService(adaptor.delegate); }
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; }
/** * Default implementation of the * {@code ServiceTrackerCustomizer.addingService} method. * * <p> * This method is only called when this {@code ServiceTracker} has been * constructed with a {@code null ServiceTrackerCustomizer} argument. * * <p> * This implementation returns the result of calling {@code getService} on * the {@code ModuleContext} with which this {@code ServiceTracker} was * created passing the specified {@code ServiceReference}. * <p> * This method can be overridden in a subclass to customize the service * object to be tracked for the service being added. In that case, take care * not to rely on the default implementation of * {@link #removedService(ServiceReference, Object) removedService} to unget * the service. * * @param reference The reference to the service being added to this * {@code ServiceTracker}. * @return The service object to be tracked for the service added to this * {@code ServiceTracker}. * @see ServiceTrackerCustomizer#addingService(ServiceReference) */ @SuppressWarnings("unchecked") public T addingService(ServiceReference<S> reference) { T result = (T) context.getService(reference); return result; }
protected ConfigurationAdmin getConfigurationAdmin(Module module) { ModuleContext context = module.getModuleContext(); return context.getService(context.getServiceReference(ConfigurationAdmin.class)); } }
@Override public T addingService(ServiceReference<T> sref) { T service = super.addingService(sref); if (serviceFilter == null || serviceFilter.match(sref)) { serviceRef.set(moduleContext.getService(sref)); latch.countDown(); } return service; } };
private LogService getLogService(ModuleContext context) { ServiceReference<LogService> sref = context.getServiceReference(LogService.class); Assert.assertNotNull("LogService not null", sref); return context.getService(sref); } }
@Override public T addingService(ServiceReference<T> sref) { T service = super.addingService(sref); if (srvfilter == null || srvfilter.match(sref)) { serviceRef.set(moduleContext.getService(sref)); latch.countDown(); } return service; } };
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; }
@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 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 } }