Search in sources :

Example 21 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class DSLTest method testConfiguration.

@Test
public void testConfiguration() throws IOException, InterruptedException {
    ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
    ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
    AtomicReference<Dictionary<?, ?>> atomicReference = new AtomicReference<>(null);
    Configuration configuration = null;
    CountDownLatch countDownLatch = new CountDownLatch(1);
    try (OSGiResult<Dictionary<String, ?>> result = configuration("test.configuration").run(bundleContext, x -> {
        atomicReference.set(x);
        countDownLatch.countDown();
    })) {
        assertNull(atomicReference.get());
        configuration = configurationAdmin.getConfiguration("test.configuration");
        configuration.update(new Hashtable<>());
        countDownLatch.await(10, TimeUnit.SECONDS);
        assertNotNull(atomicReference.get());
    } finally {
        bundleContext.ungetService(serviceReference);
        if (configuration != null) {
            configuration.delete();
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 22 with Configuration

use of org.osgi.service.cm.Configuration 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);
}
Also used : OSGi.configurations(org.apache.aries.osgi.functional.OSGi.configurations) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) OSGi.onClose(org.apache.aries.osgi.functional.OSGi.onClose) OSGi.register(org.apache.aries.osgi.functional.OSGi.register) HighestRankingRouter.highest(org.apache.aries.osgi.functional.test.HighestRankingRouter.highest) Configuration(org.osgi.service.cm.Configuration) ConfigurationException(org.osgi.service.cm.ConfigurationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OSGi.configuration(org.apache.aries.osgi.functional.OSGi.configuration) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) OSGi.serviceReferences(org.apache.aries.osgi.functional.OSGi.serviceReferences) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) OSGi.just(org.apache.aries.osgi.functional.OSGi.just) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) OSGi.services(org.apache.aries.osgi.functional.OSGi.services) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) OSGi(org.apache.aries.osgi.functional.OSGi) FrameworkUtil(org.osgi.framework.FrameworkUtil) OSGiResult(org.apache.aries.osgi.functional.OSGiResult) Dictionary(java.util.Dictionary) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) ConfigurationException(org.osgi.service.cm.ConfigurationException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 23 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class DSLTest method testConfigurations.

@Test
public void testConfigurations() throws IOException, InterruptedException {
    ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
    ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
    AtomicReference<Dictionary<?, ?>> atomicReference = new AtomicReference<>(null);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Configuration configuration = null;
    try (OSGiResult<Dictionary<String, ?>> result = configurations("test.configuration").run(bundleContext, x -> {
        atomicReference.set(x);
        countDownLatch.countDown();
    })) {
        assertNull(atomicReference.get());
        configuration = configurationAdmin.createFactoryConfiguration("test.configuration");
        configuration.update(new Hashtable<>());
        countDownLatch.await(10, TimeUnit.SECONDS);
        assertNotNull(atomicReference.get());
    } finally {
        bundleContext.ungetService(serviceReference);
        if (configuration != null) {
            configuration.delete();
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 24 with Configuration

use of org.osgi.service.cm.Configuration 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();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Bean(javax.enterprise.inject.spi.Bean) Filter(org.osgi.framework.Filter) BeanManager(javax.enterprise.inject.spi.BeanManager) Map(java.util.Map) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 25 with Configuration

use of org.osgi.service.cm.Configuration 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();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7