Search in sources :

Example 1 with ManagedServiceFactory

use of org.osgi.service.cm.ManagedServiceFactory 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 2 with ManagedServiceFactory

use of org.osgi.service.cm.ManagedServiceFactory in project felix by apache.

the class FactoryConfigurationAdapterImplTest method testInvokePlainUpdatedMethodOk.

@Test
public void testInvokePlainUpdatedMethodOk() throws Exception {
    Ensure ensure = createEnsure();
    PlainService service = new PlainService(ensure);
    FactoryConfigurationAdapterImpl cdi = createConfigurationDependency(service);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).updated(CONF_PID, (Dictionary<String, ?>) createDictionary());
    ensure.waitForStep(1, 1000);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).deleted(CONF_PID);
    ensure.waitForStep(2, 1000);
}
Also used : PlainService(org.apache.felix.dm.impl.ConfigurationDependencyImplTest.PlainService) Ensure(test.Ensure) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Test(org.junit.Test)

Example 3 with ManagedServiceFactory

use of org.osgi.service.cm.ManagedServiceFactory in project felix by apache.

the class FactoryConfigurationAdapterImplTest method testInvokeFancyUpdatedMethodOk.

@Test
public void testInvokeFancyUpdatedMethodOk() throws Exception {
    Ensure ensure = createEnsure();
    FancyService service = new FancyService(ensure);
    FactoryConfigurationAdapterImpl cdi = createConfigurationDependency(service, MyConfiguration.class);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).updated(CONF_PID, (Dictionary<String, ?>) createDictionary());
    ensure.waitForStep(1, 1000);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).deleted(CONF_PID);
    ensure.waitForStep(2, 1000);
}
Also used : FancyService(org.apache.felix.dm.impl.ConfigurationDependencyImplTest.FancyService) Ensure(test.Ensure) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Test(org.junit.Test)

Example 4 with ManagedServiceFactory

use of org.osgi.service.cm.ManagedServiceFactory in project felix by apache.

the class FactoryConfigurationAdapterImplTest method testDoNotInvokeFancyUpdatedMethodWithWrongSignatureOk.

@Test
public void testDoNotInvokeFancyUpdatedMethodWithWrongSignatureOk() throws Exception {
    Ensure ensure = createEnsure();
    FancyService service = new FancyService(ensure);
    FactoryConfigurationAdapterImpl cdi = createConfigurationDependency(service, Map.class);
    ensure.step(1);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).updated(CONF_PID, (Dictionary<String, ?>) createDictionary());
    TimeUnit.SECONDS.sleep(1L);
    // Our step shouldn't be changed...
    ensure.waitForStep(1, 1000);
}
Also used : FancyService(org.apache.felix.dm.impl.ConfigurationDependencyImplTest.FancyService) Ensure(test.Ensure) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Test(org.junit.Test)

Example 5 with ManagedServiceFactory

use of org.osgi.service.cm.ManagedServiceFactory in project felix by apache.

the class FactoryConfigurationAdapterImplTest method testInvokeManagedServiceUpdatedMethodOk.

@Test
public void testInvokeManagedServiceUpdatedMethodOk() throws Exception {
    Ensure ensure = createEnsure();
    AManagedService service = new AManagedService(ensure);
    FactoryConfigurationAdapterImpl cdi = createConfigurationDependency(service);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).updated(CONF_PID, (Dictionary<String, ?>) createDictionary());
    ensure.waitForStep(1, 1000);
    ((ManagedServiceFactory) cdi.m_component.getInstance()).deleted(CONF_PID);
    ensure.waitForStep(2, 1000);
}
Also used : AManagedService(org.apache.felix.dm.impl.ConfigurationDependencyImplTest.AManagedService) Ensure(test.Ensure) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Test(org.junit.Test)

Aggregations

ManagedServiceFactory (org.osgi.service.cm.ManagedServiceFactory)8 Test (org.junit.Test)5 Ensure (test.Ensure)4 Dictionary (java.util.Dictionary)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Hashtable (java.util.Hashtable)2 FancyService (org.apache.felix.dm.impl.ConfigurationDependencyImplTest.FancyService)2 BundleContext (org.osgi.framework.BundleContext)2 FrameworkUtil (org.osgi.framework.FrameworkUtil)2 Sets (com.google.common.collect.Sets)1 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)1 Permissions (ddf.security.permission.Permissions)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1