Search in sources :

Example 41 with DependencyManager

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

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

Example 43 with DependencyManager

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

the class FELIX5516Test method testServiceNotDereferencedInternallyUsingMethodReference.

public void testServiceNotDereferencedInternallyUsingMethodReference() 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().add(Client::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)

Example 44 with DependencyManager

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

the class FactoryConfigurationAdapterTest method testFactoryConfigurationAdapterWithMethodRef.

public void testFactoryConfigurationAdapterWithMethodRef() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    m_ensure = new Ensure();
    // Create a Configuration instance, which will create/update/remove a configuration for factoryPid "MyFactoryPid"
    ConfigurationCreator configurator = new ConfigurationCreator("MyFactoryPid", "key", "value1");
    Component s1 = component(m).impl(configurator).withSvc(ConfigurationAdmin.class, true).build();
    // Create an Adapter that will be instantiated, once the configuration is created.
    // This Adapter provides an AdapterService, and depends on an AdapterExtraDependency service.
    Component s2 = factoryPidAdapter(m).factoryPid("MyFactoryPid").impl(Adapter.class).update(Adapter::updated).propagate().provides(AdapterService.class, "foo", "bar").withSvc(AdapterExtraDependency.class, true).build();
    // Create extra adapter service dependency upon which our adapter depends on.
    Component s3 = component(m).impl(new AdapterExtraDependency()).provides(AdapterExtraDependency.class).build();
    // Create an AdapterService Consumer
    Component s4 = component(m).impl(AdapterServiceConsumer.class).withSvc(AdapterService.class, srv -> srv.add("bind").change("change").remove("remove")).build();
    // Start services
    m.add(s1);
    m.add(s2);
    m.add(s3);
    m.add(s4);
    // Wait for step 8: the AdapterService consumer has been injected with the AdapterService, and has called the doService method.
    m_ensure.waitForStep(8, 10000);
    // Modify configuration.
    configurator.update("key", "value2");
    // Wait for step 13: the AdapterService has been updated, and the AdapterService consumer has seen the change
    m_ensure.waitForStep(13, 10000);
    // Remove the configuration
    // The stop method will remove the configuration
    m.remove(s1);
    m_ensure.waitForStep(16, 10000);
    m.clear();
}
Also used : Component(org.apache.felix.dm.Component) Map(java.util.Map) DependencyManagerActivator.factoryPidAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.factoryPidAdapter) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyManagerActivator.factoryPidAdapter(org.apache.felix.dm.lambda.DependencyManagerActivator.factoryPidAdapter) Component(org.apache.felix.dm.Component) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 45 with DependencyManager

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

the class FactoryInjectedWithConfigurationBeforeTheCreateMethod method testServiceInjection.

public void testServiceInjection() {
    DependencyManager m = getDM();
    m_e = new Ensure();
    // Create the component that creates a configuration.
    Component configurator = component(m).impl(new Configurator("foobar")).withSvc(ConfigurationAdmin.class, true).build();
    // Create the object that has to be injected with the configuration before its create method is called.
    MyFactory factory = new MyFactory();
    // Create the Component for the MyComponent class that is created using the factory above.
    Component myComponent = component(m).factory(factory, "create").withCnf(b -> b.pid("foobar").update(factory, "updated")).build();
    // provide the configuration
    m.add(configurator);
    m.add(myComponent);
    m_e.waitForStep(4, 10000);
    m.remove(myComponent);
    m.remove(configurator);
}
Also used : Component(org.apache.felix.dm.Component) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) IOException(java.io.IOException) DependencyManager(org.apache.felix.dm.DependencyManager) Assert(org.junit.Assert) DependencyManagerActivator.component(org.apache.felix.dm.lambda.DependencyManagerActivator.component) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) 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