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