Search in sources :

Example 36 with DependencyManager

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

the class AutoConfigTest method testMapField.

public void testMapField() throws Exception {
    final DependencyManager dm = getDM();
    ConsumerWithMapField consumer = new ConsumerWithMapField();
    Component c = createConsumer(dm, consumer);
    Component p1 = createProvider(dm, 10, new Provider() {

        public void run() {
            m_ensure.step();
        }

        public String toString() {
            return "provider1";
        }
    });
    Component p2 = createProvider(dm, 20, new Provider() {

        public void run() {
            m_ensure.step();
        }

        public String toString() {
            return "provider2";
        }
    });
    dm.add(p2);
    dm.add(p1);
    dm.add(c);
    // the consumer should have been injected with all providers.
    m_ensure.waitForStep(3, 5000);
    // check if all providers are there
    Assert.assertNotNull(consumer.getProvider("provider1"));
    Assert.assertNotNull(consumer.getProvider("provider2"));
    // remove provider1
    dm.remove(p1);
    // check if provider1 has been removed and if provider2 is still there
    Assert.assertNull(consumer.getProvider("provider1"));
    Assert.assertNotNull(consumer.getProvider("provider2"));
    // remove provider2, the consumer should be stopped
    dm.remove(p2);
    m_ensure.waitForStep(4, 5000);
    dm.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 37 with DependencyManager

use of org.apache.felix.dm.DependencyManager 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 = bundleAdapter(m).mask(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE).callbackInstance(cbInstance).add("add").remove("remove").impl(baWithCb).provides(BundleAdapter.class.getName()).build();
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = component(m).impl(c).withSvc(BundleAdapter.class, s -> s.add("add").remove("remove")).build();
    // 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 : Component(org.apache.felix.dm.Component) DependencyManagerActivator.bundleAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.bundleAdapter) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) Bundle(org.osgi.framework.Bundle) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 38 with DependencyManager

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

the class BundleDependencyTest method testBundleDependenciesRef.

public void testBundleDependenciesRef() {
    DependencyManager m = getDM();
    // create a service provider and consumer
    MyConsumer c = new MyConsumer();
    Component consumer = component(m, comp -> comp.impl(c).withBundle(bundle -> bundle.add(MyConsumer::add).remove(MyConsumer::remove)));
    // 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();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    String filter = "(Bundle-SymbolicName=" + BSN + ")";
    Component consumerWithFilter = component(m, comp -> comp.impl(new FilteredConsumer(e)).withBundle(bundle -> bundle.filter(filter).add(FilteredConsumer::add).remove(FilteredConsumer::remove)));
    e.step(2);
    // remove the consumer again
    m.remove(consumerWithFilter);
    e.step(4);
}
Also used : Component(org.apache.felix.dm.Component) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) Bundle(org.osgi.framework.Bundle) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 39 with DependencyManager

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

the class CFDependencyTest method testComponentWithCFDependencyDefinedFromInit.

public void testComponentWithCFDependencyDefinedFromInit() throws Exception {
    final DependencyManager dm = getDM();
    // Create a consumer depending on a Provider and on the result of a CF.
    // (the CF dependency is added from the Consumer.init() method).
    CompletableFuture<String> cf = new CompletableFuture<>();
    Component consumer = component(dm).impl(new Consumer(cf)).withSvc(Provider.class, true).build();
    // Create provider
    Component provider = component(dm).impl(new ProviderImpl()).provides(Provider.class).build();
    dm.add(consumer);
    dm.add(provider);
    // Sets the result in the CF: this will normally trigger the activation of the Consumer component.
    cf.complete("cfFesult");
    m_ensure.waitForStep(3, 5000);
    dm.clear();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 40 with DependencyManager

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

the class DynamicProxyAspectTest method testImplementGenericAspectWithDynamicProxyAndFactory.

public void testImplementGenericAspectWithDynamicProxyAndFactory() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    DynamicProxyHandler.resetCounter();
    // create two service providers, each providing a different service interface
    Component sp1 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
    Component sp2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface2.class).build();
    // create a dynamic proxy based aspect and hook it up to both services
    Component a1 = aspect(m, ServiceInterface.class).rank(10).autoConfig("m_service").factory(new Factory(e, ServiceInterface.class, "ServiceInterfaceProxy"), "create").build();
    Component a2 = aspect(m, ServiceInterface2.class).rank(10).autoConfig("m_service").factory(new Factory(e, ServiceInterface2.class, "ServiceInterfaceProxy2"), "create").build();
    // create a client that invokes a method on boths services, validate that it goes
    // through the proxy twice
    Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(true, ServiceInterface.class, ServiceInterface2.class).build();
    // register both producers, validate that both services are started
    m.add(sp1);
    e.waitForStep(1, 2000);
    m.add(sp2);
    e.waitForStep(2, 2000);
    // add both aspects, and validate that both instances have been created
    m.add(a1);
    m.add(a2);
    e.waitForStep(4, 4000);
    // add the client, which will automatically invoke both services
    m.add(sc);
    // wait until both services have been invoked
    e.waitForStep(6, 4000);
    // make sure the proxy has been called twice
    Assert.assertEquals("Proxy should have been invoked this many times.", 2, DynamicProxyHandler.getCounter());
    m.remove(sc);
    m.remove(a2);
    m.remove(a1);
    m.remove(sp2);
    m.remove(sp1);
    m.remove(a2);
    m.remove(a1);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

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