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