public static <T> T getService(ModuleContext moduleContext, Class<T> type) { ServiceReference<T> sref = moduleContext.getServiceReference(type); return sref != null ? moduleContext.getService(sref) : null; }
@Override public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException { moduleContext.addServiceListener(new ServiceListenerAdaptor(listener), filter); }
/** * Create a {@code ServiceTracker} on the specified class name. * * <p> * Services registered under the specified class name will be tracked by * this {@code ServiceTracker}. * * @param context The {@code ModuleContext} against which the tracking is * done. * @param clazz The class name of the services to be tracked. * @param customizer The customizer object to call when services are added, * modified, or removed in this {@code ServiceTracker}. If customizer * is {@code null}, then this {@code ServiceTracker} will be used as * the {@code ServiceTrackerCustomizer} and this * {@code ServiceTracker} will call the * {@code ServiceTrackerCustomizer} methods on itself. */ public ServiceTracker(final ModuleContext context, final String clazz, final ServiceTrackerCustomizer<S, T> customizer) { this.context = context; this.trackReference = null; this.trackClass = clazz; this.customizer = (customizer == null) ? this : customizer; // we call clazz.toString to verify clazz is non-null! this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz.toString() + ")"; this.filter = context.createFilter(listenerFilter); }
/** * Returns the list of initial {@code ServiceReference}s that will be * tracked by this {@code ServiceTracker}. * * @param trackAllServices If {@code true}, use * {@code getAllServiceReferences}. * @param className The class name with which the service was registered, or * {@code null} for all services. * @param filterString The filter criteria or {@code null} for all services. * @return The list of initial {@code ServiceReference}s. * @throws InvalidSyntaxException If the specified filterString has an * invalid syntax. */ @SuppressWarnings("unchecked") private ServiceReference<S>[] getInitialReferences(boolean trackAllServices, String className, String filterString) { ServiceReference<S>[] result; if (trackAllServices) { result = (ServiceReference<S>[]) context.getAllServiceReferences(className, filterString); } else { result = (ServiceReference<S>[]) context.getServiceReferences(className, filterString); } return result; }
@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); } }
@Override public String toString() { String className = registration.listener.getClass().getName(); return "ListenerInfo[" + moduleContext.getModule() + "," + className + "," + removed + "]"; } }
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); } }
@Override public <S> S getService(ServiceReference<S> reference) { ServiceReferenceAdaptor<S> adaptor = (ServiceReferenceAdaptor<S>) reference; return moduleContext.getService(adaptor.delegate); }
@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 void removeServiceListener(ServiceListener listener) { moduleContext.removeServiceListener(new ServiceListenerAdaptor(listener)); }
@Override public boolean ungetService(ServiceReference<?> reference) { ServiceReferenceAdaptor<?> adaptor = (ServiceReferenceAdaptor<?>) reference; return moduleContext.ungetService(adaptor.delegate); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public ServiceReference<?>[] getServiceReferences(String className, String filter) throws InvalidSyntaxException { org.jboss.gravia.runtime.ServiceReference<?>[] srefs = moduleContext.getServiceReferences(className, filter); if (srefs == null) return null; List<ServiceReference<?>> result = new ArrayList<ServiceReference<?>>(); for (org.jboss.gravia.runtime.ServiceReference<?> sref : srefs) result.add(new ServiceReferenceAdaptor(sref)); return result.toArray(new ServiceReference[result.size()]); }
@Override public void addBundleListener(BundleListener listener) { moduleContext.addModuleListener(adaptBundleListener(listener)); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public ServiceReference<?>[] getAllServiceReferences(String className, String filter) throws InvalidSyntaxException { org.jboss.gravia.runtime.ServiceReference<?>[] srefs = moduleContext.getAllServiceReferences(className, filter); if (srefs == null) return null; List<ServiceReference<?>> result = new ArrayList<ServiceReference<?>>(); for (org.jboss.gravia.runtime.ServiceReference<?> sref : srefs) result.add(new ServiceReferenceAdaptor(sref)); return result.toArray(new ServiceReference[result.size()]); }
@Override public Bundle getBundle() { Module module = moduleContext.getModule(); return new BundleAdaptor(module); }
/** * 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; }
@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; }
@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(); } } };
/** * Default implementation of the * {@code ServiceTrackerCustomizer.removedService} method. * * <p> * This method is only called when this {@code ServiceTracker} has been * constructed with a {@code null ServiceTrackerCustomizer} argument. * * <p> * This implementation calls {@code ungetService}, 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. If the default * implementation of {@link #addingService(ServiceReference) addingService} * method was used, this method must unget the service. * * @param reference The reference to removed service. * @param service The service object for the removed service. * @see ServiceTrackerCustomizer#removedService(ServiceReference, Object) */ public void removedService(ServiceReference<S> reference, T service) { context.ungetService(reference); }
@Override public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) throws InvalidSyntaxException { Collection<org.jboss.gravia.runtime.ServiceReference<S>> srefs = moduleContext.getServiceReferences(clazz, filter); List<ServiceReference<S>> result = new ArrayList<ServiceReference<S>>(); for (org.jboss.gravia.runtime.ServiceReference<S> sref : srefs) result.add(new ServiceReferenceAdaptor<S>(sref)); return Collections.unmodifiableList(result); }