@Override public HttpService addingService(ServiceReference<HttpService> sref) { if (httpService == null) { httpService = super.addingService(sref); registerHttpServiceServlet(httpService); } return httpService; } };
@Override public void contextDestroyed(ServletContextEvent event) { if (tracker != null) { tracker.close(); } }
/** * Open this {@code ServiceTracker} and begin tracking services. * * <p> * This implementation calls {@code open(false)}. * * @throws java.lang.IllegalStateException If the {@code ModuleContext} with * which this {@code ServiceTracker} was created is no longer valid. * @see #open(boolean) */ public void open() { open(false); }
/** * Returns a service object for one of the services being tracked by this * {@code ServiceTracker}. * * <p> * If any services are being tracked, this implementation returns the result * of calling {@code getService(getServiceReference())}. * * @return A service object or {@code null} if no services are being * tracked. */ public T getService() { T service = cachedService; if (service != null) { if (DEBUG) { LOGGER.debug("ServiceTracker.getService[cached]: " + filter); } return service; } if (DEBUG) { LOGGER.debug("ServiceTracker.getService: " + filter); } ServiceReference<S> reference = getServiceReference(); if (reference == null) { return null; } return cachedService = getService(reference); }
final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return null; ServiceReference<S>[] references = getServiceReferences(); int length = (references == null) ? 0 : references.length; if (length == 0) { objects[i] = getService(references[i]);
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(); } } }
@Override public void removedService(ServiceReference<HttpService> reference, HttpService service) { RuntimeLogger.LOGGER.info("Unregister system HttpService with alias: " + SYSTEM_ALIAS); service.unregister(SYSTEM_ALIAS); super.removedService(reference, service); } };
T object = getService(); if (object != null) { return object; final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ return null; object = getService(); if (endTime > 0) { // if we have a timeout timeout = endTime - System.currentTimeMillis();
ServiceReference<S>[] references = null; if (trackClass != null) { references = getInitialReferences(trackAllServices, trackClass, null); } else { if (trackReference != null) { references = getInitialReferences(trackAllServices, null, 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 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(); } } }
@Override public void removedService(ServiceReference<T> reference, T service) { super.removedService(reference, service); dynamicReference.unbind(service); } };
final Tracked t = tracked(); if (t == null) { /* if ServiceTracker is not open */ if (array.length > 0) { ServiceReference<S>[] references = getServiceReferences(); int length = (references == null) ? 0 : references.length; if (length == 0) { array[i] = getService(references[i]);
@Override public T addingService(ServiceReference<T> reference) { T service = super.addingService(reference); dynamicReference.bind(service); return service; }
public void open() { tracker.open(); }
void close() { tracker.close(); } }
@Override public void removedService(ServiceReference<URLStreamHandler> sref, URLStreamHandler handler) { String protocol = getRequiredProtocol(sref); URLStreamHandlerProxy proxy = handlers.get(protocol); if (proxy != null && proxy.getServiceReference().equals(sref)) { removingHandler(protocol, sref, handler); proxy.update(null, null); ServiceReference<URLStreamHandler>[] srefs = getServiceReferences(); if (srefs != null) { for (ServiceReference<URLStreamHandler> aux : srefs) { if (aux != sref && protocol.equals(getProtocol(aux))) { proxy.update(aux, getService(aux)); break; } } } } super.removedService(sref, handler); } };
@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; } };
DelegatingInvocationHandler(ModuleContext context, Class<T> type, long timeout, TimeUnit unit) { dynamicReference = new DynamicReference<T>(type.getSimpleName(), timeout, unit); tracker = new ServiceTracker<T, T>(context, type, null) { @Override public T addingService(ServiceReference<T> reference) { T service = super.addingService(reference); dynamicReference.bind(service); return service; } @Override public void modifiedService(ServiceReference<T> reference, T service) { super.modifiedService(reference, service); dynamicReference.bind(service); } @Override public void removedService(ServiceReference<T> reference, T service) { super.removedService(reference, service); dynamicReference.unbind(service); } }; tracker.open(); }
public void close() { tracker.close(); }
@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; } };