use of org.apache.aries.osgi.functional.OSGi 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.apache.aries.osgi.functional.OSGi in project aries by apache.
the class DSLTest method testJust.
@Test
public void testJust() {
AtomicInteger atomicInteger = new AtomicInteger(0);
OSGi<Integer> just = just(25);
assertEquals(0, atomicInteger.get());
try (OSGiResult<Integer> result = just.run(bundleContext, atomicInteger::set)) {
assertEquals(25, atomicInteger.get());
}
atomicInteger.set(0);
OSGi<Integer> map = just(25).map(s -> s + 5);
try (OSGiResult<Integer> result = map.run(bundleContext, atomicInteger::set)) {
assertEquals(30, atomicInteger.get());
}
atomicInteger.set(0);
OSGi<Integer> flatMap = just(25).flatMap(s -> just(s + 10));
try (OSGiResult<Integer> result = flatMap.run(bundleContext, atomicInteger::set)) {
assertEquals(35, atomicInteger.get());
}
atomicInteger.set(0);
OSGi<Integer> filter = just(25).filter(s -> s % 2 == 0);
try (OSGiResult<Integer> result = filter.run(bundleContext, atomicInteger::set)) {
assertEquals(0, atomicInteger.get());
}
atomicInteger.set(0);
filter = just(25).filter(s -> s % 2 != 0);
try (OSGiResult<Integer> result = filter.run(bundleContext, atomicInteger::set)) {
assertEquals(25, atomicInteger.get());
}
}
use of org.apache.aries.osgi.functional.OSGi in project aries by apache.
the class DSLTest method testConfigurationsAndRegistrations.
@Test
public void testConfigurationsAndRegistrations() throws InvalidSyntaxException, IOException, InterruptedException {
ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
/* For each factory configuration register a service with the property
key set to the value of the property key that comes with the
configuration */
OSGi<ServiceRegistration<Service>> program = configurations("test.configuration").map(d -> d.get("key")).flatMap(key -> register(Service.class, new Service(), new HashMap<String, Object>() {
{
put("key", key);
}
}));
OSGiResult<ServiceRegistration<Service>> result = program.run(bundleContext);
assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
CountDownLatch addedLatch = new CountDownLatch(3);
ServiceRegistration<?> addedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {
@Override
public String getName() {
return "";
}
@Override
public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
addedLatch.countDown();
}
@Override
public void deleted(String s) {
}
}, new Hashtable<String, Object>() {
{
put("service.pid", "test.configuration");
}
});
CountDownLatch deletedLatch = new CountDownLatch(3);
ServiceRegistration<?> deletedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {
@Override
public String getName() {
return "";
}
@Override
public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
}
@Override
public void deleted(String s) {
deletedLatch.countDown();
}
}, new Hashtable<String, Object>() {
{
put("service.pid", "test.configuration");
}
});
Configuration configuration = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration.update(new Hashtable<String, Object>() {
{
put("key", "service one");
}
});
Configuration configuration2 = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration2.update(new Hashtable<String, Object>() {
{
put("key", "service two");
}
});
Configuration configuration3 = configurationAdmin.createFactoryConfiguration("test.configuration");
configuration3.update(new Hashtable<String, Object>() {
{
put("key", "service three");
}
});
assertTrue(addedLatch.await(10, TimeUnit.SECONDS));
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service one)").size());
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service two)").size());
assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service three)").size());
configuration3.delete();
configuration2.delete();
configuration.delete();
assertTrue(deletedLatch.await(10, TimeUnit.SECONDS));
assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
addedServiceRegistration.unregister();
deletedServiceRegistration.unregister();
result.close();
bundleContext.ungetService(serviceReference);
}
use of org.apache.aries.osgi.functional.OSGi in project aries by apache.
the class DSLTest method testApplicativeApplyTo.
@Test
public void testApplicativeApplyTo() {
AtomicInteger integer = new AtomicInteger(0);
OSGi<Integer> program = just(5).applyTo(just((i) -> i + 5));
program.run(bundleContext, integer::set);
assertEquals(10, integer.get());
}
use of org.apache.aries.osgi.functional.OSGi in project aries by apache.
the class ComponentTest method testComponentApplicative.
@Test
public void testComponentApplicative() throws IOException, TimeoutException {
OSGi<?> program = apply(Component::new, configurations("org.components.MyComponent"), services(Service.class)).flatMap(comp -> register(Component.class, comp, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), comp::setOptional, c -> comp.setOptional(null)), ign -> dynamic(services(ServiceForList.class), comp::addService, comp::removeService)));
ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
serviceTracker.open();
Configuration factoryConfiguration = null;
try (OSGiResult<?> run = program.run(_bundleContext)) {
factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
factoryConfiguration.update(new Hashtable<>());
Thread.sleep(1000);
assertNull(serviceTracker.getService());
ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
Component component = serviceTracker.waitForService(10 * 1000);
assertNotNull(component);
ServiceRegistration<Service> serviceRegistration10 = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
assertEquals(2, serviceTracker.getServiceReferences().length);
Configuration factoryConfiguration2 = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
factoryConfiguration2.update(new Hashtable<>());
Thread.sleep(1000);
assertEquals(4, serviceTracker.getServiceReferences().length);
factoryConfiguration2.delete();
Thread.sleep(1000);
assertEquals(2, serviceTracker.getServiceReferences().length);
serviceRegistration10.unregister();
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();
}
}
}
Aggregations