Search in sources :

Example 66 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 67 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 68 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 69 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 70 with ServiceTracker

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

the class SecuredCommandProcessorImpl method trackConverters.

private ServiceTracker<Converter, Converter> trackConverters(BundleContext context) {
    return new ServiceTracker<Converter, Converter>(context, Converter.class.getName(), null) {

        @Override
        public Converter addingService(ServiceReference<Converter> reference) {
            Converter converter = super.addingService(reference);
            addConverter(converter);
            return converter;
        }

        @Override
        public void removedService(ServiceReference<Converter> reference, Converter service) {
            removeConverter(service);
            super.removedService(reference, service);
        }
    };
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Converter(org.apache.felix.service.command.Converter) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)96 ServiceReference (org.osgi.framework.ServiceReference)32 Filter (org.osgi.framework.Filter)27 Activate (org.apache.felix.scr.annotations.Activate)18 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)14 Hashtable (java.util.Hashtable)12 BundleContext (org.osgi.framework.BundleContext)11 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)10 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 Bundle (org.osgi.framework.Bundle)7 Dictionary (java.util.Dictionary)6 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 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 CdiContainer (org.osgi.service.cdi.CdiContainer)4