use of org.apache.felix.dm.Component 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();
}
use of org.apache.felix.dm.Component 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);
}
use of org.apache.felix.dm.Component 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();
}
use of org.apache.felix.dm.Component in project felix by apache.
the class AspectBaseTest method testMultipleAspectsRef.
public void testMultipleAspectsRef() {
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 = component(m).impl(new ServiceProvider("a")).provides(ServiceInterface.class).properties(name -> "a").build();
Component sp2 = component(m).impl(new ServiceProvider("b")).provides(ServiceInterface.class).properties(name -> "b").build();
Component sc = component(m).impl(c).withSvc(ServiceInterface.class, srv -> srv.add(c::addRef).remove(c::removeRef)).build();
Component sa = aspect(m, ServiceInterface.class).rank(20).impl(ServiceAspect.class).build();
Component sa2 = aspect(m, ServiceInterface.class).rank(10).impl(ServiceAspect.class).build();
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);
}
use of org.apache.felix.dm.Component in project felix by apache.
the class AspectBaseTest method testSingleAspectRef.
public void testSingleAspectRef() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
ServiceProvider p = new ServiceProvider("a");
ServiceConsumer c = new ServiceConsumer(e);
Component sp = component(m).impl(p).provides(ServiceInterface.class).properties(name -> "a").build();
Component sc = component(m).impl(c).withSvc(ServiceInterface.class, srv -> srv.add(c::addRef).remove(c::removeRef).autoConfig("m_service")).build();
Component sa = aspect(m, ServiceInterface.class).rank(20).impl(ServiceAspect.class).build();
m.add(sc);
m.add(sp);
// after the provider was added, the consumer's add should have been invoked once
e.waitForStep(1, 2000);
Assert.assertEquals("a", c.invoke());
m.add(sa);
// after the aspect was added, the consumer should get and add for the aspect and a remove
// for the original service
e.waitForStep(3, 2000);
Assert.assertEquals("aa", c.invoke());
m.remove(sa);
// removing the aspect again should give a remove and add
e.waitForStep(5, 2000);
Assert.assertEquals("a", c.invoke());
m.remove(sp);
// finally removing the original service should give a remove
e.waitForStep(6, 2000);
m.remove(sc);
e.step(7);
clearComponents();
}
Aggregations