Search in sources :

Example 26 with Component

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

the class UngetAspectServiceTest method testUngetSwappedService.

public void testUngetSwappedService() {
    DependencyManager m = getDM();
    Client clientInstance = new Client();
    Component client = m.createComponent().setImplementation(clientInstance).add(m.createServiceDependency().setService(Provider.class).setRequired(true).setCallbacks("bind", null, null, "swap"));
    Component provider = m.createComponent().setImplementation(new ProviderImpl()).setInterface(Provider.class.getName(), null);
    Aspect aspectInstance = new Aspect();
    Component aspect = m.createAspectService(Provider.class, null, 1).setImplementation(aspectInstance);
    // add client, provider
    m.add(client);
    m.add(provider);
    // wait for client to be bound to provider
    m_ensure.waitForStep(1, 5000);
    // add aspect
    m.add(aspect);
    // check for client to be swapped with aspect
    m_ensure.waitForStep(2, 5000);
    // remove client, and aspect
    m.remove(client);
    m.remove(aspect);
    // Now, no more references should point to the provider
    Assert.assertEquals(false, this.context.ungetService(clientInstance.getServiceRef()));
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 27 with Component

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

the class UngetServiceTest method testUngetService.

public void testUngetService() {
    DependencyManager m = getDM();
    Component service = m.createComponent().setImplementation(new Service()).setInterface(Service.class.getName(), null);
    m.add(service);
    Client clientImpl = new Client();
    Component client = m.createComponent().setImplementation(clientImpl).add(m.createServiceDependency().setService(Service.class).setRequired(true).setCallbacks("bind", null));
    m.add(client);
    m_ensure.waitForStep(1, 5000);
    m.remove(client);
    // The client has been removed and the service reference must have been ungotten.
    ServiceReference ref = clientImpl.getServiceRef();
    Assert.assertEquals(false, this.context.ungetService(ref));
    m.remove(service);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) ServiceReference(org.osgi.framework.ServiceReference)

Example 28 with Component

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

the class Activator method init.

@Override
public void init(BundleContext context, DependencyManager dm) throws Exception {
    Component c = createComponent().setImplementation(AddRemoveService.class).setInterface(AddRemoveService.class.getName(), null);
    dm.add(c);
    dm.remove(c);
}
Also used : Component(org.apache.felix.dm.Component)

Example 29 with Component

use of org.apache.felix.dm.Component 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 = component(m).provides(OriginalService.class).impl(new ServiceProvider(e)).build();
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, true).build();
    Component adapter = adapter(m, OriginalService.class).provides(AdaptedService.class).impl(ServiceAdapter.class).build();
    // 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) Component(org.apache.felix.dm.Component)

Example 30 with Component

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

the class AdapterWithCallbackInstanceTest method testServiceWithAdapterAndConsumer.

public void testServiceWithAdapterAndConsumer() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    ServiceProvider serviceProvider = new ServiceProvider(e);
    Component provider = component(m).provides(OriginalService.class).impl(serviceProvider).build();
    Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, true).build();
    ServiceAdapterCallbackInstance callbackInstance = new ServiceAdapterCallbackInstance(e);
    Component adapter = adapter(m, OriginalService.class).provides(AdaptedService.class).impl(new ServiceAdapter(e)).propagate(true).autoConfig("m_originalService").callbackInstance(callbackInstance).add("set").change("changed").remove("unset").build();
    // add the provider and the adapter
    m.add(provider);
    m.add(adapter);
    // Checks if the callbackInstances is called, and if the adapter start method is called
    e.waitForStep(2, 5000);
    // 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(4, 5000);
    // change the service properties of the provider, and check that the adapter callback instance is changed.
    serviceProvider.changeServiceProperties();
    e.waitForStep(5, 5000);
    // remove the provider
    m.remove(provider);
    // ensure that the consumer is stopped, the adapter callback is called in its unset method, and the adapter is stopped.
    e.waitForStep(8, 5000);
    // remove adapter and consumer
    m.remove(adapter);
    m.remove(consumer);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) 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