Search in sources :

Example 41 with Component

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

Example 44 with Component

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

the class AutoConfigTest method testMapField.

public void testMapField() throws Exception {
    final DependencyManager dm = getDM();
    ConsumerWithMapField consumer = new ConsumerWithMapField();
    Component c = createConsumer(dm, consumer);
    Component p1 = createProvider(dm, 10, new Provider() {

        public void run() {
            m_ensure.step();
        }

        public String toString() {
            return "provider1";
        }
    });
    Component p2 = createProvider(dm, 20, new Provider() {

        public void run() {
            m_ensure.step();
        }

        public String toString() {
            return "provider2";
        }
    });
    dm.add(p2);
    dm.add(p1);
    dm.add(c);
    // the consumer should have been injected with all providers.
    m_ensure.waitForStep(3, 5000);
    // check if all providers are there
    Assert.assertNotNull(consumer.getProvider("provider1"));
    Assert.assertNotNull(consumer.getProvider("provider2"));
    // remove provider1
    dm.remove(p1);
    // check if provider1 has been removed and if provider2 is still there
    Assert.assertNull(consumer.getProvider("provider1"));
    Assert.assertNotNull(consumer.getProvider("provider2"));
    // remove provider2, the consumer should be stopped
    dm.remove(p2);
    m_ensure.waitForStep(4, 5000);
    dm.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 45 with Component

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

the class BundleAdapterTest method testBundleAdapterWithCallbackInstance.

public void testBundleAdapterWithCallbackInstance() {
    DependencyManager m = getDM();
    // create a bundle adapter service (one is created for each bundle)
    BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback();
    BundleAdapterCallbackInstance cbInstance = new BundleAdapterCallbackInstance(baWithCb);
    Component adapter = bundleAdapter(m).mask(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE).callbackInstance(cbInstance).add("add").remove("remove").impl(baWithCb).provides(BundleAdapter.class.getName()).build();
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = component(m).impl(c).withSvc(BundleAdapter.class, s -> s.add("add").remove("remove")).build();
    // add the bundle adapter
    m.add(adapter);
    // add the service consumer
    m.add(consumer);
    // check if at least one bundle was found
    c.check();
    // remove the consumer again
    m.remove(consumer);
    // check if all bundles were removed correctly
    c.doubleCheck();
    // remove the bundle adapter
    m.remove(adapter);
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.bundleAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.bundleAdapter) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) Bundle(org.osgi.framework.Bundle) 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