Search in sources :

Example 21 with ServiceTracker

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

the class CamelKarafTestSupport method getOsgiService.

@SuppressWarnings("unchecked")
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 22 with ServiceTracker

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

the class Activator method createCommandProcessorTracker.

private ServiceTracker createCommandProcessorTracker() {
    return new ServiceTracker(context, CommandProcessor.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            CommandProcessor processor = (CommandProcessor) super.addingService(reference);
            startShell(context, processor);
            return processor;
        }

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

Example 23 with ServiceTracker

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

the class FileInstall method start.

public void start(BundleContext context) throws Exception {
    this.context = context;
    lock.writeLock().lock();
    try {
        Hashtable<String, Object> props = new Hashtable<String, Object>();
        props.put("url.handler.protocol", JarDirUrlHandler.PROTOCOL);
        urlHandlerRegistration = context.registerService(org.osgi.service.url.URLStreamHandlerService.class.getName(), new JarDirUrlHandler(), props);
        String flt = "(|(" + Constants.OBJECTCLASS + "=" + ArtifactInstaller.class.getName() + ")" + "(" + Constants.OBJECTCLASS + "=" + ArtifactTransformer.class.getName() + ")" + "(" + Constants.OBJECTCLASS + "=" + ArtifactUrlTransformer.class.getName() + "))";
        listenersTracker = new ServiceTracker(context, FrameworkUtil.createFilter(flt), this);
        listenersTracker.open();
        try {
            cmSupport = new ConfigAdminSupport(context, this);
        } catch (NoClassDefFoundError e) {
            Util.log(context, Logger.LOG_DEBUG, "ConfigAdmin is not available, some features will be disabled", e);
        }
        // Created the initial configuration
        Hashtable<String, String> ht = new Hashtable<String, String>();
        set(ht, DirectoryWatcher.POLL);
        set(ht, DirectoryWatcher.DIR);
        set(ht, DirectoryWatcher.LOG_LEVEL);
        set(ht, DirectoryWatcher.LOG_DEFAULT);
        set(ht, DirectoryWatcher.FILTER);
        set(ht, DirectoryWatcher.TMPDIR);
        set(ht, DirectoryWatcher.START_NEW_BUNDLES);
        set(ht, DirectoryWatcher.USE_START_TRANSIENT);
        set(ht, DirectoryWatcher.USE_START_ACTIVATION_POLICY);
        set(ht, DirectoryWatcher.NO_INITIAL_DELAY);
        set(ht, DirectoryWatcher.DISABLE_CONFIG_SAVE);
        set(ht, DirectoryWatcher.ENABLE_CONFIG_SAVE);
        set(ht, DirectoryWatcher.CONFIG_ENCODING);
        set(ht, DirectoryWatcher.START_LEVEL);
        set(ht, DirectoryWatcher.ACTIVE_LEVEL);
        set(ht, DirectoryWatcher.UPDATE_WITH_LISTENERS);
        set(ht, DirectoryWatcher.OPTIONAL_SCOPE);
        set(ht, DirectoryWatcher.FRAGMENT_SCOPE);
        set(ht, DirectoryWatcher.DISABLE_NIO2);
        set(ht, DirectoryWatcher.SUBDIR_MODE);
        // check if dir is an array of dirs
        String dirs = ht.get(DirectoryWatcher.DIR);
        if (dirs != null && dirs.indexOf(',') != -1) {
            StringTokenizer st = new StringTokenizer(dirs, ",");
            int index = 0;
            while (st.hasMoreTokens()) {
                final String dir = st.nextToken().trim();
                ht.put(DirectoryWatcher.DIR, dir);
                String name = "initial";
                if (index > 0)
                    name = name + index;
                updated(name, new Hashtable<String, String>(ht));
                index++;
            }
        } else {
            updated("initial", ht);
        }
    } finally {
        // now notify all the directory watchers to proceed
        // We need this to avoid race conditions observed in FELIX-2791
        lock.writeLock().unlock();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ArtifactTransformer(org.apache.felix.fileinstall.ArtifactTransformer)

Example 24 with ServiceTracker

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

the class Activator method start.

public void start(BundleContext context) throws Exception {
    m_context = context;
    m_tracker = new ServiceTracker(context, TestService.class.getName(), this);
    m_tracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 25 with ServiceTracker

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

the class BaseIntegrationTest method awaitService.

protected <T> T awaitService(String serviceName) throws Exception {
    ServiceTracker tracker = new ServiceTracker(m_context, serviceName, null);
    tracker.open();
    T result;
    try {
        result = (T) tracker.waitForService(DEFAULT_TIMEOUT);
    } finally {
        tracker.close();
    }
    return result;
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

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