Search in sources :

Example 1 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestDynamicPropsReconfiguration method testFactoryReconf.

@Test
public void testFactoryReconf() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Boolean boolProp = (Boolean) sr.getProperty("boolean");
    String strProp = (String) sr.getProperty("string");
    String[] strAProp = (String[]) sr.getProperty("strAProp");
    int[] intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(0));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String(""));
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[0];
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    int[] v2 = new int[0];
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ServiceReference fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    Properties p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", 1);
    p3.put("boolean", true);
    p3.put("string", "foo");
    p3.put("strAProp", new String[] { "foo", "bar", "baz" });
    p3.put("intAProp", new int[] { 1, 2, 3 });
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNull("Check intAProp hidding (no value)", intAProp);
    // Reconfiguration
    fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", 1);
    p3.put("boolean", true);
    p3.put("string", "foo");
    p3.put("strAProp", new String[] { "foo", "bar", "baz" });
    p3.put("intAProp", new int[] { 1, 2, 3 });
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 2 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestDynamicPropsReconfiguration method testFactoryReconfNoValue.

@Test
public void testFactoryReconfNoValue() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-4");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Object boolProp = sr.getProperty("boolean");
    Object strProp = sr.getProperty("string");
    Object strAProp = sr.getProperty("strAProp");
    int[] intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(4));
    assertEquals("Check longProp equality", boolProp, null);
    assertEquals("Check strProp equality", strProp, null);
    assertNull("Check strAProp nullity", strAProp);
    assertNotNull("Check intAProp not nullity", intAProp);
    int[] v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ServiceReference fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    Properties p3 = new Properties();
    p3.put("instance.name", "FooProvider-4");
    p3.put("int", new Integer(1));
    p3.put("boolean", new Boolean(true));
    p3.put("string", new String("foo"));
    p3.put("strAProp", new String[] { "foo", "bar", "baz" });
    p3.put("intAProp", new int[] { 1, 2, 3 });
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-4");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < ((String[]) strAProp).length; i++) {
        if (!((String[]) strAProp)[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar" };
    for (int i = 0; i < ((String[]) strAProp).length; i++) {
        if (!((String[]) strAProp)[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNull("Check intAProp hidding (no value)", intAProp);
    // Reconfiguration
    fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", new Integer(1));
    p3.put("boolean", new Boolean(true));
    p3.put("string", new String("foo"));
    p3.put("strAProp", new String[] { "foo", "bar", "baz" });
    p3.put("intAProp", new int[] { 1, 2, 3 });
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < ((String[]) strAProp).length; i++) {
        if (!((String[]) strAProp)[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    fact = null;
    fs = null;
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestProvidedServiceArchitecture method testProps.

@Test
public void testProps() {
    String factName = "PS-FooProviderType-3";
    String compName = "FooProvider";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider", fact);
    Properties props = new Properties();
    props.put("instance.name", compName);
    props.put("foo", "foo");
    props.put("bar", "2");
    props.put("baz", "baz");
    ipojoHelper.createComponentInstance(factName, props);
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
    assertNotNull("Architecture not available", arch_ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
    InstanceDescription id = arch.getInstanceDescription();
    assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
    assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooProviderType1");
    HandlerDescription[] handlers = id.getHandlers();
    assertEquals("Number of handlers", handlers.length, 3);
    // Look for the ProvidedService Handler
    ProvidedServiceHandlerDescription pshd = null;
    pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
    assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
    ProvidedServiceDescription[] ps = pshd.getProvidedServices();
    assertEquals("Check ProvidedService number", ps.length, 1);
    assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 1);
    assertEquals("Check Provided Service Specs - 2", ps[0].getServiceSpecifications()[0], FooService.class.getName());
    assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
    Properties prop = ps[0].getProperties();
    assertNotNull("Check Props", prop);
    assertEquals("Check service properties number (#" + prop + "?=5)", prop.size(), 5);
    assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
    assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
    assertEquals("Check foo property", prop.get("foo"), "foo");
    assertEquals("Check bar property", prop.get("bar"), new Integer(2));
    assertEquals("Check baz property", prop.get("baz"), "baz");
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) ServiceReference(org.osgi.framework.ServiceReference) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) Test(org.junit.Test)

Example 4 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestProvidedServiceArchitecture method testDoubleProviding.

@Test
public void testDoubleProviding() {
    String factName = "PS-FooBarProviderType-1";
    String compName = "FooProvider";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider", fact);
    ipojoHelper.createComponentInstance(factName, compName);
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
    assertNotNull("Architecture not available", arch_ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
    InstanceDescription id = arch.getInstanceDescription();
    assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
    assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooBarProviderType1");
    HandlerDescription[] handlers = id.getHandlers();
    assertEquals("Number of handlers", handlers.length, 2);
    // Look for the ProvidedService Handler
    ProvidedServiceHandlerDescription pshd = null;
    pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
    // for(int i = 0; i < handlers.length; i++) {
    // if(handlers[i].getHandlerName().equals("org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler")) {
    // pshd = (ProvidedServiceHandlerDescription) handlers[i];
    // }
    // }
    assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
    ProvidedServiceDescription[] ps = pshd.getProvidedServices();
    assertEquals("Check ProvidedService number", ps.length, 1);
    assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 2);
    assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), FooService.class.getName());
    assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), BarService.class.getName());
    assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) Factory(org.apache.felix.ipojo.Factory) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestSimplePS method testPS.

@Test
public void testPS() {
    String factName = "PS-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref;
    // Check that no Foo Service are available
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS already available", ref);
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    ipojoHelper.createComponentInstance(factName, compName);
    // Get a FooService provider
    ref = osgiHelper.getServiceReference(FooService.class.getName(), "(" + "instance.name" + "=" + compName + ")");
    assertNotNull("FS not available", ref);
    // Test foo invocation
    FooService fs = (FooService) osgiHelper.getRawServiceObject(ref);
    assertTrue("FooService invocation failed", fs.foo());
    ipojoHelper.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName(), null);
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

Factory (org.apache.felix.ipojo.Factory)111 Properties (java.util.Properties)88 Test (org.junit.Test)71 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)53 Before (org.junit.Before)33 ServiceReference (org.osgi.framework.ServiceReference)29 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)17 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)13 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)11 IOException (java.io.IOException)6 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4