Search in sources :

Example 16 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testStaticInstance1.

@Test
public void testStaticInstance1() {
    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 -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(), "FooProvider-3");
    assertNotNull("Check ManagedServiceFactory 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);
    }
    // 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 -2", fooP, "foo");
    assertEquals("Check bar equality -2", barP, new Integer(2));
    assertEquals("Check baz equality -2", bazP, "zab");
    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)

Example 17 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testPropagationInstance3.

@Test
public void testPropagationInstance3() {
    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");
    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 ManagedService availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "foo");
    conf.put("bar", new Integer(2));
    conf.put("propagated1", "propagated");
    conf.put("propagated2", new Integer(1));
    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");
    assertNotNull("Check the propagated1 existency", fooRef.getProperty("propagated1"));
    String prop1 = (String) fooRef.getProperty("propagated1");
    assertNotNull("Check the propagated2 existency", fooRef.getProperty("propagated2"));
    Integer prop2 = (Integer) fooRef.getProperty("propagated2");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "zab");
    assertEquals("Check propagated1 equality", prop1, "propagated");
    assertEquals("Check propagated2 equality", prop2, new Integer(1));
    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)

Example 18 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testDynamicInstance2.

@Test
public void testDynamicInstance2() {
    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 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(), 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 19 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testStaticInstance3.

@Test
public void testStaticInstance3() {
    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 -1", fooP, null);
    assertEquals("Check bar equality -1", barP, null);
    assertEquals("Check baz equality -1", bazP, null);
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance-3");
    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(), instance3.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)

Example 20 with ManagedService

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

the class TestManagedServiceConfigurableProperties method testDynamicStringInstance1.

@Test
public void testDynamicStringInstance1() {
    assertEquals("Check instance1 state", ComponentInstance.VALID, instance1.getState());
    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 - 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(), "FooProvider-3");
    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");
    assertEquals("Check instance1 state (2)", ComponentInstance.VALID, instance1.getState());
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    PrimitiveHandler ph = (PrimitiveHandler) ms;
    assertSame("Check the correct instance", ph.getInstanceManager(), instance1);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    assertEquals("Check instance1 state (3)", ComponentInstance.VALID, instance1.getState());
    // Recheck 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 - 2", fooP, "oof");
    assertEquals("Check bar equality - 2", barP, new Integer(0));
    assertEquals("Check baz equality - 2", 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) PrimitiveHandler(org.apache.felix.ipojo.PrimitiveHandler) Properties(java.util.Properties) 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