Search in sources :

Example 26 with Factory

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

the class Arch method factories.

/**
 * Displays the list of public iPOJO factories.
 */
@Descriptor("Display iPOJO factories")
public void factories() {
    StringBuilder buffer = new StringBuilder();
    for (Factory m_factory : m_factories) {
        if (m_factory.getMissingHandlers().size() == 0) {
            buffer.append(format("Factory %s (VALID)%n", m_factory.getName()));
        } else {
            buffer.append(format("Factory %s (INVALID: %s)%n", m_factory.getName(), m_factory.getMissingHandlers()));
        }
    }
    for (TypeDeclaration type : m_types) {
        if (!type.isPublic()) {
            // give access to the underlying Factory or description (if valid)
            if (type.getStatus().isBound()) {
                buffer.append(format("Factory %s (UNKNOWN) - Private%n", type.getComponentName()));
            } else {
                // Unbound type means that required extension is not available
                // We'll say that the factory is INVALID even if in reality it's not even instantiated
                buffer.append(format("Factory %s (INVALID) - Private%n", type.getComponentName()));
                buffer.append(format("  -> %s", type.getStatus().getMessage()));
            }
        } else {
            if (!type.getStatus().isBound()) {
                buffer.append(format("Factory %s is not bound%n", type.getComponentName()));
                buffer.append(format("  -> %s%n", type.getStatus().getMessage()));
            }
        }
    }
    if (buffer.length() == 0) {
        buffer.append("No factories \n");
    }
    System.out.println(buffer.toString());
}
Also used : Factory(org.apache.felix.ipojo.Factory) HandlerFactory(org.apache.felix.ipojo.HandlerFactory) TypeDeclaration(org.apache.felix.ipojo.extender.TypeDeclaration) Descriptor(org.apache.felix.service.command.Descriptor)

Example 27 with Factory

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

the class TestCompositeAPI method testInstantiator.

@Test
public void testInstantiator() {
    String type = "composite.instantiator";
    Factory fact = ipojoHelper.getFactory(type);
    ComponentInstance ci = null;
    Properties p = new Properties();
    p.put("instance.name", "mon_coeur");
    try {
        ci = fact.createComponentInstance(p);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue("Check ci", ci.getState() == ComponentInstance.VALID);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(BazService.class.getName(), "mon_coeur");
    assertNotNull("Check ref", ref);
    BazService bs = (BazService) getContext().getService(ref);
    assertTrue("Check invocation", bs.foo());
    getContext().ungetService(ref);
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "mon_coeur");
    assertNotNull("Check ref 2 ", ref);
    FooService fs = (FooService) getContext().getService(ref);
    assertTrue("Check invocation", fs.foo());
    getContext().ungetService(ref);
    ci.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) BazService(org.apache.felix.ipojo.runtime.core.services.BazService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 28 with Factory

use of org.apache.felix.ipojo.Factory 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 29 with Factory

use of org.apache.felix.ipojo.Factory 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 30 with Factory

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

the class TestComp2 method setUp.

@Before
public void setUp() {
    tataFactory = (ComponentFactory) ipojoHelper.getFactory("tata");
    totoFactory = (ComponentFactory) ipojoHelper.getFactory("toto");
    tataFactory2 = (ComponentFactory) ipojoHelper.getFactory("comp-6");
    tataFactory2.stop();
    tataFactory.stop();
    Properties props = new Properties();
    props.put("instance.name", "toto provider");
    try {
        totoProv = totoFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties props3 = new Properties();
    props3.put("instance.name", "toto provider 2");
    try {
        totoProv2 = totoFactory.createComponentInstance(props3);
    } catch (Exception e) {
        e.printStackTrace();
    }
    totoProv.stop();
    totoProv2.stop();
    Factory factory = ipojoHelper.getFactory("comp-2");
    Properties props2 = new Properties();
    props2.put("instance.name", "ff");
    try {
        under = factory.createComponentInstance(props2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

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