Search in sources :

Example 46 with Component

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

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

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

Example 49 with Component

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

the class DynamicProxyAspectTest method testImplementGenericAspectWithDynamicProxyAndFactoryRef.

public void testImplementGenericAspectWithDynamicProxyAndFactoryRef() {
    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"), Factory::create).build();
    Component a2 = aspect(m, ServiceInterface2.class).rank(10).autoConfig("m_service").factory(() -> new Factory(e, ServiceInterface2.class, "ServiceInterfaceProxy2"), Factory::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)

Example 50 with Component

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

the class FELIX5516Test method testServiceNotDereferencedInternallyUsingReflectionCallback.

public void testServiceNotDereferencedInternallyUsingReflectionCallback() 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().dereference(false).add("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)

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