Search in sources :

Example 46 with DependencyManager

use of org.apache.felix.dm.DependencyManager in project felix by apache.

the class FactoryInjectedWithConfigurationBeforeTheCreateMethod method testServiceInjectionRef.

public void testServiceInjectionRef() {
    DependencyManager m = getDM();
    m_e = new Ensure();
    // Create the component that creates a configuration.
    Component configurator = component(m).impl(new Configurator("foobar")).withSvc(ConfigurationAdmin.class, true).build();
    // Create the object that has to be injected with the configuration before its create method is called.
    MyFactory factory = new MyFactory();
    // Create the Component for the MyComponent class that is created using the factory above.
    Component myComponent = component(m).factory(factory, "create").withCnf(b -> b.pid("foobar").update(factory::updated)).build();
    // provide the configuration
    m.add(configurator);
    m.add(myComponent);
    m_e.waitForStep(4, 10000);
    m.remove(myComponent);
    m.remove(configurator);
}
Also used : Component(org.apache.felix.dm.Component) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 47 with DependencyManager

use of org.apache.felix.dm.DependencyManager in project felix by apache.

the class InstanceBoundDependencyTest method testServiceInjection.

public void testServiceInjection() {
    DependencyManager m = getDM();
    m_e = new Ensure();
    // Create a "C" component: it depends on some S1 services, and on some S2 instance-bound services (declared from C.init() method)
    C cimpl = new C();
    Component c = component(m).impl(cimpl).withSvc(S1.class, sb -> sb.add("addS1").change("changeS1").remove("removeS1").autoConfig(true)).build();
    m.add(c);
    // Add S1 (s1_1): C.add(S1 s1) is called, then init() is called where a dependency is declared on S2
    Hashtable s1_1_props = new Hashtable();
    s1_1_props.put("name", "s1_1");
    s1_1_props.put(Constants.SERVICE_RANKING, new Integer(10));
    S1Impl s1_1_impl = new S1Impl();
    Component s1_1 = component(m).impl(s1_1_impl).provides(S1.class.getName(), s1_1_props).build();
    m.add(s1_1);
    // wait until C.init called
    m_e.waitForStep(1, 5000);
    ServiceReference ref = cimpl.getS1("s1_1");
    Assert.assertNotNull(ref);
    Assert.assertNotNull(cimpl.getS1());
    Assert.assertEquals(s1_1_impl, cimpl.getS1());
    // At this point, MyComponent is in INSTANTIATED_AND_WAITING_FOR_REQUIRED state.
    // add now add another higher ranked S1 (s1_2) instance. C.add(s1_2) method should be called (the S1 dependency
    // is not instance bound), and m_s1 autoconfig field should be updated.
    Hashtable s1_2_props = new Hashtable();
    s1_2_props.put(Constants.SERVICE_RANKING, new Integer(20));
    s1_2_props.put("name", "s1_2");
    S1Impl s1_2_impl = new S1Impl();
    Component s1_2 = component(m).impl(s1_2_impl).provides(S1.class.getName(), s1_2_props).build();
    m.add(s1_2);
    ref = cimpl.getS1("s1_2");
    Assert.assertNotNull(ref);
    Assert.assertNotNull(cimpl.getS1());
    // must return s1_2 with ranking = 20
    Assert.assertEquals(s1_2_impl, cimpl.getS1());
    // Now, change the s1_1 service properties: C.changed(s1_1) should be called, and C.m_s1AutoConfig should be updated
    s1_1_props.put(Constants.SERVICE_RANKING, new Integer(30));
    s1_1.setServiceProperties(s1_1_props);
    ref = cimpl.getS1("s1_1");
    Assert.assertNotNull(ref);
    Assert.assertEquals(new Integer(30), ref.getProperty(Constants.SERVICE_RANKING));
    Assert.assertNotNull(cimpl.getS1());
    Assert.assertEquals(s1_1_impl, cimpl.getS1());
    // Now, remove the s1_1: C.remove(s1_1) should be called, and C.m_s1AutoConfig should be updated
    m.remove(s1_1);
    ref = cimpl.getS1("s1_1");
    Assert.assertNull(cimpl.getS1("s1_1"));
    Assert.assertNotNull(cimpl.getS1());
    Assert.assertEquals(s1_2_impl, cimpl.getS1());
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) Constants(org.osgi.framework.Constants) Map(java.util.Map) HashMap(java.util.HashMap) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ServiceReference(org.osgi.framework.ServiceReference)

Example 48 with DependencyManager

use of org.apache.felix.dm.DependencyManager in project felix by apache.

the class ModifiedBundleDependencyTest method testAdapterWithChangedInstanceBoundDependencyRef.

public void testAdapterWithChangedInstanceBoundDependencyRef() {
    DependencyManager m = getDM();
    Ensure e = new Ensure();
    Component a = component(m, comp -> comp.impl(new AImpl()).provides(A.class).autoAdd(false));
    BImpl impl = new BImpl(e);
    String filter = "(Bundle-SymbolicName=org.apache.felix.metatype)";
    int mask = Bundle.INSTALLED | Bundle.ACTIVE | Bundle.RESOLVED | Bundle.STARTING;
    Component b = component(m).provides(B.class).impl(impl).withBundle(bd -> bd.filter(filter).mask(mask).add(impl::add).change(impl::change).remove(impl::remove)).build();
    Bundle dmtest = getBundle("org.apache.felix.metatype");
    try {
        dmtest.stop();
    } catch (BundleException e1) {
        Assert.fail("could not find metatype bundle");
    }
    m.add(a);
    m.add(b);
    e.waitForStep(4, 5000);
    // B will loose A and will enter into "waiting for required (instantiated)" state.
    m.remove(a);
    System.out.println("Starting metatype bundle ...");
    try {
        dmtest.start();
    } catch (BundleException e1) {
        Assert.fail("could not start metatype bundle");
    }
    e.waitForStep(7, 5000);
    m.remove(b);
    e.waitForStep(10, 5000);
}
Also used : Component(org.apache.felix.dm.Component) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) Bundle(org.osgi.framework.Bundle) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) BundleException(org.osgi.framework.BundleException) Bundle(org.osgi.framework.Bundle) DependencyManager(org.apache.felix.dm.DependencyManager) BundleException(org.osgi.framework.BundleException) Component(org.apache.felix.dm.Component)

