/** * Open this {@code ServiceTracker} and begin tracking services. * * <p> * This implementation calls {@code open(false)}. * * @throws java.lang.IllegalStateException If the {@code BundleContext} with * which this {@code ServiceTracker} was created is no longer valid. * @see #open(boolean) */ public void open() { open(false); }
private <T> ServiceTracker getServiceTracker(String contractClassName) { if ( !serviceTrackers.containsKey( contractClassName ) ) { final ServiceTracker<T, T> serviceTracker = new ServiceTracker<T, T>( context, contractClassName, null ); serviceTracker.open(); serviceTrackers.put( contractClassName, serviceTracker ); } return serviceTrackers.get( contractClassName ); }
@Override public synchronized void stop(BundleContext bundleContext) throws Exception { this.tracker.close(); }
@Override public Object addingService(ServiceReference serviceRef) { httpService = (HttpService) super.addingService(serviceRef); registerServlets(); return httpService; }
@Override public void removedService(ServiceReference ref, Object service) { if (httpService == service) { unregisterServlets(); httpService = null; } super.removedService(ref, service); } };
/** * Return an array of {@code Bundle}s for all bundles being tracked by this * {@code BundleTracker}. * * @return An array of {@code Bundle}s or {@code null} if no bundles are * being tracked. */ public Bundle[] getBundles() { final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return null; } synchronized (t) { int length = t.size(); if (length == 0) { return null; } return t.copyKeys(new Bundle[length]); } }
/** * Return an array of {@code ServiceReference}s for all services being * tracked by this {@code ServiceTracker}. * * @return Array of {@code ServiceReference}s or {@code null} if no services * are being tracked. */ public ServiceReference<S>[] getServiceReferences() { final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return null; } synchronized (t) { int length = t.size(); if (length == 0) { return null; } @SuppressWarnings("unchecked") ServiceReference<S>[] result = new ServiceReference[length]; return t.copyKeys(result); } }
/** * Remove a bundle from this {@code BundleTracker}. * * The specified bundle will be removed from this {@code BundleTracker} . If * the specified bundle was being tracked then the * {@code BundleTrackerCustomizer.removedBundle} method will be called for * that bundle. * * @param bundle The {@code Bundle} to be removed. */ public void remove(Bundle bundle) { final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return; } t.untrack(bundle, null); }
/** * Remove a service from this {@code ServiceTracker}. * * The specified service will be removed from this {@code ServiceTracker}. * If the specified service was being tracked then the * {@code ServiceTrackerCustomizer.removedService} method will be called for * that service. * * @param reference The reference to the service to be removed. */ public void remove(ServiceReference<S> reference) { final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return; } t.untrack(reference, null); }
/** * Returns the service object for the specified {@code ServiceReference} if * the specified referenced service is being tracked by this * {@code ServiceTracker}. * * @param reference The reference to the desired service. * @return A service object or {@code null} if the service referenced by the * specified {@code ServiceReference} is not being tracked. */ public T getService(ServiceReference<S> reference) { final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return null; } synchronized (t) { return t.getCustomizedObject(reference); } }
/** * Returns the customized object for the specified {@code Bundle} if the * specified bundle is being tracked by this {@code BundleTracker}. * * @param bundle The {@code Bundle} being tracked. * @return The customized object for the specified {@code Bundle} or * {@code null} if the specified {@code Bundle} is not being * tracked. */ public T getObject(Bundle bundle) { final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return null; } synchronized (t) { return t.getCustomizedObject(bundle); } }
/** * Return if this {@code BundleTracker} is empty. * * @return {@code true} if this {@code BundleTracker} is not tracking any * bundles. * @since 1.5 */ public boolean isEmpty() { final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return true; } synchronized (t) { return t.isEmpty(); } }
/** * Return if this {@code ServiceTracker} is empty. * * @return {@code true} if this {@code ServiceTracker} is not tracking any * services. * @since 1.5 */ public boolean isEmpty() { final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return true; } synchronized (t) { return t.isEmpty(); } }
/** * Returns the tracking count for this {@code BundleTracker}. * * The tracking count is initialized to 0 when this {@code BundleTracker} is * opened. Every time a bundle is added, modified or removed from this * {@code BundleTracker} the tracking count is incremented. * * <p> * The tracking count can be used to determine if this {@code BundleTracker} * has added, modified or removed a bundle by comparing a tracking count * value previously collected with the current tracking count value. If the * value has not changed, then no bundle has been added, modified or removed * from this {@code BundleTracker} since the previous tracking count was * collected. * * @return The tracking count for this {@code BundleTracker} or -1 if this * {@code BundleTracker} is not open. */ public int getTrackingCount() { final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return -1; } synchronized (t) { return t.getTrackingCount(); } }
/** * Returns the tracking count for this {@code ServiceTracker}. * * The tracking count is initialized to 0 when this {@code ServiceTracker} * is opened. Every time a service is added, modified or removed from this * {@code ServiceTracker}, the tracking count is incremented. * * <p> * The tracking count can be used to determine if this * {@code ServiceTracker} has added, modified or removed a service by * comparing a tracking count value previously collected with the current * tracking count value. If the value has not changed, then no service has * been added, modified or removed from this {@code ServiceTracker} since * the previous tracking count was collected. * * @since 1.2 * @return The tracking count for this {@code ServiceTracker} or -1 if this * {@code ServiceTracker} is not open. */ public int getTrackingCount() { final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return -1; } synchronized (t) { return t.getTrackingCount(); } }
/** * Return a {@code Map} with the {@code Bundle}s and customized objects for * all bundles being tracked by this {@code BundleTracker}. * * @return A {@code Map} with the {@code Bundle}s and customized objects for * all services being tracked by this {@code BundleTracker}. If no * bundles are being tracked, then the returned map is empty. * @since 1.5 */ public Map<Bundle, T> getTracked() { Map<Bundle, T> map = new HashMap<Bundle, T>(); final Tracked t = tracked(); if (t == null) { /* if BundleTracker is not open */ return map; } synchronized (t) { return t.copyEntries(map); } }
@Override public void stop() { for (String key : serviceTrackers.keySet()) { serviceTrackers.get( key ).close(); } serviceTrackers.clear(); } }
@Override public Object addingService(ServiceReference reference) { LogService service = (LogService) super.addingService(reference); logService = service; return service; }