Search in sources :

Example 61 with DependencyManager

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

the class AdapterWithModifiedInstanceBoundDependencyTest method testAdapterWithChangedInstanceBoundDependency.

public void testAdapterWithChangedInstanceBoundDependency() {
    DependencyManager m = getDM();
    Ensure e = new Ensure();
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component a = m.createComponent().setImplementation(new AImpl(e)).setInterface(A.class.getName(), props);
    Component b = m.createAdapterService(A.class, null, "add", "change", "remove").setInterface(B.class.getName(), null).setImplementation(new BImpl(e));
    Component c = m.createComponent().setImplementation(new CImpl()).setInterface(C.class.getName(), null).add(m.createServiceDependency().setService(A.class, "(foo=bar)").setRequired(true));
    m.add(a);
    m.add(c);
    m.add(b);
    e.waitForStep(4, 5000);
    System.out.println("changing A props ...");
    props = new Hashtable();
    props.put("foo", "bar2");
    a.setServiceProperties(props);
    e.waitForStep(7, 5000);
    m.remove(c);
    m.remove(a);
    m.remove(b);
    e.waitForStep(9, 5000);
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 62 with DependencyManager

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

the class AdapterWithPropagationTest method testAdapterWithPropagation.

public void testAdapterWithPropagation() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Dictionary s1Properties = new Hashtable();
    s1Properties.put("p1", "v1");
    // should not override adapter
    s1Properties.put("p2", "v2overriden");
    Component s1 = m.createComponent().setImplementation(new S1Impl(e)).setInterface(S1.class.getName(), s1Properties);
    Dictionary s1AdapterProperties = new Hashtable();
    s1AdapterProperties.put("p2", "v2");
    Component s1Adapter = m.createAdapterService(S1.class, null, "add", "change", null).setInterface(S2.class.getName(), s1AdapterProperties).setImplementation(new S1Adapter(e));
    Component s3 = m.createComponent().setImplementation(new S3(e)).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    m.add(s1);
    m.add(s1Adapter);
    m.add(s3);
    e.waitForStep(3, 5000);
    s1Properties = new Hashtable();
    s1Properties.put("p1", "v1modified");
    s1Properties.put("p2", "v2overriden");
    s1.setServiceProperties(s1Properties);
    e.waitForStep(5, 5000);
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 63 with DependencyManager

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

the class AdapterWithoutPropagationTest method testAdapterNoPropagate.

public void testAdapterNoPropagate() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // The provider has a "foo=bar" property
    Hashtable<String, String> props = new Hashtable<>();
    props.put("foo", "bar");
    ServiceProvider serviceProvider = new ServiceProvider(e);
    Component provider = m.createComponent().setInterface(OriginalService.class.getName(), props).setImplementation(serviceProvider);
    // The Adapter will see the "foo=bar" property from the adaptee
    Component adapter = m.createAdapterService(OriginalService.class, null, null, null, "set", "change", null, null, false).setInterface(AdaptedService.class.getName(), null).setImplementation(new ServiceAdapter(e));
    // The consumer depends on the AdaptedService, but won't see foo=bar property from the adaptee
    Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(AdaptedService.class).setRequired(true).setCallbacks("set", "change", null));
    // add the provider and the adapter
    m.add(provider);
    m.add(adapter);
    // Checks if the adapter has been started and has seen the adaptee properties
    e.waitForStep(1, 5000);
    // add a consumer that must not see the adaptee service properties
    m.add(consumer);
    e.waitForStep(2, 5000);
    // change the service properties of the provider, and check that the adapter callback instance is caled.
    serviceProvider.changeServiceProperties();
    e.waitForStep(3, 5000);
    // cleanup
    m.clear();
}
Also used : Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 64 with DependencyManager

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

the class AspectBaseTest method testSingleAspectThatAlreadyExisted.

@SuppressWarnings("serial")
public void testSingleAspectThatAlreadyExisted() {
    DependencyManager m = new DependencyManager(context);
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    ServiceProvider p = new ServiceProvider(e, "a");
    ServiceConsumer c = new ServiceConsumer(e);
    Component sp = m.createComponent().setImplementation(p).setInterface(ServiceInterface.class.getName(), new Hashtable() {

        {
            put("name", "a");
        }
    });
    Component sc = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", "remove").setAutoConfig("m_service"));
    Component sa = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(ServiceAspect.class);
    // we first add the aspect
    m.add(sa);
    // then the service provider
    m.add(sp);
    // finally the consumer
    m.add(sc);
    Assert.assertEquals("aa", c.invoke());
    // now the consumer's added should be invoked once, as the aspect is already available and should
    // directly hide the original service
    e.waitForStep(1, 2000);
    e.step(2);
    m.remove(sa);
    // after removing the aspect, the consumer should get the original service back, so
    // remove and add will be invoked
    e.waitForStep(4, 2000);
    Assert.assertEquals("a", c.invoke());
    m.remove(sp);
    // after removing the original service, the consumer's remove should be called once
    e.waitForStep(5, 2000);
    m.remove(sc);
    e.step(6);
}
Also used : Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 65 with DependencyManager

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

the class AspectBaseTest method testMultipleAspects.

@SuppressWarnings("serial")
public void testMultipleAspects() {
    DependencyManager m = new DependencyManager(context);
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create service providers and consumers
    ServiceConsumer c = new ServiceConsumer(e);
    Component sp = m.createComponent().setImplementation(new ServiceProvider(e, "a")).setInterface(ServiceInterface.class.getName(), new Hashtable() {

        {
            put("name", "a");
        }
    });
    Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e, "b")).setInterface(ServiceInterface.class.getName(), new Hashtable() {

        {
            put("name", "b");
        }
    });
    Component sc = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", "remove"));
    Component sa = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(ServiceAspect.class);
    Component sa2 = m.createAspectService(ServiceInterface.class, null, 10, null).setImplementation(ServiceAspect.class);
    m.add(sp);
    m.add(sp2);
    m.add(sa);
    m.add(sa2);
    m.add(sc);
    // the consumer will monitor progress, it should get it's add invoked twice, once for every
    // (highest) aspect
    e.waitForStep(2, 2000);
    e.step(3);
    // now invoke all services the consumer collected
    List<String> list = c.invokeAll();
    // and make sure both of them are correctly invoked
    Assert.assertTrue(list.size() == 2);
    Assert.assertTrue(list.contains("aaa"));
    Assert.assertTrue(list.contains("bbb"));
    m.remove(sc);
    // removing the consumer now should get its removed method invoked twice
    e.waitForStep(5, 2000);
    e.step(6);
    m.remove(sa2);
    m.remove(sa);
    m.remove(sp2);
    m.remove(sp);
    e.step(7);
}
Also used : Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) 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