Search in sources :

Example 26 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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 27 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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)

Example 28 with DependencyManager

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

the class AspectBaseTest method testSingleAspect.

public void testSingleAspect() {
    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("add").remove("remove").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)

Example 29 with DependencyManager

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

the class AspectDynamicsTest method testDynamicallyAddAndRemoveAspect.

public void testDynamicallyAddAndRemoveAspect() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Ensure aspectStopEnsure = new Ensure();
    // create a service provider and consumer
    Component provider = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
    Component provider2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface2.class.getName()).build();
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add("add").swap("swap")).build();
    Component aspect = aspect(m, ServiceInterface.class).autoAdd(false).rank(1).impl(new ServiceAspect(e, aspectStopEnsure)).build();
    m.add(consumer);
    m.add(provider);
    // the consumer should invoke the provider here, and when done, arrive at step 3
    // finally wait for step 6 before continuing
    e.waitForStep(3, 15000);
    m.add(aspect);
    // after adding the aspect, we wait for its init to be invoked, arriving at
    // step 4 after an instance bound dependency was added (on a service provided by
    // provider 2)
    e.waitForStep(4, 15000);
    m.add(provider2);
    // after adding provider 2, we should now see the client being swapped, so
    // we wait for step 5 to happen
    e.waitForStep(5, 15000);
    // now we continue with step 6, which will trigger the next part of the consumer's
    // run method to be executed
    e.step(6);
    // invoking step 7, 8 and 9 when invoking the aspect which in turn invokes the
    // dependency and the original service, so we wait for that to finish here, which
    // is after step 10 has been reached (the client will now wait for step 12)
    e.waitForStep(10, 15000);
    m.remove(aspect);
    aspectStopEnsure.waitForStep(1, 15000);
    // removing the aspect should trigger step 11 (in the swap method of the consumer)
    e.waitForStep(11, 15000);
    // step 12 triggers the client to continue
    e.step(12);
    // wait for step 13, the final invocation of the provided service (without aspect)
    e.waitForStep(13, 15000);
    // clean up
    m.remove(provider2);
    m.remove(provider);
    m.remove(consumer);
    e.waitForStep(16, 15000);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.aspect(org.apache.felix.dm.lambda.DependencyManagerActivator.aspect) 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 30 with DependencyManager

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

the class AspectDynamicsTest method testDynamicallyAddAndRemoveAspectRef.

public void testDynamicallyAddAndRemoveAspectRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Ensure aspectStopEnsure = new Ensure();
    // create a service provider and consumer
    Component provider = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
    Component provider2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface2.class.getName()).build();
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add(ServiceConsumer::add).swap(ServiceConsumer::swap)).build();
    Component aspect = aspect(m, ServiceInterface.class).autoAdd(false).rank(1).impl(new ServiceAspect(e, aspectStopEnsure)).build();
    m.add(consumer);
    m.add(provider);
    // the consumer should invoke the provider here, and when done, arrive at step 3
    // finally wait for step 6 before continuing
    e.waitForStep(3, 15000);
    m.add(aspect);
    // after adding the aspect, we wait for its init to be invoked, arriving at
    // step 4 after an instance bound dependency was added (on a service provided by
    // provider 2)
    e.waitForStep(4, 15000);
    m.add(provider2);
    // after adding provider 2, we should now see the client being swapped, so
    // we wait for step 5 to happen
    e.waitForStep(5, 15000);
    // now we continue with step 6, which will trigger the next part of the consumer's
    // run method to be executed
    e.step(6);
    // invoking step 7, 8 and 9 when invoking the aspect which in turn invokes the
    // dependency and the original service, so we wait for that to finish here, which
    // is after step 10 has been reached (the client will now wait for step 12)
    e.waitForStep(10, 15000);
    m.remove(aspect);
    aspectStopEnsure.waitForStep(1, 15000);
    // removing the aspect should trigger step 11 (in the swap method of the consumer)
    e.waitForStep(11, 15000);
    // step 12 triggers the client to continue
    e.step(12);
    // wait for step 13, the final invocation of the provided service (without aspect)
    e.waitForStep(13, 15000);
    // clean up
    m.remove(provider2);
    m.remove(provider);
    m.remove(consumer);
    e.waitForStep(16, 15000);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.aspect(org.apache.felix.dm.lambda.DependencyManagerActivator.aspect) 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)

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