Search in sources :

Example 81 with ServiceTracker

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

the class Activator method start.

public void start(final BundleContext bc) {
    tracker = new ServiceTracker(bc, GreeterService.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            Object result = super.addingService(reference);
            useService(bc, reference);
            return result;
        }
    };
    tracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceReference(org.osgi.framework.ServiceReference)

Example 82 with ServiceTracker

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

the class ConfigAdminPropsFileContentHandlerTest method testConfigurationContentHandler.

@Test
public void testConfigurationContentHandler() throws Exception {
    // This test works as follows: it first installs a subsystem (cmContent.esa)
    // that contains two configuration files (org.foo.Bar.cfg and com.blah.Blah.cfg)
    // These configuration files are marked as 'osgi.config' content type.
    // The ConfigAdminContentHandler handles the installation of this content
    // and registers them as configuration with the Config Admin Service.
    // The .esa file also contains an ordinary bundle that registers two
    // Config Admin ManagedServices. Each registerd under one of the PIDs.
    // Once they receive the expected configuration they each register a String
    // service to mark that they have.
    // After starting the subsystem this test waits for these 'String' services
    // to appear so that it knows that the whole process worked.
    Subsystem subsystem = installSubsystemFromFile("cmContent.esa");
    subsystem.start();
    // Now check that both Managed Services (Config Admin services) have been configured
    // If they are configured correctly they will register a marker String service to
    // indicate this.
    Filter f = bundleContext.createFilter("(&(objectClass=java.lang.String)(test.pid=org.foo.Bar))");
    ServiceTracker<String, String> barTracker = new ServiceTracker<String, String>(bundleContext, f, null);
    try {
        barTracker.open();
        String blahSvc = barTracker.waitForService(2000);
        assertEquals("Bar!", blahSvc);
    } finally {
        barTracker.close();
    }
    Filter f2 = bundleContext.createFilter("(&(objectClass=java.lang.String)(test.pid=com.blah.Blah))");
    ServiceTracker<String, String> blahTracker = new ServiceTracker<String, String>(bundleContext, f2, null);
    try {
        blahTracker.open();
        String blahSvc = blahTracker.waitForService(2000);
        assertEquals("Blah!", blahSvc);
    } finally {
        blahTracker.close();
    }
    stopAndUninstallSubsystemSilently(subsystem);
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Subsystem(org.osgi.service.subsystem.Subsystem) Test(org.junit.Test)

Example 83 with ServiceTracker

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

the class RichBundleContext method getService.

public <T> T getService(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(delegate, osgiFilter, null);
        tracker.open();
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            System.out.println("Could not obtain a service in time, service-ref=" + tracker.getServiceReference() + ", time=" + System.currentTimeMillis());
            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) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 84 with ServiceTracker

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

the class AbstractTransactionTest method getService.

private <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 85 with ServiceTracker

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

the class Helper method getOsgiService.

public static <T> T getOsgiService(BundleContext bundleContext, 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 = 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 + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            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) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)115 ServiceReference (org.osgi.framework.ServiceReference)33 Filter (org.osgi.framework.Filter)28 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)19 Hashtable (java.util.Hashtable)18 Activate (org.apache.felix.scr.annotations.Activate)18 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)14 BundleContext (org.osgi.framework.BundleContext)12 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Dictionary (java.util.Dictionary)7 Bundle (org.osgi.framework.Bundle)7 Converter (net.heartsome.cat.converter.Converter)6 AndFilter (net.heartsome.cat.converter.util.AndFilter)6 EqFilter (net.heartsome.cat.converter.util.EqFilter)6 Configuration (org.osgi.service.cm.Configuration)6 HashMap (java.util.HashMap)5 CdiContainer (org.osgi.service.cdi.CdiContainer)4