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);
}
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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
}
Aggregations