Search in sources :

Example 66 with Component

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

the class AdapterAndConsumerTest method testServiceWithAdapterAndConsumer.

public void testServiceWithAdapterAndConsumer() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component provider = m.createComponent().setInterface(OriginalService.class.getName(), null).setImplementation(new ServiceProvider(e));
    Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(AdaptedService.class).setRequired(true));
    Component adapter = m.createAdapterService(OriginalService.class, null).setInterface(AdaptedService.class.getName(), null).setImplementation(ServiceAdapter.class);
    // add the provider and the adapter
    m.add(provider);
    m.add(adapter);
    // 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(2, 5000);
    // remove the provider again
    m.remove(provider);
    // ensure that the consumer is stopped
    e.waitForStep(3, 5000);
    // remove adapter and consumer
    m.remove(adapter);
    m.remove(consumer);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 67 with Component

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

the class AdapterNoAutoConfigIfInstanceCallbackIsUsed method testNoAutoConfigIfINstanceCallbackIsUsed.

public void testNoAutoConfigIfINstanceCallbackIsUsed() {
    DependencyManager m = getDM();
    // Declare S1 service
    Component s1 = m.createComponent().setImplementation(S1Impl.class).setInterface(S1.class.getName(), null);
    m.add(s1);
    // Declare S1 adapter
    S1AdapterCallback s1AdapterCB = new S1AdapterCallback();
    Component s1Adapter = m.createAdapterService(S1.class, null, null, s1AdapterCB, "set", null, null, null, false).setImplementation(S1Adapter.class);
    m.add(s1Adapter);
    // 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) Component(org.apache.felix.dm.Component)

Example 68 with Component

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

the class AdapterWithConfigurationAndMetaType method testAdapterWithConfigurationDependencyAndMetaType.

public void testAdapterWithConfigurationDependencyAndMetaType() {
    DependencyManager m = getDM();
    Ensure e = new Ensure();
    m.add(m.createAdapterService(A.class, null).setInterface(B.class.getName(), null).setImplementation(new BImpl(e)).add(m.createConfigurationDependency().setPid(PID).setHeading(PID_HEADING).setDescription(PID_DESC).add(m.createPropertyMetaData().setCardinality(Integer.MAX_VALUE).setType(String.class).setHeading(WORDS_HEADING).setDescription(WORDS_DESC).setDefaults(new String[] { "hello", "world" }).setId(WORDS_PROPERTY))));
    m.add(m.createComponent().setInterface(A.class.getName(), null).setImplementation(new AImpl()));
    Component configurator = m.createComponent().setImplementation(new Configurator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)).add(m.createServiceDependency().setService(MetaTypeService.class).setRequired(true));
    m.add(configurator);
    // Ensures that all components are started
    e.waitForStep(4, 5000);
    // now stop configurator, and ensure that all components have been stopped
    m.remove(configurator);
    e.waitForStep(7, 5000);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 69 with Component

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();
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component a = m.createComponent().setImplementation(new AImpl(e)).setInterface(A.class.getName(), props);
    Component b = m.createAdapterService(A.class, null, "add", "change", "remove").setInterface(B.class.getName(), null).setImplementation(new BImpl(e));
    Component c = m.createComponent().setImplementation(new CImpl()).setInterface(C.class.getName(), null).add(m.createServiceDependency().setService(A.class, "(foo=bar)").setRequired(true));
    m.add(a);
    m.add(c);
    m.add(b);
    e.waitForStep(4, 5000);
    System.out.println("changing A props ...");
    props = new Hashtable();
    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 : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 70 with Component

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

the class AdapterWithPropagationTest method testAdapterWithPropagation.

public void testAdapterWithPropagation() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Dictionary s1Properties = new Hashtable();
    s1Properties.put("p1", "v1");
    // should not override adapter
    s1Properties.put("p2", "v2overriden");
    Component s1 = m.createComponent().setImplementation(new S1Impl(e)).setInterface(S1.class.getName(), s1Properties);
    Dictionary s1AdapterProperties = new Hashtable();
    s1AdapterProperties.put("p2", "v2");
    Component s1Adapter = m.createAdapterService(S1.class, null, "add", "change", null).setInterface(S2.class.getName(), s1AdapterProperties).setImplementation(new S1Adapter(e));
    Component s3 = m.createComponent().setImplementation(new S3(e)).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    m.add(s1);
    m.add(s1Adapter);
    m.add(s3);
    e.waitForStep(3, 5000);
    s1Properties = new Hashtable();
    s1Properties.put("p1", "v1modified");
    s1Properties.put("p2", "v2overriden");
    s1.setServiceProperties(s1Properties);
    e.waitForStep(5, 5000);
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

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