Search in sources :

Example 81 with ServiceReference

use of org.osgi.framework.ServiceReference in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkTest method registerServicesWithSameSymbolicName.

private void registerServicesWithSameSymbolicName(String symbolicName, SomeInterface... someInterfaces) throws InvalidSyntaxException {
    ArrayList<ServiceReference<SomeInterface>> references = new ArrayList<>();
    for (int i = 0; i < someInterfaces.length; ++i) {
        ServiceReference reference = mock(ServiceReference.class);
        Bundle bundle = mock(Bundle.class);
        when(reference.getBundle()).thenReturn(bundle);
        when(bundle.getSymbolicName()).thenReturn(TEST_SYMBOLIC_NAME);
        when(bundleContext.getService(reference)).thenReturn(someInterfaces[i]);
        references.add(reference);
    }
    String propertyFormat = String.format("(%s=%s)", Constants.BUNDLE_SYMBOLICNAME, symbolicName);
    when(bundleContext.getServiceReferences(SomeInterface.class, propertyFormat)).thenReturn(references);
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 82 with ServiceReference

use of org.osgi.framework.ServiceReference in project camel by apache.

the class AbstractFeatureTest method getOsgiService.

@SuppressWarnings("unchecked")
public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
    ServiceTracker tracker;
    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();
            LOG.warn("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                LOG.warn("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                LOG.warn("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)

Example 83 with ServiceReference

use of org.osgi.framework.ServiceReference 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 84 with ServiceReference

use of org.osgi.framework.ServiceReference in project camel by apache.

the class Activator method updateAvailableScriptLanguages.

private void updateAvailableScriptLanguages() {
    ServiceReference<LanguageResolver> ref = null;
    try {
        Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
        if (references.size() == 1) {
            // Unregistry the old language resolver first
            if (registration != null) {
                registration.unregister();
                registration = null;
            }
            ref = references.iterator().next();
            LanguageResolver resolver = context.getService(ref);
            Dictionary props = new Hashtable();
            // Just publish the language resolve with the language we found
            props.put("language", getAvailableScriptNames());
            registration = context.registerService(LanguageResolver.class, resolver, props);
        }
    } catch (InvalidSyntaxException e) {
        LOG.error("Invalid syntax for LanguageResolver service reference filter.");
    } finally {
        if (ref != null) {
            context.ungetService(ref);
        }
    }
}
Also used : Dictionary(java.util.Dictionary) LanguageResolver(org.apache.camel.spi.LanguageResolver) Hashtable(java.util.Hashtable) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 85 with ServiceReference

use of org.osgi.framework.ServiceReference in project camel by apache.

the class CamelMockBundleContext method addServicePID.

private static void addServicePID(ServiceReference[] srs, String filter) {
    for (ServiceReference sr : srs) {
        if (sr instanceof MockServiceReference) {
            Dictionary properties = new Hashtable();
            String pid = filter.replace("(" + Constants.SERVICE_PID + "=", "").replace(")", "");
            properties.put(Constants.SERVICE_PID, pid);
            for (String key : sr.getPropertyKeys()) {
                if (properties.get(key) == null) {
                    properties.put(key, sr.getProperty(key));
                }
            }
            ((MockServiceReference) sr).setProperties(properties);
        }
    }
}
Also used : MockServiceReference(org.springframework.osgi.mock.MockServiceReference) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) MockServiceReference(org.springframework.osgi.mock.MockServiceReference) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65