Search in sources :

Example 81 with Component

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

the class AutoConfigTest method testField.

public void testField() throws Exception {
    final DependencyManager dm = getDM();
    // Create a consumer, depending on some providers (autoconfig field).
    ConsumeWithProviderField consumer = new ConsumeWithProviderField();
    Component c = createConsumer(dm, consumer);
    // Create two providers
    Component p1 = createProvider(dm, 10, new Provider() {

        public String toString() {
            return "provider1";
        }

        public void run() {
            m_ensure.step();
        }
    });
    Component p2 = createProvider(dm, 20, new Provider() {

        public String toString() {
            return "provider2";
        }

        public void run() {
            m_ensure.step();
        }
    });
    // add the two providers
    dm.add(p2);
    dm.add(p1);
    // add the consumer, which should have been injected with provider2 (highest rank)
    dm.add(c);
    m_ensure.waitForStep(1, 5000);
    // remove the provider2, the consumer should now be injected with provider1
    dm.remove(p2);
    Assert.assertNotNull(consumer.getProvider());
    Assert.assertEquals("provider1", consumer.getProvider().toString());
    // remove the provider1, the consumer should have been stopped
    dm.remove(p1);
    m_ensure.waitForStep(2, 5000);
    dm.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 82 with Component

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

the class BundleAdapterTest method testBundleAdapter.

public void testBundleAdapter() {
    DependencyManager m = getDM();
    // create a bundle adapter service (one is created for each bundle)
    Component adapter = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false).setImplementation(BundleAdapter.class).setInterface(BundleAdapter.class.getName(), null);
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove"));
    // add the bundle adapter
    m.add(adapter);
    // add the service consumer
    m.add(consumer);
    // check if at least one bundle was found
    c.check();
    // remove the consumer again
    m.remove(consumer);
    // check if all bundles were removed correctly
    c.doubleCheck();
    // remove the bundle adapter
    m.remove(adapter);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 83 with Component

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

the class BundleAdapterTest method testBundleAdapterWithCallbackInstance.

public void testBundleAdapterWithCallbackInstance() {
    DependencyManager m = getDM();
    // create a bundle adapter service (one is created for each bundle)
    BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback();
    BundleAdapterCallbackInstance cbInstance = new BundleAdapterCallbackInstance(baWithCb);
    Component adapter = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false, cbInstance, "add", null, "remove").setImplementation(baWithCb).setInterface(BundleAdapter.class.getName(), null);
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove"));
    // add the bundle adapter
    m.add(adapter);
    // add the service consumer
    m.add(consumer);
    // check if at least one bundle was found
    c.check();
    // remove the consumer again
    m.remove(consumer);
    // check if all bundles were removed correctly
    c.doubleCheck();
    // remove the bundle adapter
    m.remove(adapter);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 84 with Component

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

the class BundleAdapterWithCallbacksNotAutoConfiguredTest method testBundleAdapterWithCallbacksNotAutoConfigured.

public void testBundleAdapterWithCallbacksNotAutoConfigured() {
    DependencyManager m = getDM();
    // create a bundle adapter service (one is created for each bundle)
    BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback();
    String bsn = "org.apache.felix.dependencymanager";
    String filter = "(Bundle-SymbolicName=" + bsn + ")";
    Component adapter = m.createBundleAdapterService(Bundle.ACTIVE, filter, false, null, "add", null, null).setImplementation(baWithCb);
    // add the bundle adapter
    m.add(adapter);
    // Check if adapter has not been auto configured (because it has callbacks defined).
    m_e.waitForStep(1, 3000);
    Assert.assertNull("bundle adapter must not be auto configured", baWithCb.getBundle());
    // remove the bundle adapters
    m.remove(adapter);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 85 with Component

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

the class BundleDependencyTest method testRequiredBundleDependency.

public void testRequiredBundleDependency() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component consumerWithFilter = m.createComponent().setImplementation(new FilteredConsumerRequired(e)).add(m.createBundleDependency().setRequired(true).setFilter("(Bundle-SymbolicName=" + BSN + ")").setCallbacks("add", "remove"));
    // add a consumer with a filter
    m.add(consumerWithFilter);
    e.waitForStep(1, 5000);
    // remove the consumer again
    m.remove(consumerWithFilter);
    e.waitForStep(2, 5000);
}
Also used : 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