Search in sources :

Example 56 with DependencyManager

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

the class TestBase method setUp.

public void setUp() throws Exception {
    warn("Setting up test " + getClass().getName());
    context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
    Hashtable<String, Object> props = new Hashtable<>();
    props.put(Constants.SERVICE_RANKING, new Integer(Integer.MAX_VALUE));
    logService = context.registerService(LogService.class.getName(), this, props);
    context.addFrameworkListener(this);
    m_dm = new DependencyManager(context);
    if (m_parallel) {
        warn("Using threadpool ...");
        m_threadPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
        m_componentExecutorFactoryReg = context.registerService(ComponentExecutorFactory.class.getName(), new ComponentExecutorFactory() {

            @Override
            public Executor getExecutorFor(Component component) {
                return m_threadPool;
            }
        }, null);
    }
}
Also used : Hashtable(java.util.Hashtable) ComponentExecutorFactory(org.apache.felix.dm.ComponentExecutorFactory) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 57 with DependencyManager

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

the class AbstractServiceDependencyTest method testAbstractClassDependency.

public void testAbstractClassDependency() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    Component sp = m.createComponent().setInterface(ServiceAbstract.class.getName(), null).setImplementation(new ServiceProvider(e));
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceAbstract.class).setRequired(true).setCallbacks("bind", "unbind"));
    m.add(sp);
    m.add(sc);
    m.remove(sp);
    // ensure we executed all steps inside the component instance
    e.step(8);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 58 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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 59 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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 60 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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)

Aggregations

DependencyManager (org.apache.felix.dm.DependencyManager)255 Component (org.apache.felix.dm.Component)226 Ensure (org.apache.felix.dm.itest.util.Ensure)91 DependencyManagerActivator.component (org.apache.felix.dm.lambda.DependencyManagerActivator.component)70 Hashtable (java.util.Hashtable)56 Assert (org.junit.Assert)50 Dictionary (java.util.Dictionary)28 Map (java.util.Map)27 ServiceReference (org.osgi.framework.ServiceReference)24 DependencyManagerActivator.aspect (org.apache.felix.dm.lambda.DependencyManagerActivator.aspect)21 ServiceRegistration (org.osgi.framework.ServiceRegistration)18 DependencyManagerActivator.adapter (org.apache.felix.dm.lambda.DependencyManagerActivator.adapter)15 Bundle (org.osgi.framework.Bundle)15 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)12 ServiceDependency (org.apache.felix.dm.ServiceDependency)11 Properties (java.util.Properties)10 List (java.util.List)9 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)9