Search in sources :

Example 31 with DependencyManager

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

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

Example 33 with DependencyManager

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

the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumption.

public void testServiceRegistrationAndConsumption() {
    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("add").remove("remove")).build();
    Component asp = aspect(m, ServiceInterface.class).rank(100).add("add").remove("remove").swap("swap").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(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)

Example 34 with DependencyManager

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

the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumptionWithAspectCallbackInstance.

public void testServiceRegistrationAndConsumptionWithAspectCallbackInstance() {
    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).build();
    Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add("add").remove("remove")).build();
    ServiceProviderAspect providerAspect = new ServiceProviderAspect();
    ServiceProviderAspectCallbackInstance aspectCb = new ServiceProviderAspectCallbackInstance(providerAspect);
    Component asp = aspect(m, ServiceInterface.class).rank(100).impl(providerAspect).add(aspectCb::add).remove(aspectCb::remove).swap(aspectCb::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)

Example 35 with DependencyManager

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

the class AspectWithPropagationTest method testAdapterWithAspectsAndPropagation.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects
 * - Create S2 Adapter, which adapts S to S2
 * - Create Client2, which depends on S2. Client2 listens to S2 property change events.
 * - Now, invoke Client2.invoke(): all S aspects, and finally original S service must be invoked orderly.
 * - Modify S original service properties, and check if all aspects, S2 Adapter, and Client2 have been orderly called in their "change" callback.
 */
public void testAdapterWithAspectsAndPropagation() {
    System.out.println("----------- Running testAdapterWithAspectsAndPropagation ...");
    DependencyManager m = getDM();
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component s = component(m).impl(new SImpl()).provides(S.class, props).build();
    // Create some "S" aspects
    Component[] aspects = new Component[ASPECTS];
    for (int rank = 1; rank <= ASPECTS; rank++) {
        aspects[rank - 1] = aspect(m, S.class).rank(rank).impl(new A("A" + rank, rank)).add("add").change("change").remove("remove").swap("swap").build();
        props = new Hashtable();
        props.put("a" + rank, "v" + rank);
        aspects[rank - 1].setServiceProperties(props);
    }
    // Create S2 adapter (which adapts S1 to S2 interface)
    Component adapter = adapter(m, S.class).add("add").change("change").remove("remove").swap("swap").provides(S2.class).impl(new S2Impl()).build();
    // Create Client2, which depends on "S2" service.
    Client2 client2Impl;
    Component client2 = component(m).impl((client2Impl = new Client2())).withSvc(S2.class, srv -> srv.add("add").change("change")).build();
    // Register client2
    m.add(client2);
    // Register S2 adapter
    m.add(adapter);
    // Randomly register aspects, original service
    boolean originalServiceAdded = false;
    for (int i = 0; i < ASPECTS; i++) {
        int index = getRandomAspect();
        m.add(aspects[index]);
        if (!originalServiceAdded && _rnd.nextBoolean()) {
            m.add(s);
            originalServiceAdded = true;
        }
    }
    if (!originalServiceAdded) {
        m.add(s);
    }
    // Now invoke client2, which orderly calls all S1 aspects, then S1Impl, and finally S2 service
    System.out.println("-------------------------- Invoking client2.");
    client2Impl.invoke2();
    m_invokeStep.waitForStep(ASPECTS + 2, 5000);
    // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, S2Impl, and Client2.
    System.out.println("-------------------------- Modifying original service properties.");
    m_changeStep = new Ensure();
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if aspects and Client2 have been orderly called in their "changed" callback
    m_changeStep.waitForStep(ASPECTS + 2, 5000);
    // Check if modified "foo" original service property has been propagated to Client2
    Map check = new HashMap();
    check.put("foo", "barModified");
    for (int i = 1; i < (ASPECTS - 1); i++) {
        // we must not inherit from lower ranks, only from the top-level aspect.
        check.put("a" + i, null);
    }
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, client2Impl.getServiceProperties());
    // Clear all components.
    m_changeStep = null;
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.aspect(org.apache.felix.dm.lambda.DependencyManagerActivator.aspect) Set(java.util.Set) HashMap(java.util.HashMap) Random(java.util.Random) HashSet(java.util.HashSet) 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) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration) Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) HashMap(java.util.HashMap) Map(java.util.Map)

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