Search in sources :

Example 31 with Component

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

the class AdapterWithExtraDependenciesTest method testAdapterWithExtraDependenciesAndCallbacksRef.

public void testAdapterWithExtraDependenciesAndCallbacksRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service adapter that adapts to services S1 and has an optional dependency on services S2
    Component sa = adapter(m, S1.class).impl(SA.class).withSvc(S2.class, s2 -> s2.add(SA::add).remove(SA::remove)).build();
    m.add(sa);
    // create a service S1, which triggers the creation of the first adapter instance (A1)
    Component s1 = component(m).provides(S1.class).impl(new S1Impl()).build();
    m.add(s1);
    // create a service S2, which will be added to A1
    Component s2 = component(m).provides(S2.class).impl(new S2Impl(e)).build();
    m.add(s2);
    // create a second service S1, which triggers the creation of the second adapter instance (A2)
    Component s1b = component(m).provides(S1.class).impl(new S1Impl()).build();
    m.add(s1b);
    // observe that S2 is also added to A2
    e.waitForStep(2, 5000);
    // remove S2 again
    m.remove(s2);
    // make sure both adapters have their "remove" callbacks invoked
    e.waitForStep(4, 5000);
    m.remove(s1);
    m.remove(sa);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.adapter(org.apache.felix.dm.lambda.DependencyManagerActivator.adapter) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 32 with Component

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

the class AdapterWithModifiedInstanceBoundDependencyTest method testAdapterWithChangedInstanceBoundDependency.

public void testAdapterWithChangedInstanceBoundDependency() {
    DependencyManager m = getDM();
    Ensure e = new Ensure();
    Component a = component(m).impl(new AImpl(e)).provides(A.class).properties(foo -> "bar").build();
    Component b = adapter(m, A.class).provides(B.class).impl(new BImpl(e)).add("addA").change("changeA").remove("removeA").build();
    Component c = component(m).impl(new CImpl()).provides(C.class).withSvc(A.class, "(foo=bar)", true).build();
    m.add(a);
    m.add(c);
    m.add(b);
    e.waitForStep(4, 5000);
    System.out.println("changing A props ...");
    Properties props = new Properties();
    props.put("foo", "bar2");
    a.setServiceProperties(props);
    e.waitForStep(7, 5000);
    m.remove(c);
    m.remove(a);
    m.remove(b);
    e.waitForStep(9, 5000);
}
Also used : Properties(java.util.Properties) Component(org.apache.felix.dm.Component) DependencyManagerActivator.adapter(org.apache.felix.dm.lambda.DependencyManagerActivator.adapter) Map(java.util.Map) 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) Properties(java.util.Properties)

Example 33 with Component

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

the class AdapterWithoutPropagationTest method testAdapterNoPropagate.

public void testAdapterNoPropagate() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // The provider has a "foo=bar" property
    ServiceProvider serviceProvider = new ServiceProvider(e);
    Component provider = component(m).provides(OriginalService.class).properties(foo -> "bar").impl(serviceProvider).build();
    // The Adapter will see the "foo=bar" property from the adaptee
    Component adapter = adapter(m, OriginalService.class).propagate(false).add("set").change("change").provides(AdaptedService.class).impl(new ServiceAdapter(e)).build();
    // The consumer depends on the AdaptedService, but won't see foo=bar property from the adaptee
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, b -> b.add("set").change("change")).build();
    // add the provider and the adapter
    m.add(provider);
    m.add(adapter);
    // Checks if the adapter has been started and has seen the adaptee properties
    e.waitForStep(1, 5000);
    // add a consumer that must not see the adaptee service properties
    m.add(consumer);
    e.waitForStep(2, 5000);
    // change the service properties of the provider, and check that the adapter callback instance is caled.
    serviceProvider.changeServiceProperties();
    e.waitForStep(3, 5000);
    // cleanup
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.adapter(org.apache.felix.dm.lambda.DependencyManagerActivator.adapter) 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) ServiceRegistration(org.osgi.framework.ServiceRegistration) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 34 with Component

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

the class AspectBaseTest method testMultipleAspectsRef.

