Search in sources :

Example 1 with Tracker

use of org.apache.jackrabbit.oak.spi.whiteboard.Tracker in project jackrabbit-oak by apache.

the class OsgiWhiteboard method track.

/**
     * Returns a tracker for services of the given type. The returned tracker
     * is optimized for frequent {@link Tracker#getServices()} calls through
     * the use of a pre-compiled list of services that's atomically updated
     * whenever services are added, modified or removed.
     */
@Override
public <T> Tracker<T> track(final Class<T> type) {
    checkNotNull(type);
    final AtomicReference<List<T>> list = new AtomicReference<List<T>>(Collections.<T>emptyList());
    final ServiceTrackerCustomizer customizer = new ServiceTrackerCustomizer() {

        private final Map<ServiceReference, T> services = newHashMap();

        @Override
        @SuppressWarnings("unchecked")
        public synchronized Object addingService(ServiceReference reference) {
            Object service = context.getService(reference);
            if (type.isInstance(service)) {
                services.put(reference, (T) service);
                list.set(getServiceList(services));
                return service;
            } else {
                context.ungetService(reference);
                return null;
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public synchronized void modifiedService(ServiceReference reference, Object service) {
            // TODO: Figure out if the old reference instance
            // would automatically reflect the updated properties.
            // For now we play it safe by replacing the old key
            // with the new reference instance passed as argument.
            services.remove(reference);
            services.put(reference, (T) service);
            list.set(getServiceList(services));
        }

        @Override
        public synchronized void removedService(ServiceReference reference, Object service) {
            services.remove(reference);
            list.set(getServiceList(services));
            // TODO: Note that the service might still be in use
            // by some client that called getServices() before
            // this method was invoked.
            context.ungetService(reference);
        }
    };
    final ServiceTracker tracker = new ServiceTracker(context, type.getName(), customizer);
    tracker.open();
    return new Tracker<T>() {

        @Override
        public List<T> getServices() {
            return list.get();
        }

        @Override
        public void stop() {
            tracker.close();
        }
    };
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Tracker(org.apache.jackrabbit.oak.spi.whiteboard.Tracker) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Map(java.util.Map) Maps.newTreeMap(com.google.common.collect.Maps.newTreeMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SortedMap(java.util.SortedMap) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with Tracker

use of org.apache.jackrabbit.oak.spi.whiteboard.Tracker in project jackrabbit-oak by apache.

the class OsgiWhiteboard method track.

private <T> Tracker<T> track(Class<T> type, Filter filter) {
    checkNotNull(type);
    final AtomicReference<List<T>> list = new AtomicReference<List<T>>(Collections.<T>emptyList());
    final ServiceTrackerCustomizer customizer = new ServiceTrackerCustomizer() {

        private final Map<ServiceReference, T> services = newHashMap();

        @Override
        @SuppressWarnings("unchecked")
        public synchronized Object addingService(ServiceReference reference) {
            Object service = context.getService(reference);
            if (type.isInstance(service)) {
                services.put(reference, (T) service);
                list.set(getServiceList(services));
                return service;
            } else {
                context.ungetService(reference);
                return null;
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public synchronized void modifiedService(ServiceReference reference, Object service) {
            // TODO: Figure out if the old reference instance
            // would automatically reflect the updated properties.
            // For now we play it safe by replacing the old key
            // with the new reference instance passed as argument.
            services.remove(reference);
            services.put(reference, (T) service);
            list.set(getServiceList(services));
        }

        @Override
        public synchronized void removedService(ServiceReference reference, Object service) {
            services.remove(reference);
            list.set(getServiceList(services));
            // TODO: Note that the service might still be in use
            // by some client that called getServices() before
            // this method was invoked.
            context.ungetService(reference);
        }
    };
    final ServiceTracker tracker = new ServiceTracker(context, filter, customizer);
    tracker.open();
    return new Tracker<T>() {

        @Override
        public List<T> getServices() {
            return list.get();
        }

        @Override
        public void stop() {
            tracker.close();
        }
    };
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Tracker(org.apache.jackrabbit.oak.spi.whiteboard.Tracker) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Map(java.util.Map) Maps.newTreeMap(com.google.common.collect.Maps.newTreeMap) Collections.emptyMap(java.util.Collections.emptyMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SortedMap(java.util.SortedMap) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 Maps.newTreeMap (com.google.common.collect.Maps.newTreeMap)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Tracker (org.apache.jackrabbit.oak.spi.whiteboard.Tracker)2 ServiceReference (org.osgi.framework.ServiceReference)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)2 Collections.emptyMap (java.util.Collections.emptyMap)1