Example 49 with DependencyManager

use of org.apache.felix.dm.DependencyManager in project felix by apache.

the class MultipleExtraDependenciesTest method testWithOneAvailableExtraDependency.

/**
 * Check that list of extra dependencies (defined from init method) are handled properly.
 * The extra dependencies are added using a List object (Component.add(List)).
 * A component c1 will define two extra dependencies over c4/c5. At the point c1.init()
 * is adding the two extra dependencies from its init method, c4 is available, but not c5.
 * So, c1 is not yet activated.
 * Then c5 is added, and it triggers the c1 activation ...
 */
public void testWithOneAvailableExtraDependency() {
    DependencyManager m = getDM();
    // Helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component c1 = component(m).provides(Service1.class).impl(new MyComponent1(e)).withSvc(Service2.class, srv -> srv.autoConfig("m_service2")).build();
    Component c2 = component(m).impl(new MyComponent2(e)).withSvc(Service1.class, srv -> srv.required(false).autoConfig(false).add("added")).build();
    Component c3 = component(m).provides(Service2.class).impl(Service2Impl.class).build();
    Component c4 = component(m).impl(Service3Impl1.class).provides(Service3.class, type -> "xx").build();
    Component c5 = component(m).impl(Service3Impl2.class).provides(Service3.class, type -> "yy").build();
    System.out.println("\n+++ Adding c2 / MyComponent2");
    m.add(c2);
    System.out.println("\n+++ Adding c3 / Service2");
    m.add(c3);
    System.out.println("\n+++ Adding c4 / Service3(xx)");
    m.add(c4);
    System.out.println("\n+++ Adding c1 / MyComponent1");
    m.add(c1);
    // c1 have declared two extra dependency on Service3 (xx/yy).
    // So, because we have not yet added c5 (yy), c1 should not be started currently.
    // But, now, we'll add c5 (Service3/yy) and c1 should then be started ...
    System.out.println("\n+++ Adding c5 / Service3(yy)");
    m.add(c5);
    e.waitForStep(3, 3000);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 50 with DependencyManager

use of org.apache.felix.dm.DependencyManager in project felix by apache.

the class MultipleExtraDependenciesTest method testWithTwoAvailableExtraDependency.

/**
 * Check that list of extra dependencies (defined from init method) are handled properly.
 * The extra dependencies are added using a List object (Component.add(List)).
 * A component c1 will define two extra dependencies over *available* c4/c5 services.
 */
public void testWithTwoAvailableExtraDependency() {
    DependencyManager m = getDM();
    // Helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component c1 = component(m).provides(Service1.class).impl(new MyComponent1(e)).withSvc(Service2.class, srv -> srv.autoConfig("m_service2")).build();
    Component c2 = component(m).impl(new MyComponent2(e)).withSvc(Service1.class, srv -> srv.required(false).autoConfig(false).add("added")).build();
    Component c3 = component(m).provides(Service2.class).impl(Service2Impl.class).build();
    Component c4 = component(m).impl(Service3Impl1.class).provides(Service3.class, type -> "xx").build();
    Component c5 = component(m).impl(Service3Impl2.class).provides(Service3.class, type -> "yy").build();
    System.out.println("\n+++ Adding c2 / MyComponent2");
    m.add(c2);
    System.out.println("\n+++ Adding c3 / Service2");
    m.add(c3);
    System.out.println("\n+++ Adding c4 / Service3(xx)");
    m.add(c4);
    System.out.println("\n+++ Adding c5 / Service3(yy)");
    m.add(c5);
    System.out.println("\n+++ Adding c1 / MyComponent1");
    // c1 have declared two extra dependency on Service3 (xx/yy).
    // both extra dependencies are available, so the c1 component should be started immediately.
    m.add(c1);
    e.waitForStep(3, 3000);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Aggregations

DependencyManager (org.apache.felix.dm.DependencyManager)255 Component (org.apache.felix.dm.Component)226 Ensure (org.apache.felix.dm.itest.util.Ensure)91 DependencyManagerActivator.component (org.apache.felix.dm.lambda.DependencyManagerActivator.component)70 Hashtable (java.util.Hashtable)56 Assert (org.junit.Assert)50 Dictionary (java.util.Dictionary)28 Map (java.util.Map)27 ServiceReference (org.osgi.framework.ServiceReference)24 DependencyManagerActivator.aspect (org.apache.felix.dm.lambda.DependencyManagerActivator.aspect)21 ServiceRegistration (org.osgi.framework.ServiceRegistration)18 DependencyManagerActivator.adapter (org.apache.felix.dm.lambda.DependencyManagerActivator.adapter)15 Bundle (org.osgi.framework.Bundle)15 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)12 ServiceDependency (org.apache.felix.dm.ServiceDependency)11 Properties (java.util.Properties)10 List (java.util.List)9 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)9