use of org.apache.felix.dm.Component 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.Component 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.Component 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);
}
use of org.apache.felix.dm.Component in project felix by apache.
the class FactoryInjectedWithConfigurationBeforeTheCreateMethod method testServiceInjectionRef.
public void testServiceInjectionRef() {
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);
}
use of org.apache.felix.dm.Component in project felix by apache.
the class InstanceBoundDependencyTest method testServiceInjection.
public void testServiceInjection() {
DependencyManager m = getDM();
m_e = new Ensure();
// Create a "C" component: it depends on some S1 services, and on some S2 instance-bound services (declared from C.init() method)
C cimpl = new C();
Component c = component(m).impl(cimpl).withSvc(S1.class, sb -> sb.add("addS1").change("changeS1").remove("removeS1").autoConfig(true)).build();
m.add(c);
// Add S1 (s1_1): C.add(S1 s1) is called, then init() is called where a dependency is declared on S2
Hashtable s1_1_props = new Hashtable();
s1_1_props.put("name", "s1_1");
s1_1_props.put(Constants.SERVICE_RANKING, new Integer(10));
S1Impl s1_1_impl = new S1Impl();
Component s1_1 = component(m).impl(s1_1_impl).provides(S1.class.getName(), s1_1_props).build();
m.add(s1_1);
// wait until C.init called
m_e.waitForStep(1, 5000);
ServiceReference ref = cimpl.getS1("s1_1");
Assert.assertNotNull(ref);
Assert.assertNotNull(cimpl.getS1());
Assert.assertEquals(s1_1_impl, cimpl.getS1());
// At this point, MyComponent is in INSTANTIATED_AND_WAITING_FOR_REQUIRED state.
// add now add another higher ranked S1 (s1_2) instance. C.add(s1_2) method should be called (the S1 dependency
// is not instance bound), and m_s1 autoconfig field should be updated.
Hashtable s1_2_props = new Hashtable();
s1_2_props.put(Constants.SERVICE_RANKING, new Integer(20));
s1_2_props.put("name", "s1_2");
S1Impl s1_2_impl = new S1Impl();
Component s1_2 = component(m).impl(s1_2_impl).provides(S1.class.getName(), s1_2_props).build();
m.add(s1_2);
ref = cimpl.getS1("s1_2");
Assert.assertNotNull(ref);
Assert.assertNotNull(cimpl.getS1());
// must return s1_2 with ranking = 20
Assert.assertEquals(s1_2_impl, cimpl.getS1());
// Now, change the s1_1 service properties: C.changed(s1_1) should be called, and C.m_s1AutoConfig should be updated
s1_1_props.put(Constants.SERVICE_RANKING, new Integer(30));
s1_1.setServiceProperties(s1_1_props);
ref = cimpl.getS1("s1_1");
Assert.assertNotNull(ref);
Assert.assertEquals(new Integer(30), ref.getProperty(Constants.SERVICE_RANKING));
Assert.assertNotNull(cimpl.getS1());
Assert.assertEquals(s1_1_impl, cimpl.getS1());
// Now, remove the s1_1: C.remove(s1_1) should be called, and C.m_s1AutoConfig should be updated
m.remove(s1_1);
ref = cimpl.getS1("s1_1");
Assert.assertNull(cimpl.getS1("s1_1"));
Assert.assertNotNull(cimpl.getS1());
Assert.assertEquals(s1_2_impl, cimpl.getS1());
m.clear();
}
Aggregations