Search in sources :

Example 1 with ServiceContext

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

the class TestCompositeAPI method testAPI2.

@Test
public void testAPI2() {
    Factory fact1 = ipojoHelper.getFactory("composite.empty");
    Properties p = new Properties();
    p.put("instance.name", "empty-2");
    ComponentInstance empty = null;
    try {
        empty = fact1.createComponentInstance(p);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ServiceContext sc = getServiceContext(empty);
    Factory fact2 = ipojoHelper.getFactory(sc, "composite.test.2");
    Properties props2 = new Properties();
    props2.put("instance.name", "4");
    Factory fact3 = ipojoHelper.getFactory(sc, "composite.test.3");
    Properties props3 = new Properties();
    props3.put("instance.name", "5");
    ComponentInstance comp2 = null;
    ComponentInstance comp3 = null;
    try {
        comp2 = fact2.createComponentInstance(props2, sc);
        comp3 = fact3.createComponentInstance(props3, sc);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue("Test comp3", comp3.getState() == ComponentInstance.VALID);
    assertTrue("Test comp2", comp2.getState() == ComponentInstance.VALID);
    ServiceReference ref = null;
    ref = ipojoHelper.getServiceReferenceByName(sc, CheckService.class.getName(), "4");
    assertNotNull("Check ref", ref);
    CheckService cs = (CheckService) sc.getService(ref);
    assertTrue("Check invoke", cs.check());
    comp3.dispose();
    comp2.dispose();
    empty.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 2 with ServiceContext

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

the class TestCompositeAPI method testAPI.

@Test
public void testAPI() {
    Factory fact1 = ipojoHelper.getFactory("composite.empty");
    Properties p = new Properties();
    p.put("instance.name", "empty-1");
    ComponentInstance empty = null;
    try {
        empty = fact1.createComponentInstance(p);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ServiceContext sc = getServiceContext(empty);
    Factory fact2 = ipojoHelper.getFactory("composite.test.2");
    Properties props2 = new Properties();
    // 2
    props2.put("instance.name", "2");
    Factory fact3 = ipojoHelper.getFactory("composite.test.3");
    Properties props3 = new Properties();
    props3.put("instance.name", "3");
    ComponentInstance comp2 = null;
    ComponentInstance comp3 = null;
    try {
        comp2 = fact2.createComponentInstance(props2, sc);
        comp3 = fact3.createComponentInstance(props3, sc);
    } catch (Throwable e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    assertTrue("Test comp3", comp3.getState() == ComponentInstance.VALID);
    assertTrue("Test comp2", comp2.getState() == ComponentInstance.VALID);
    ServiceReference ref = null;
    // 2
    ref = ipojoHelper.getServiceReferenceByName(sc, CheckService.class.getName(), "2");
    assertNotNull("Check ref", ref);
    CheckService cs = (CheckService) sc.getService(ref);
    assertTrue("Check invoke", cs.check());
    comp3.dispose();
    comp2.dispose();
    empty.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with ServiceContext

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

the class CompositeInstanceDescription method getContainedInstances.

/**
 * Gets the list of contained instance in the described instance.
 * This list contains only instances who exposed their architecture.
 * @return the list of contained instances.
 */
public InstanceDescription[] getContainedInstances() {
    // Get instances description of internal instance
    ServiceContext internal = ((CompositeManager) m_instance).getServiceContext();
    try {
        ServiceReference[] refs = internal.getServiceReferences(Architecture.class.getName(), null);
        if (refs != null) {
            InstanceDescription[] desc = new InstanceDescription[refs.length];
            for (int i = 0; i < refs.length; i++) {
                Architecture arch = (Architecture) internal.getService(refs[i]);
                desc[i] = arch.getInstanceDescription();
                internal.ungetService(refs[i]);
            }
            return desc;
        }
    } catch (InvalidSyntaxException e) {
    // Cannot happen
    }
    return new InstanceDescription[0];
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) ServiceContext(org.apache.felix.ipojo.ServiceContext) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with ServiceContext

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

the class CompositeInstanceDescription method getInternalServices.

/**
 * Gets the list of internally published services.
 * @return the list of published services.
 */
public Element getInternalServices() {
    Element services = new Element("services", "");
    ServiceContext internal = ((CompositeManager) m_instance).getServiceContext();
    try {
        ServiceReference[] refs = internal.getServiceReferences((String) null, "(!(objectclass=" + Factory.class.getName() + "))");
        for (int i = 0; refs != null && i < refs.length; i++) {
            Element svc = new Element("service", "");
            String[] keys = refs[i].getPropertyKeys();
            for (int j = 0; j < keys.length; j++) {
                Object v = refs[i].getProperty(keys[j]);
                if (v instanceof String[]) {
                    List l = Arrays.asList((String[]) v);
                    svc.addAttribute(new Attribute(keys[j], l.toString()));
                } else {
                    svc.addAttribute(new Attribute(keys[j], v.toString()));
                }
            }
            services.addElement(svc);
        }
    } catch (InvalidSyntaxException e) {
    // Cannot happen
    }
    return services;
}
Also used : Attribute(org.apache.felix.ipojo.metadata.Attribute) ServiceContext(org.apache.felix.ipojo.ServiceContext) Element(org.apache.felix.ipojo.metadata.Element) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 5 with ServiceContext

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

the class TestDelayedFilteredImport method testSimple.

@Test
public void testSimple() {
    import1.start();
    // Two providers
    assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import1);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    foo1.stop();
    assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import1);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    // Stop the second provider
    foo2.stop();
    assertTrue("Test component invalidity - 2", import1.getState() == ComponentInstance.INVALID);
    foo2.start();
    assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import1);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceContext (org.apache.felix.ipojo.ServiceContext)62 Test (org.junit.Test)60 Properties (java.util.Properties)48 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)47 ServiceReference (org.osgi.framework.ServiceReference)35 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 Factory (org.apache.felix.ipojo.Factory)14 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)10 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)7 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)6 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 Architecture (org.apache.felix.ipojo.architecture.Architecture)2 List (java.util.List)1 ComponentTypeDescription (org.apache.felix.ipojo.architecture.ComponentTypeDescription)1 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)1 Attribute (org.apache.felix.ipojo.metadata.Attribute)1 Element (org.apache.felix.ipojo.metadata.Element)1 Ignore (org.junit.Ignore)1