Search in sources :

Example 71 with DependencyManager

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

Example 73 with DependencyManager

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

the class AutoConfigTest method testField.

public void testField() throws Exception {
    final DependencyManager dm = getDM();
    // Create a consumer, depending on some providers (autoconfig field).
    ConsumeWithProviderField consumer = new ConsumeWithProviderField();
    Component c = createConsumer(dm, consumer);
    // Create two providers
    Component p1 = createProvider(dm, 10, new Provider() {

        public String toString() {
            return "provider1";
        }

        public void run() {
            m_ensure.step();
        }
    });
    Component p2 = createProvider(dm, 20, new Provider() {

        public String toString() {
            return "provider2";
        }

        public void run() {
            m_ensure.step();
        }
    });
    // add the two providers
    dm.add(p2);
    dm.add(p1);
    // add the consumer, which should have been injected with provider2 (highest rank)
    dm.add(c);
    m_ensure.waitForStep(1, 5000);
    // remove the provider2, the consumer should now be injected with provider1
    dm.remove(p2);
    Assert.assertNotNull(consumer.getProvider());
    Assert.assertEquals("provider1", consumer.getProvider().toString());
    // remove the provider1, the consumer should have been stopped
    dm.remove(p1);
    m_ensure.waitForStep(2, 5000);
    dm.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 74 with DependencyManager

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

the class BundleAdapterTest method testBundleAdapter.

public void testBundleAdapter() {
    DependencyManager m = getDM();
    // create a bundle adapter service (one is created for each bundle)
    Component adapter = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false).setImplementation(BundleAdapter.class).setInterface(BundleAdapter.class.getName(), null);
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove"));
    // 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 : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 75 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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 = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false, cbInstance, "add", null, "remove").setImplementation(baWithCb).setInterface(BundleAdapter.class.getName(), null);
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove"));
    // 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 : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

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