use of org.osgi.util.tracker.ServiceTracker in project sling by apache.
the class ITConfigPrinter method waitForPrinter.
private void waitForPrinter() throws InterruptedException {
if (configPrinter == null) {
tracker = new ServiceTracker(bundleContext, "org.apache.sling.commons.log.logback.internal.SlingConfigurationPrinter", null);
tracker.open();
configPrinter = tracker.waitForService(0);
}
}
use of org.osgi.util.tracker.ServiceTracker in project aries by apache.
the class OSGiBeanDescriptorTests method testServices.
public void testServices() throws Exception {
Bundle tb2Bundle = installBundle("tb2.jar");
ServiceTracker<Pojo, Pojo> st = new ServiceTracker<Pojo, Pojo>(bundleContext, Pojo.class, null);
st.open(true);
try {
Pojo pojo = st.waitForService(timeout);
assertNotNull(pojo);
} finally {
tb2Bundle.uninstall();
}
}
use of org.osgi.util.tracker.ServiceTracker in project aries by apache.
the class ComponentTest method testComponent.
@Test
public void testComponent() throws IOException {
OSGi<?> program = configurations("org.components.MyComponent").flatMap(props -> services(Service.class).flatMap(ms -> just(new Component(props, ms)).flatMap(component -> register(Component.class, component, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), component::setOptional, c -> component.setOptional(null)), ign -> dynamic(services(ServiceForList.class), component::addService, component::removeService)))));
ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
serviceTracker.open();
CountDownLatch countDownLatch = new CountDownLatch(1);
_bundleContext.registerService(ManagedService.class, dictionary -> countDownLatch.countDown(), new Hashtable<String, Object>() {
{
put("service.pid", "org.components.MyComponent");
}
});
Configuration factoryConfiguration = null;
try (OSGiResult<?> run = program.run(_bundleContext)) {
factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
factoryConfiguration.update(new Hashtable<>());
countDownLatch.await(10, TimeUnit.SECONDS);
assertNull(serviceTracker.getService());
ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
Component component = serviceTracker.waitForService(10 * 1000);
assertNotNull(component);
assertNull(component.getOptional());
ServiceRegistration<ServiceOptional> serviceRegistration2 = _bundleContext.registerService(ServiceOptional.class, new ServiceOptional(), new Hashtable<>());
Thread.sleep(1000L);
assertNotNull(component.getOptional());
ServiceOptional serviceOptional = new ServiceOptional();
ServiceRegistration<ServiceOptional> serviceRegistration3 = _bundleContext.registerService(ServiceOptional.class, serviceOptional, new Hashtable<String, Object>() {
{
put("service.ranking", 1);
}
});
assertEquals(serviceOptional, component.getOptional());
serviceRegistration3.unregister();
assertNotNull(component.getOptional());
serviceRegistration2.unregister();
assertNull(component.getOptional());
ServiceRegistration<ServiceForList> serviceRegistration4 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
ServiceRegistration<ServiceForList> serviceRegistration5 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
assertEquals(2, component.getServiceForLists().size());
serviceRegistration4.unregister();
assertEquals(1, component.getServiceForLists().size());
serviceRegistration5.unregister();
assertEquals(0, component.getServiceForLists().size());
serviceRegistration.unregister();
assertNull(serviceTracker.getService());
} catch (IOException ioe) {
} catch (InterruptedException e) {
Assert.fail("Timeout waiting for configuration");
} finally {
serviceTracker.close();
if (factoryConfiguration != null) {
factoryConfiguration.delete();
}
}
}
use of org.osgi.util.tracker.ServiceTracker in project aries by apache.
the class ConfigurationTests method testNamedConfiguration.
@SuppressWarnings({ "unchecked", "serial" })
public void testNamedConfiguration() throws Exception {
Bundle tb3Bundle = installBundle("tb3.jar");
Configuration configurationA = null, configurationB = null;
try {
configurationA = configurationAdmin.getConfiguration("configA", "?");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("ports", new int[] { 12, 4567 });
configurationA.update(properties);
configurationB = configurationAdmin.getConfiguration("configB", "?");
properties = new Hashtable<>();
properties.put("color", "green");
properties.put("ports", new int[] { 80 });
configurationB.update(properties);
Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
ServiceTracker<CdiContainer, CdiContainer> st = new ServiceTracker<>(bundleContext, filter, null);
st.open();
CdiContainer container = st.waitForService(timeout);
assertNotNull(container);
int t = st.getTrackingCount();
BeanManager beanManager = container.getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans("configB");
assertNotNull(beans);
Bean<? extends Object> bean = beanManager.resolve(beans);
CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
Map<String, Object> config = (Map<String, Object>) beanManager.getReference(bean, new TypeLiteral<Map<String, Object>>() {
}.getType(), ctx);
assertNotNull(config);
assertEquals("green", config.get("color"));
assertArrayEquals(new int[] { 80 }, (int[]) config.get("ports"));
configurationA.delete();
while (t == st.getTrackingCount()) {
Thread.sleep(10);
}
assertTrue(st.isEmpty());
st.close();
filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.WAITING_FOR_CONFIGURATIONS + "))");
st = new ServiceTracker<>(bundleContext, filter, null);
st.open();
assertFalse(st.isEmpty());
} finally {
if (configurationB != null) {
try {
configurationB.delete();
} catch (Exception e) {
// ignore
}
}
tb3Bundle.uninstall();
}
}
use of org.osgi.util.tracker.ServiceTracker in project aries by apache.
the class ConfigurationTests method testOptionalConfiguration.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testOptionalConfiguration() throws Exception {
Bundle tb5Bundle = installBundle("tb5.jar");
Configuration configurationC = null;
try {
Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb5Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
containerTracker.open();
containerTracker.waitForService(timeout);
ServiceTracker<BeanService, BeanService> stC = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=C))"), null);
stC.open(true);
BeanService<Callable<int[]>> beanService = stC.waitForService(timeout);
int t = stC.getTrackingCount();
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 35777 }, beanService.get().call());
configurationC = configurationAdmin.getConfiguration("foo.bar", "?");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("ports", new int[] { 12, 4567 });
configurationC.update(properties);
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
t = stC.getTrackingCount();
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
t = stC.getTrackingCount();
beanService = stC.waitForService(timeout);
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
configurationC.delete();
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
beanService = stC.waitForService(timeout);
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 35777 }, beanService.get().call());
} finally {
if (configurationC != null) {
try {
configurationC.delete();
} catch (Exception e) {
// ignore
}
}
tb5Bundle.uninstall();
}
}
Aggregations