use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class DynamicProxyAspectTest method testImplementGenericAspectWithDynamicProxyAndFactoryRef.
public void testImplementGenericAspectWithDynamicProxyAndFactoryRef() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
DynamicProxyHandler.resetCounter();
// create two service providers, each providing a different service interface
Component sp1 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
Component sp2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface2.class).build();
// create a dynamic proxy based aspect and hook it up to both services
Component a1 = aspect(m, ServiceInterface.class).rank(10).autoConfig("m_service").factory(() -> new Factory(e, ServiceInterface.class, "ServiceInterfaceProxy"), Factory::create).build();
Component a2 = aspect(m, ServiceInterface2.class).rank(10).autoConfig("m_service").factory(() -> new Factory(e, ServiceInterface2.class, "ServiceInterfaceProxy2"), Factory::create).build();
// create a client that invokes a method on boths services, validate that it goes
// through the proxy twice
Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(true, ServiceInterface.class, ServiceInterface2.class).build();
// register both producers, validate that both services are started
m.add(sp1);
e.waitForStep(1, 2000);
m.add(sp2);
e.waitForStep(2, 2000);
// add both aspects, and validate that both instances have been created
m.add(a1);
m.add(a2);
e.waitForStep(4, 4000);
// add the client, which will automatically invoke both services
m.add(sc);
// wait until both services have been invoked
e.waitForStep(6, 4000);
// make sure the proxy has been called twice
Assert.assertEquals("Proxy should have been invoked this many times.", 2, DynamicProxyHandler.getCounter());
m.remove(sc);
m.remove(a2);
m.remove(a1);
m.remove(sp2);
m.remove(sp1);
m.remove(a2);
m.remove(a1);
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class FELIX5516Test method testServiceNotDereferencedInternallyUsingReflectionCallback.
public void testServiceNotDereferencedInternallyUsingReflectionCallback() throws Exception {
final DependencyManager dm = getDM();
Component service = component(dm).impl(new Factory()).provides(Service.class).build();
Component client = component(dm).impl(new Client()).withSvc(Service.class, svc -> svc.required().dereference(false).add("bind")).build();
dm.add(service);
dm.add(client);
m_ensure.waitForStep(9, 5000);
dm.clear();
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class FELIX5516Test method testServiceNotDereferencedInternallyUsingMethodReference.
public void testServiceNotDereferencedInternallyUsingMethodReference() throws Exception {
final DependencyManager dm = getDM();
Component service = component(dm).impl(new Factory()).provides(Service.class).build();
Component client = component(dm).impl(new Client()).withSvc(Service.class, svc -> svc.required().add(Client::bind)).build();
dm.add(service);
dm.add(client);
m_ensure.waitForStep(9, 5000);
dm.clear();
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class FactoryConfigurationAdapterTest method testFactoryConfigurationAdapterWithMethodRef.
public void testFactoryConfigurationAdapterWithMethodRef() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
m_ensure = new Ensure();
// Create a Configuration instance, which will create/update/remove a configuration for factoryPid "MyFactoryPid"
ConfigurationCreator configurator = new ConfigurationCreator("MyFactoryPid", "key", "value1");
Component s1 = component(m).impl(configurator).withSvc(ConfigurationAdmin.class, true).build();
// Create an Adapter that will be instantiated, once the configuration is created.
// This Adapter provides an AdapterService, and depends on an AdapterExtraDependency service.
Component s2 = factoryPidAdapter(m).factoryPid("MyFactoryPid").impl(Adapter.class).update(Adapter::updated).propagate().provides(AdapterService.class, "foo", "bar").withSvc(AdapterExtraDependency.class, true).build();
// Create extra adapter service dependency upon which our adapter depends on.
Component s3 = component(m).impl(new AdapterExtraDependency()).provides(AdapterExtraDependency.class).build();
// Create an AdapterService Consumer
Component s4 = component(m).impl(AdapterServiceConsumer.class).withSvc(AdapterService.class, srv -> srv.add("bind").change("change").remove("remove")).build();
// Start services
m.add(s1);
m.add(s2);
m.add(s3);
m.add(s4);
// Wait for step 8: the AdapterService consumer has been injected with the AdapterService, and has called the doService method.
m_ensure.waitForStep(8, 10000);
// Modify configuration.
configurator.update("key", "value2");
// Wait for step 13: the AdapterService has been updated, and the AdapterService consumer has seen the change
m_ensure.waitForStep(13, 10000);
// Remove the configuration
// The stop method will remove the configuration
m.remove(s1);
m_ensure.waitForStep(16, 10000);
m.clear();
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class FactoryInjectedWithConfigurationBeforeTheCreateMethod method testServiceInjection.
public void testServiceInjection() {
DependencyManager m = getDM();
m_e = new Ensure();
// Create the component that creates a configuration.
Component configurator = component(m).impl(new Configurator("foobar")).withSvc(ConfigurationAdmin.class, true).build();
// Create the object that has to be injected with the configuration before its create method is called.
MyFactory factory = new MyFactory();
// Create the Component for the MyComponent class that is created using the factory above.
Component myComponent = component(m).factory(factory, "create").withCnf(b -> b.pid("foobar").update(factory, "updated")).build();
// provide the configuration
m.add(configurator);
m.add(myComponent);
m_e.waitForStep(4, 10000);
m.remove(myComponent);
m.remove(configurator);
}
Aggregations