Search in sources :

Example 1 with ManagedService

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

the class Activator method start.

@Override
public void start(BundleContext context) throws Exception {
    this.context = context;
    synchronized (this) {
        open = true;
    }
    Dictionary<String, Object> properties = getMSProperties();
    logger.info("Registering for configuration updates {}", properties);
    ManagedService service = c -> configurationUpdated(c, false);
    msReg = context.registerService(ManagedService.class, service, properties);
    new Thread(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        configurationUpdated(null, true);
    }).start();
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) Logger(org.slf4j.Logger) Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) BundleActivator(org.osgi.framework.BundleActivator) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TransactionControl(org.osgi.service.transaction.control.TransactionControl) BundleContext(org.osgi.framework.BundleContext) Map(java.util.Map) RECREATE(org.apache.aries.tx.control.service.xa.impl.Activator.ChangeType.RECREATE) ManagedService(org.osgi.service.cm.ManagedService) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testDynamicStringInstance2.

@Test
public void testDynamicStringInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance");
    assertNotNull("Check ManagedService availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", "0");
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testDynamicInstance3.

@Test
public void testDynamicInstance3() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    Object fooP = fooRef.getProperty("foo");
    Object barP = fooRef.getProperty("bar");
    Object bazP = fooRef.getProperty("baz");
    // No values ... no properties.
    assertEquals("Check foo equality", fooP, null);
    assertEquals("Check bar equality", barP, null);
    assertEquals("Check baz equality", bazP, null);
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance-3");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", new Integer(0));
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance3.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testDynamicInstance1.

@Test
public void testDynamicInstance1() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "FooProvider-3");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", new Integer(0));
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Re-check props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testStaticInstance2.

@Test
public void testStaticInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -1", fooP, "foo");
    assertEquals("Check bar equality -1", barP, new Integer(2));
    assertEquals("Check baz equality -1", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance");
    assertNotNull("Check ManagedService availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("bar", new Integer(2));
    conf.put("foo", "foo");
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -2", fooP, "foo");
    assertEquals("Check bar equality -2", barP, new Integer(2));
    assertEquals("Check baz equality -2", bazP, "zab");
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ManagedService (org.osgi.service.cm.ManagedService)29 ServiceReference (org.osgi.framework.ServiceReference)23 Hashtable (java.util.Hashtable)22 ConfigurationException (org.osgi.service.cm.ConfigurationException)20 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)19 Test (org.junit.Test)19 Properties (java.util.Properties)10 Dictionary (java.util.Dictionary)7 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 Configuration (org.osgi.service.cm.Configuration)3 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)3 Enumeration (java.util.Enumeration)2 Bundle (org.osgi.framework.Bundle)2 Sets (com.google.common.collect.Sets)1 BasicDBObject (com.mongodb.BasicDBObject)1 CommandResult (com.mongodb.CommandResult)1