Search in sources :

Example 6 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class AbstractJPATransactionTest method getService.

protected <T> T getService(Class<T> clazz, String filter, long timeout) throws InvalidSyntaxException {
    Filter f = FrameworkUtil.createFilter(filter == null ? "(|(foo=bar)(!(foo=bar)))" : filter);
    ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(context, clazz, null) {

        @Override
        public T addingService(ServiceReference<T> reference) {
            return f.match(reference) ? super.addingService(reference) : null;
        }
    };
    tracker.open();
    try {
        T t = tracker.waitForService(timeout);
        if (t == null) {
            throw new NoSuchElementException(clazz.getName());
        }
        return t;
    } catch (InterruptedException e) {
        throw new RuntimeException("Error waiting for service " + clazz.getName(), e);
    } finally {
        trackers.add(tracker);
    }
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) NoSuchElementException(java.util.NoSuchElementException) ServiceReference(org.osgi.framework.ServiceReference)

Example 7 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker 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 8 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project karaf by apache.

the class EncryptableConfigAdminPropertyPlaceholderTest method getOsgiService.

protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            Dictionary dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref);
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref);
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 9 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project karaf by apache.

the class EncryptablePropertyPlaceholderTest method getOsgiService.

protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            Dictionary dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref);
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref);
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 10 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project sling by apache.

the class BindingsValuesProvidersByContextImpl method activate.

@Activate
public void activate(ComponentContext ctx) {
    bundleContext = ctx.getBundleContext();
    synchronized (pendingRefs) {
        for (ServiceReference ref : pendingRefs) {
            addingService(ref);
        }
        pendingRefs.clear();
    }
    bvpTracker = new ServiceTracker(bundleContext, BindingsValuesProvider.class.getName(), this);
    bvpTracker.open();
    // Map services can also be registered to provide bindings
    mapsTracker = new ServiceTracker(bundleContext, Map.class.getName(), this);
    mapsTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)321 ServiceReference (org.osgi.framework.ServiceReference)65 Filter (org.osgi.framework.Filter)41 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)32 BundleContext (org.osgi.framework.BundleContext)28 Hashtable (java.util.Hashtable)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)23 Activate (org.apache.felix.scr.annotations.Activate)20 IContainerManager (org.eclipse.ecf.core.IContainerManager)20 Bundle (org.osgi.framework.Bundle)19 LogService (org.osgi.service.log.LogService)17 Dictionary (java.util.Dictionary)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 IOException (java.io.IOException)11 Test (org.junit.Test)10 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 Map (java.util.Map)7 List (java.util.List)6 Properties (java.util.Properties)6