Search in sources :

Example 66 with DependencyManager

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

the class AspectChainTest method testBuildAspectChain.

public void testBuildAspectChain() {
    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 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true));
    Component sa2 = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(new ServiceAspect(e, 3));
    Component sa3 = m.createAspectService(ServiceInterface.class, null, 30, null).setImplementation(new ServiceAspect(e, 2));
    Component sa1 = m.createAspectService(ServiceInterface.class, null, 10, null).setImplementation(new ServiceAspect(e, 4));
    m.add(sc);
    m.add(sp);
    m.add(sa2);
    m.add(sa3);
    m.add(sa1);
    e.step();
    e.waitForStep(5, 5000);
    m.remove(sa3);
    m.remove(sa2);
    m.remove(sa1);
    m.remove(sp);
    m.remove(sc);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 67 with DependencyManager

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

the class AspectServiceDependencyTest 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 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setCallbacks("add", "remove").setRequired(true));
    Component asp = m.createAspectService(ServiceInterface.class, null, 100).setImplementation(ServiceProviderAspect.class);
    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 : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 68 with DependencyManager

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

the class AspectServiceDependencyWithSwapCallbackTest 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 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setCallbacks("add", null, "remove", "swap").setRequired(true));
    Component asp = m.createAspectService(ServiceInterface.class, null, 100).setImplementation(ServiceProviderAspect.class);
    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 : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 69 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 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setCallbacks("add", "remove").setRequired(true));
    ServiceProviderAspect providerAspect = new ServiceProviderAspect();
    ServiceProviderAspectCallbackInstance aspectCb = new ServiceProviderAspectCallbackInstance(providerAspect);
    Component asp = m.createAspectService(ServiceInterface.class, null, 100, aspectCb, "add", null, "remove", "swap").setImplementation(providerAspect);
    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 : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 70 with DependencyManager

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

the class AspectWithPropagationTest method testAspectsWithPropagationAndNoCallbacks.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects without any callbacks (add/change/remove/swap)
 * - Create a Client, depending on S (actually, on the top-level S aspect)
 * - Client has a "change" callack in order to track S service properties modifications.
 * - First, invoke Client.invoke(): all S aspects, and finally original S service must be invoked orderly.
 * - Modify S original service properties, and check if the client has been called in its "change" callback.
 */
public void testAspectsWithPropagationAndNoCallbacks() {
    System.out.println("----------- Running testAspectsWithPropagation ...");
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component s = m.createComponent().setImplementation(new SImpl()).setInterface(S.class.getName(), props);
    // Create an aspect aware client, depending on "S" service.
    Client clientImpl;
    Component client = m.createComponent().setImplementation((clientImpl = new Client())).add(m.createServiceDependency().setService(S.class).setRequired(true).setCallbacks("add", "change", "remove"));
    // Create some "S" aspects
    Component[] aspects = new Component[ASPECTS];
    for (int rank = 1; rank <= ASPECTS; rank++) {
        aspects[rank - 1] = m.createAspectService(S.class, null, rank).setImplementation(new A("A" + rank, rank));
        props = new Hashtable();
        props.put("a" + rank, "v" + rank);
        aspects[rank - 1].setServiceProperties(props);
    }
    // Register client
    m.add(client);
    // Randomly register aspects and 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);
    }
    // All set, check if client has inherited from top level aspect properties + original service properties
    Map check = new HashMap();
    check.put("foo", "bar");
    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, clientImpl.getServiceProperties());
    // Now invoke client, which orderly calls all aspects in the chain, and finally the original service "S".
    System.out.println("-------------------------- Invoking client.");
    clientImpl.invoke();
    m_invokeStep.waitForStep(ASPECTS + 1, 5000);
    // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, and on client.
    System.out.println("-------------------------- Modifying original service properties.");
    m_changeStep = new Ensure();
    for (int i = 1; i <= ASPECTS; i++) {
        // skip aspects, which have no "change" callbacks.
        m_changeStep.step(i);
    }
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if aspects and client have been orderly called in their "changed" callback
    m_changeStep.waitForStep(ASPECTS + 1, 5000);
    // Check if modified "foo" original service property has been propagated
    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);
    }
    // we only see top-level aspect service properties
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, clientImpl.getServiceProperties());
    // Clear all components.
    m_changeStep = null;
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) 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