public void testMultipleAspectsRef() {
    DependencyManager m = new DependencyManager(context);
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create service providers and consumers
    ServiceConsumer c = new ServiceConsumer(e);
    Component sp = component(m).impl(new ServiceProvider("a")).provides(ServiceInterface.class).properties(name -> "a").build();
    Component sp2 = component(m).impl(new ServiceProvider("b")).provides(ServiceInterface.class).properties(name -> "b").build();
    Component sc = component(m).impl(c).withSvc(ServiceInterface.class, srv -> srv.add(c::addRef).remove(c::removeRef)).build();
    Component sa = aspect(m, ServiceInterface.class).rank(20).impl(ServiceAspect.class).build();
    Component sa2 = aspect(m, ServiceInterface.class).rank(10).impl(ServiceAspect.class).build();
    m.add(sp);
    m.add(sp2);
    m.add(sa);
    m.add(sa2);
    m.add(sc);
    // the consumer will monitor progress, it should get it's add invoked twice, once for every
    // (highest) aspect
    e.waitForStep(2, 2000);
    e.step(3);
    // now invoke all services the consumer collected
    List<String> list = c.invokeAll();
    // and make sure both of them are correctly invoked
    Assert.assertTrue(list.size() == 2);
    Assert.assertTrue(list.contains("aaa"));
    Assert.assertTrue(list.contains("bbb"));
    m.remove(sc);
    // removing the consumer now should get its removed method invoked twice
    e.waitForStep(5, 2000);
    e.step(6);
    m.remove(sa2);
    m.remove(sa);
    m.remove(sp2);
    m.remove(sp);
    e.step(7);
}
Also used : List(java.util.List) Component(org.apache.felix.dm.Component) DependencyManagerActivator.aspect(org.apache.felix.dm.lambda.DependencyManagerActivator.aspect) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) ServiceReference(org.osgi.framework.ServiceReference) ArrayList(java.util.ArrayList) ServiceRegistration(org.osgi.framework.ServiceRegistration) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 35 with Component

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

the class AspectBaseTest method testSingleAspectRef.

public void testSingleAspectRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    ServiceProvider p = new ServiceProvider("a");
    ServiceConsumer c = new ServiceConsumer(e);
    Component sp = component(m).impl(p).provides(ServiceInterface.class).properties(name -> "a").build();
    Component sc = component(m).impl(c).withSvc(ServiceInterface.class, srv -> srv.add(c::addRef).remove(c::removeRef).autoConfig("m_service")).build();
    Component sa = aspect(m, ServiceInterface.class).rank(20).impl(ServiceAspect.class).build();
    m.add(sc);
    m.add(sp);
    // after the provider was added, the consumer's add should have been invoked once
    e.waitForStep(1, 2000);
    Assert.assertEquals("a", c.invoke());
    m.add(sa);
    // after the aspect was added, the consumer should get and add for the aspect and a remove
    // for the original service
    e.waitForStep(3, 2000);
    Assert.assertEquals("aa", c.invoke());
    m.remove(sa);
    // removing the aspect again should give a remove and add
    e.waitForStep(5, 2000);
    Assert.assertEquals("a", c.invoke());
    m.remove(sp);
    // finally removing the original service should give a remove
    e.waitForStep(6, 2000);
    m.remove(sc);
    e.step(7);
    clearComponents();
}
Also used : List(java.util.List) Component(org.apache.felix.dm.Component) DependencyManagerActivator.aspect(org.apache.felix.dm.lambda.DependencyManagerActivator.aspect) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) ServiceReference(org.osgi.framework.ServiceReference) ArrayList(java.util.ArrayList) ServiceRegistration(org.osgi.framework.ServiceRegistration) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Aggregations

Component (org.apache.felix.dm.Component)271 DependencyManager (org.apache.felix.dm.DependencyManager)227 Ensure (org.apache.felix.dm.itest.util.Ensure)91 DependencyManagerActivator.component (org.apache.felix.dm.lambda.DependencyManagerActivator.component)65 Hashtable (java.util.Hashtable)59 Assert (org.junit.Assert)46 Dictionary (java.util.Dictionary)32 ServiceReference (org.osgi.framework.ServiceReference)25 Map (java.util.Map)23 DependencyManagerActivator.aspect (org.apache.felix.dm.lambda.DependencyManagerActivator.aspect)21 Bundle (org.osgi.framework.Bundle)17 ServiceRegistration (org.osgi.framework.ServiceRegistration)17 DependencyManagerActivator.adapter (org.apache.felix.dm.lambda.DependencyManagerActivator.adapter)15 ArrayList (java.util.ArrayList)14 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)13 HashMap (java.util.HashMap)12 ServiceDependency (org.apache.felix.dm.ServiceDependency)12 Test (org.junit.Test)11 Properties (java.util.Properties)10 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)10