Search in sources :

Example 76 with Component

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

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

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

Example 79 with Component

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

the class AspectWithPropagationTest method testAdapterWithAspectsAndPropagationNoCallbacks.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects without any callbacks (add/change/remove)
 * - Create S2 Adapter, which adapts S to S2 (but does not have any add/change/remove callbacks)
 * - 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 testAdapterWithAspectsAndPropagationNoCallbacks() {
    System.out.println("----------- Running testAdapterWithAspectsAndPropagationNoCallbacks ...");
    DependencyManager m = getDM();
    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 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);
    }
    // Create S2 adapter (which adapts S1 to S2 interface)
    Component adapter = m.createAdapterService(S.class, null).setInterface(S2.class.getName(), null).setImplementation(new S2Impl());
    // Create Client2, which depends on "S2" service.
    Client2 client2Impl;
    Component client2 = m.createComponent().setImplementation((client2Impl = new Client2())).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    // 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();
    for (int i = 1; i <= ASPECTS + 1; i++) {
        // skip all aspects and the adapter
        m_changeStep.step(i);
    }
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if Client2 has been called in its "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 : 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)

Example 80 with Component

use of org.apache.felix.dm.Component 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 = m.createComponent().setImplementation(new SImpl()).setInterface(S.class.getName(), props);
    // 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, "add", "change", "remove", "swap").setImplementation(new A("A" + rank, rank));
        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 = m.createAdapterService(S.class, null, "add", "change", "remove", "swap").setInterface(S2.class.getName(), null).setImplementation(new S2Impl());
    // Create Client2, which depends on "S2" service.
    Client2 client2Impl;
    Component client2 = m.createComponent().setImplementation((client2Impl = new Client2())).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    // 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 : 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

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