/** * 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 an array of <code>ServiceReference</code> objects. The returned array of <code>ServiceReference</code> objects * contains services that were registered under the specified class, match the specified filter expression. * * If checkAssignable is true, the packages for the class names under which the services were registered match the context * module's packages as defined in {@link ServiceReference#isAssignableTo(Bundle, String)}. * * * @param clazz The class name with which the service was registered or <code>null</code> for all services. * @param filterStr The filter expression or <code>null</code> for all services. * @return A potentially empty list of <code>ServiceReference</code> objects. */ List<ServiceState<?>> getServiceReferences(ModuleContext context, String clazz, String filterStr, boolean checkAssignable) { Filter filter = NoFilter.INSTANCE; if (filterStr != null) filter = context.createFilter(filterStr); List<ServiceState<?>> result = getServiceReferencesInternal(context, clazz, filter, checkAssignable); return result; }
/** * Create a {@code ServiceTracker} on the specified {@code ServiceReference} * . * * <p> * The service referenced by the specified {@code ServiceReference} will be * tracked by this {@code ServiceTracker}. * * @param context The {@code ModuleContext} against which the tracking is * done. * @param reference The {@code ServiceReference} for the service 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 ServiceReference<S> reference, final ServiceTrackerCustomizer<S, T> customizer) { this.context = context; this.trackReference = reference; this.trackClass = null; this.customizer = (customizer == null) ? this : customizer; this.listenerFilter = "(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + ")"; this.filter = context.createFilter(listenerFilter); }
public static <T> T awaitService(final ModuleContext moduleContext, Class<T> type, String filterspec, long timeout, TimeUnit unit) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<T> serviceRef = new AtomicReference<T>(); final Filter srvfilter = filterspec != null ? moduleContext.createFilter(filterspec) : null; ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(moduleContext, type, null) { @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; } }; tracker.open(); try { if (!latch.await(timeout, unit)) { throw new RuntimeException("Cannot obtain service: " + srvfilter); } return serviceRef.get(); } catch (InterruptedException ex) { throw new IllegalStateException(); } finally { tracker.close(); } } }
public static <T> T awaitService(final ModuleContext moduleContext, Class<T> type, String filterspec, long timeout, TimeUnit unit) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<T> serviceRef = new AtomicReference<T>(); final Filter serviceFilter = filterspec != null ? moduleContext.createFilter(filterspec) : null; ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(moduleContext, type, null) { @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; } }; tracker.open(); try { if (!latch.await(timeout, unit)) { String srvspec = (type != null ? type.getName() : "") + (serviceFilter != null ? serviceFilter : ""); throw new IllegalStateException("Cannot obtain service: " + srvspec); } return serviceRef.get(); } catch (InterruptedException ex) { throw new IllegalStateException(); } finally { tracker.close(); } } }