Search in sources :

Example 51 with Component

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();
}
Also used : Component(org.apache.felix.dm.Component) PrototypeServiceFactory(org.osgi.framework.PrototypeServiceFactory) DependencyManager(org.apache.felix.dm.DependencyManager) Bundle(org.osgi.framework.Bundle) ServiceObjects(org.osgi.framework.ServiceObjects) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration) DependencyManager(org.apache.felix.dm.DependencyManager) PrototypeServiceFactory(org.osgi.framework.PrototypeServiceFactory) Component(org.apache.felix.dm.Component)

Example 52 with Component

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();
}
Also used : Component(org.apache.felix.dm.Component) Map(java.util.Map) DependencyManagerActivator.factoryPidAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.factoryPidAdapter) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) 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) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyManagerActivator.factoryPidAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.factoryPidAdapter) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 53 with Component

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);
}
Also used : Component(org.apache.felix.dm.Component) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) 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) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 54 with Component

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);
}
Also used : Component(org.apache.felix.dm.Component) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) 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) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 55 with Component

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();
}
Also used : Component(org.apache.felix.dm.Component) Constants(org.osgi.framework.Constants) Map(java.util.Map) HashMap(java.util.HashMap) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Component (org.apache.felix.dm.Component)271 DependencyManager (org.apache.felix.dm.DependencyManager)227 Ensure (org.apache.felix.dm.itest.util.Ensure)91 DependencyManagerActivator.component (org.apache.felix.dm.lambda.DependencyManagerActivator.component)65 Hashtable (java.util.Hashtable)59 Assert (org.junit.Assert)46 Dictionary (java.util.Dictionary)32 ServiceReference (org.osgi.framework.ServiceReference)25 Map (java.util.Map)23 DependencyManagerActivator.aspect (org.apache.felix.dm.lambda.DependencyManagerActivator.aspect)21 Bundle (org.osgi.framework.Bundle)17 ServiceRegistration (org.osgi.framework.ServiceRegistration)17 DependencyManagerActivator.adapter (org.apache.felix.dm.lambda.DependencyManagerActivator.adapter)15 ArrayList (java.util.ArrayList)14 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)13 HashMap (java.util.HashMap)12 ServiceDependency (org.apache.felix.dm.ServiceDependency)12 Test (org.junit.Test)11 Properties (java.util.Properties)10 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)10