Search in sources :

Example 36 with Component

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

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

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

Example 39 with Component

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

the class AspectServiceDependencyWithSwapCallbackTest method testServiceRegistrationAndConsumptionRef.

public void testServiceRegistrationAndConsumptionRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    ServiceConsumer scimpl = new ServiceConsumer(e);
    Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
    Component sc = component(m).impl(scimpl).withSvc(ServiceInterface.class, s -> s.add(scimpl::add).remove(scimpl::remove).swap(scimpl::swap)).build();
    Component asp = aspect(m, ServiceInterface.class).rank(100).impl(ServiceProviderAspect.class).build();
    m.add(sp);
    m.add(sc);
    m.add(asp);
    m.remove(asp);
    m.remove(sc);
    m.remove(sp);
    // ensure we executed all steps inside the component instance
    e.step(7);
}
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 40 with Component

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

the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumptionRef.

public void testServiceRegistrationAndConsumptionRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
    Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add(ServiceConsumer::add).remove(ServiceConsumer::remove)).build();
    Component asp = aspect(m, ServiceInterface.class).impl(ServiceProviderAspect.class).rank(100).add(ServiceProviderAspect::add).remove(ServiceProviderAspect::remove).swap(ServiceProviderAspect::swap).build();
    m.add(sp);
    m.add(sc);
    m.add(asp);
    m.remove(asp);
    m.remove(sc);
    m.remove(sp);
    // ensure we executed all steps inside the component instance
    e.step(8);
}
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

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