Search in sources :

Example 16 with DependencyManager

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

the class TemporalServiceDependencyTest method testServiceConsumptionAndIntermittentAvailability.

public void testServiceConsumptionAndIntermittentAvailability() {
    final DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    TemporalServiceProvider provider = new TemporalServiceProvider(e);
    Component sp = m.createComponent().setImplementation(provider).setInterface(TemporalServiceInterface.class.getName(), null);
    TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
    Component sp2 = m.createComponent().setImplementation(provider2).setInterface(TemporalServiceInterface.class.getName(), null);
    TemporalServiceConsumer consumer = new TemporalServiceConsumer(e);
    Component sc = m.createComponent().setImplementation(consumer).add(m.createTemporalServiceDependency(10000).setService(TemporalServiceInterface.class).setRequired(true));
    // add the service consumer
    m.add(sc);
    // now add the first provider
    m.add(sp);
    e.waitForStep(2, 5000);
    // and remove it again (this should not affect the consumer yet)
    m.remove(sp);
    // now add the second provider
    m.add(sp2);
    e.step(3);
    e.waitForStep(4, 5000);
    // and remove it again
    m.remove(sp2);
    // finally remove the consumer
    m.remove(sc);
    // ensure we executed all steps inside the component instance
    e.step(6);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 17 with DependencyManager

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

the class TemporalServiceDependencyTest method testFelix4602_PropagateServiceInvocationException.

public void testFelix4602_PropagateServiceInvocationException() {
    final DependencyManager m = getDM();
    final Ensure ensure = new Ensure();
    Runnable provider = new Runnable() {

        public void run() {
            throw new UncheckedException();
        }
    };
    Hashtable props = new Hashtable();
    props.put("target", getClass().getSimpleName());
    Component providerComp = m.createComponent().setInterface(Runnable.class.getName(), props).setImplementation(provider);
    Object consumer = new Object() {

        volatile Runnable m_provider;

        @SuppressWarnings("unused")
        void start() {
            try {
                ensure.step(1);
                m_provider.run();
            } catch (UncheckedException e) {
                ensure.step(2);
            }
        }
    };
    Component consumerComp = m.createComponent().setImplementation(consumer).add(m.createTemporalServiceDependency(5000).setService(Runnable.class, "(target=" + getClass().getSimpleName() + ")").setRequired(true));
    m.add(consumerComp);
    m.add(providerComp);
    ensure.waitForStep(2, 5000);
    m.clear();
}
Also used : Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 18 with DependencyManager

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

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

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

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