Search in sources :

Example 21 with DependencyManager

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

the class AdapterNoAutoConfigIfInstanceCallbackIsUsed method testNoAutoConfigIfInstanceCallbackIsUsed.

public void testNoAutoConfigIfInstanceCallbackIsUsed() {
    m_e = new Ensure();
    DependencyManager m = getDM();
    // Declare S1 service
    component(m, c -> c.impl(S1Impl.class).provides(S1.class));
    // Declare S1 adapter
    S1AdapterCallback s1AdapterCB = new S1AdapterCallback();
    adapter(m, S1.class, a -> a.impl(S1Adapter.class).callbackInstance(s1AdapterCB).add("set"));
    // At this point, the s1AdapterCB.set(S1 s1) method should be called, and s1Adapter.start() method should then be called.
    // but s1 should not be injected on s1Adapter class fields.
    m_e.waitForStep(3, 5000);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager)

Example 22 with DependencyManager

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

the class AdapterWithCallbackInstanceTest method testServiceWithAdapterAndConsumer.

public void testServiceWithAdapterAndConsumer() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    ServiceProvider serviceProvider = new ServiceProvider(e);
    Component provider = component(m).provides(OriginalService.class).impl(serviceProvider).build();
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, true).build();
    ServiceAdapterCallbackInstance callbackInstance = new ServiceAdapterCallbackInstance(e);
    Component adapter = adapter(m, OriginalService.class).provides(AdaptedService.class).impl(new ServiceAdapter(e)).propagate(true).autoConfig("m_originalService").callbackInstance(callbackInstance).add("set").change("changed").remove("unset").build();
    // add the provider and the adapter
    m.add(provider);
    m.add(adapter);
    // Checks if the callbackInstances is called, and if the adapter start method is called
    e.waitForStep(2, 5000);
    // add a consumer that will invoke the adapter
    // which will in turn invoke the original provider
    m.add(consumer);
    // now validate that both have been invoked in the right order
    e.waitForStep(4, 5000);
    // change the service properties of the provider, and check that the adapter callback instance is changed.
    serviceProvider.changeServiceProperties();
    e.waitForStep(5, 5000);
    // remove the provider
    m.remove(provider);
    // ensure that the consumer is stopped, the adapter callback is called in its unset method, and the adapter is stopped.
    e.waitForStep(8, 5000);
    // remove adapter and consumer
    m.remove(adapter);
    m.remove(consumer);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 23 with DependencyManager

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

the class AdapterWithExtraDependenciesTest method testAdapterWithExtraDependenciesAndCallbacksRef.

public void testAdapterWithExtraDependenciesAndCallbacksRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service adapter that adapts to services S1 and has an optional dependency on services S2
    Component sa = adapter(m, S1.class).impl(SA.class).withSvc(S2.class, s2 -> s2.add(SA::add).remove(SA::remove)).build();
    m.add(sa);
    // create a service S1, which triggers the creation of the first adapter instance (A1)
    Component s1 = component(m).provides(S1.class).impl(new S1Impl()).build();
    m.add(s1);
    // create a service S2, which will be added to A1
    Component s2 = component(m).provides(S2.class).impl(new S2Impl(e)).build();
    m.add(s2);
    // create a second service S1, which triggers the creation of the second adapter instance (A2)
    Component s1b = component(m).provides(S1.class).impl(new S1Impl()).build();
    m.add(s1b);
    // observe that S2 is also added to A2
    e.waitForStep(2, 5000);
    // remove S2 again
    m.remove(s2);
    // make sure both adapters have their "remove" callbacks invoked
    e.waitForStep(4, 5000);
    m.remove(s1);
    m.remove(sa);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) DependencyManagerActivator.adapter(org.apache.felix.dm.lambda.DependencyManagerActivator.adapter) 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 24 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();
    Component a = component(m).impl(new AImpl(e)).provides(A.class).properties(foo -> "bar").build();
    Component b = adapter(m, A.class).provides(B.class).impl(new BImpl(e)).add("addA").change("changeA").remove("removeA").build();
    Component c = component(m).impl(new CImpl()).provides(C.class).withSvc(A.class, "(foo=bar)", true).build();
    m.add(a);
    m.add(c);
    m.add(b);
    e.waitForStep(4, 5000);
    System.out.println("changing A props ...");
    Properties props = new Properties();
    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 : Properties(java.util.Properties) Component(org.apache.felix.dm.Component) 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) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) Properties(java.util.Properties)

Example 25 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
    ServiceProvider serviceProvider = new ServiceProvider(e);
    Component provider = component(m).provides(OriginalService.class).properties(foo -> "bar").impl(serviceProvider).build();
    // The Adapter will see the "foo=bar" property from the adaptee
    Component adapter = adapter(m, OriginalService.class).propagate(false).add("set").change("change").provides(AdaptedService.class).impl(new ServiceAdapter(e)).build();
    // The consumer depends on the AdaptedService, but won't see foo=bar property from the adaptee
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, b -> b.add("set").change("change")).build();
    // 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 : Component(org.apache.felix.dm.Component) DependencyManagerActivator.adapter(org.apache.felix.dm.lambda.DependencyManagerActivator.adapter) 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) ServiceRegistration(org.osgi.framework.ServiceRegistration) 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