Package org.apache.felix.dm

Examples of org.apache.felix.dm.DependencyManager


@RunWith(PaxExam.class)
public class TemporalServiceDependencyTest extends TestBase {
    @Test
    public void testServiceConsumptionAndIntermittentAvailability() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service provider and consumer
        Component sp = m.createComponent().setImplementation(new TemporalServiceProvider(e)).setInterface(TemporalServiceInterface.class.getName(), null);
        Component sp2 = m.createComponent().setImplementation(new TemporalServiceProvider2(e)).setInterface(TemporalServiceInterface.class.getName(), null);
        Component sc = m.createComponent().setImplementation(new TemporalServiceConsumer(e)).add(m.createTemporalServiceDependency().setService(TemporalServiceInterface.class).setRequired(true));
        // add the service consumer
        m.add(sc);
        // now add the first provider
        m.add(sp);
        e.waitForStep(2, 15000);
        // 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, 15000);
        // 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);
    }
View Full Code Here


@RunWith(PaxExam.class)
public class AdapterWithExtraDependenciesTest extends TestBase {
    @Test
    public void testAdapterWithExtraDependenciesAndCallbacks() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
       
        // create a service adapter that adapts to services S1 and has an optional dependency on services S2
        Component sa = m.createAdapterService(S1.class, null)
            .setImplementation(SA.class)
            .add(m.createServiceDependency().setService(S2.class).setCallbacks("add", "remove"));
        m.add(sa);
       
        // create a service S1, which triggers the creation of the first adapter instance (A1)
        Component s1 = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new S1Impl());
        m.add(s1);
       
        // create a service S2, which will be added to A1
        Component s2 = m.createComponent().setInterface(S2.class.getName(), null).setImplementation(new S2Impl(e));
        m.add(s2);
       
        // create a second service S1, which triggers the creation of the second adapter instance (A2)
        Component s1b = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new S1Impl());
        m.add(s1b);
       
        // observe that S2 is also added to A2
        e.waitForStep(2, 5000);
       
        // remove S2 again
        m.remove(s2);
       
        // make sure both adapters have their "remove" callbacks invoked
        e.waitForStep(4, 5000);
       
        m.remove(s1);
        m.remove(sa);
    }
View Full Code Here

@RunWith(PaxExam.class)
public class FELIX2078_ServiceDependencyTest extends TestBase {
    @Test
    public void testRequiredServiceRegistrationAndConsumption() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service provider and consumer
        Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", "remove"));
        m.add(sp);
        m.add(sp2);
        System.out.println("adding client");
        m.add(sc);
        System.out.println("waiting");
        // wait until both services have been added to our consumer
        e.waitForStep(2, 5000);
        m.remove(sc);
        m.remove(sp2);
        m.remove(sp);
        // ensure we executed all steps inside the component instance
    }
View Full Code Here

        // ensure we executed all steps inside the component instance
    }
   
    @Test
    public void testOptionalServiceRegistrationAndConsumption() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service provider and consumer
        Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
        m.add(sp);
        m.add(sp2);
        m.add(sc);
        // wait until both services have been added to our consumer
        e.waitForStep(2, 5000);
        m.remove(sc);
        m.remove(sp2);
        m.remove(sp);
        // ensure we executed all steps inside the component instance
    }
View Full Code Here

@RunWith(PaxExam.class)
public class ServiceUpdateTest extends TestBase {
    @Test
    public void testServiceUpdate() throws Exception {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a resource provider
        ResourceProvider provider = new ResourceProvider(e);
        // activate it
        m.add(m.createComponent()
        .setImplementation(new ServiceProvider(e))
        .add(m.createServiceDependency()
            .setService(ServiceInterface.class)
        .setRequired(true)
        .setCallbacks("add", "change", "remove")
      )
    );
       
        m.add(m.createComponent()
        .setImplementation(provider)
        .add(m.createServiceDependency()
          .setService(ResourceHandler.class)
          .setCallbacks("add", "remove")
      )
    );
       
        // create a resource adapter for our single resource
        // note that we can provide an actual implementation instance here because there will be only one
        // adapter, normally you'd want to specify a Class here
        CallbackInstance callbackInstance = new CallbackInstance(e);
        Properties serviceProps = new Properties();
        serviceProps.setProperty("number", "1");
        Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed")
            .setImplementation(new ResourceAdapter(e))
            .setInterface(ServiceInterface.class.getName(), serviceProps)
            .setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
        m.add(component);
        // wait until the single resource is available
        e.waitForStep(1, 5000);
        // wait until the component gets the dependency injected
        e.waitForStep(2, 5000);
        // trigger a 'change' in our resource
        provider.change();
        // wait until the changed callback is invoked
        e.waitForStep(3, 5000);       
        // wait until the changed event arrived at the component
        e.waitForStep(4, 5000);
        System.out.println("Done!");
        m.clear();
     }
View Full Code Here

    }
   
    @Test
    public void testFELIX2987() {
        // mimics testComponentWithRequiredConfigurationAndServicePropertyPropagation
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service provider and consumer
        Component s1 = m.createComponent().setImplementation(new ConfigurationConsumer2(e)).setInterface(Runnable.class.getName(), null).add(m.createConfigurationDependency().setPid("test").setPropagate(true));
        Component s2 = m.createComponent().setImplementation(new ConfigurationCreator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true));
        Component s3 = m.createComponent().setImplementation(new ConfiguredServiceConsumer(e)).add(m.createServiceDependency().setService(Runnable.class, ("(testkey=testvalue)")).setRequired(true));
        m.add(s1);
        m.add(s2);
        m.add(s3);
        e.waitForStep(4, 15000);
        m.remove(s1);
        m.remove(s2);
        m.remove(s3);
        // ensure we executed all steps inside the component instance
        e.step(5);
    }
View Full Code Here

@RunWith(PaxExam.class)
public class AspectWhiteboardTest extends TestBase {
    @Test
    public void testWhiteboardConsumer() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create service providers and consumer
        Component sp1 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        ServiceConsumer sci = new ServiceConsumer(e);
        Component sc = m.createComponent().setImplementation(sci).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
        Component sa2 = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(new ServiceAspect(e, 3));
        Component sa1 = m.createAspectService(ServiceInterface.class, null, 10, null).setImplementation(new ServiceAspect(e, 4));
       
        // start with a service consumer
        System.out.println("Adding consumer");
        m.add(sc);
       
        // then add two providers, so the consumer will see two services
        System.out.println("Adding 2 providers");
        m.add(sp1);
        m.add(sp2);
       
        // make sure consumer sees both services
        Assert.assertEquals(2, sci.services());
       
        // add an aspect with ranking 20
        System.out.println("Adding aspect with rank 20");
        m.add(sa2);
       
        // make sure the consumer sees the two new aspects and no longer sees the two original services
        Assert.assertEquals(2, sci.services());
        Assert.assertEquals(20, sci.highestRanking());
        Assert.assertEquals(20, sci.lowestRanking());
       
        // add an aspect with ranking 10
        System.out.println("Adding aspect with rank 10");
        m.add(sa1);
       
        // make sure the consumer still sees the two aspects with ranking 20
        Assert.assertEquals(2, sci.services());
        Assert.assertEquals(20, sci.highestRanking());
        Assert.assertEquals(20, sci.lowestRanking());
       
        // remove the aspect with ranking 20
        System.out.println("Removing aspect with rank 20");
        m.remove(sa2);
       
        // make sure the consumer now sees the aspects with ranking 10
        Assert.assertEquals(2, sci.services());
        Assert.assertEquals(10, sci.highestRanking());
        Assert.assertEquals(10, sci.lowestRanking());
       
        // remove one of the original services
        System.out.println("Removing 1 service");
        m.remove(sp1);
       
        // make sure the aspect of that service goes away
        Assert.assertEquals(1, sci.services());
        Assert.assertEquals(10, sci.highestRanking());
        Assert.assertEquals(10, sci.lowestRanking());
       
        // remove the aspect with ranking 10
        System.out.println("Removing aspect with rank 10");
        m.remove(sa1);
       
        // make sure only the original service remains
        Assert.assertEquals(1, sci.services());
        Assert.assertEquals(0, sci.highestRanking());
        Assert.assertEquals(0, sci.lowestRanking());

        System.out.println("Done with test");

        // end of test
        m.remove(sa2);
        m.remove(sp2);
        m.remove(sc);
    }
View Full Code Here

     * The extra dependencies are added using a List object (Component.add(List)).
     * A component c1 will define two extra dependencies over *available* c4/c5 services.
     */
     @Test
     public void testWithTwoAvailableExtraDependency() {  
         DependencyManager m = new DependencyManager(context);
         // Helper class that ensures certain steps get executed in sequence
         Ensure e = new Ensure();
         Component c1 = m.createComponent()
                         .setInterface(Service1.class.getName(), null)
                         .setImplementation(new MyComponent1(e))
                         .add(m.createServiceDependency()
                              .setService(Service2.class)
                              .setRequired(true)
                              .setAutoConfig("m_service2"));
        
         Component c2 = m.createComponent()
                         .setImplementation(new MyComponent2(e))
                         .add(m.createServiceDependency()
                              .setService(Service1.class)
                              .setRequired(false)
                              .setAutoConfig(false)
                              .setCallbacks("added", null, null));
             
         Component c3 = m.createComponent()
                         .setInterface(Service2.class.getName(), null)
                         .setImplementation(Service2Impl.class);
        
         Hashtable h = new Hashtable();
         h.put("type", "xx");
         Component c4 = m.createComponent()
                         .setInterface(Service3.class.getName(), h)
                         .setImplementation(Service3Impl1.class);

         h = new Hashtable();
         h.put("type", "yy");
         Component c5 = m.createComponent()
                         .setInterface(Service3.class.getName(), h)
                         .setImplementation(Service3Impl2.class);


         System.out.println("\n+++ Adding c2 / MyComponent2");
         m.add(c2);
         System.out.println("\n+++ Adding c3 / Service2");
         m.add(c3);
         System.out.println("\n+++ Adding c4 / Service3(xx)");
         m.add(c4);
         System.out.println("\n+++ Adding c5 / Service3(yy)");
         m.add(c5);
         System.out.println("\n+++ Adding c1 / MyComponent1");
         // c1 have declared two extra dependency on Service3 (xx/yy).
         // both extra dependencies are available, so the c1 component should be started immediately.
         m.add(c1);
         e.waitForStep(3, 3000);
         m.clear();
     }
View Full Code Here

     * So, c1 is not yet activated.
     * Then c5 is added, and it triggers the c1 activation ...
     */
   @Test
    public void testWithOneAvailableExtraDependency() { 
        DependencyManager m = new DependencyManager(context);
        // Helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        Component c1 = m.createComponent()
                        .setInterface(Service1.class.getName(), null)
                        .setImplementation(new MyComponent1(e))
                        .add(m.createServiceDependency()
                             .setService(Service2.class)
                             .setRequired(true)
                             .setAutoConfig("m_service2"));
       
        Component c2 = m.createComponent()
                        .setImplementation(new MyComponent2(e))
                        .add(m.createServiceDependency()
                             .setService(Service1.class)
                             .setRequired(false)
                             .setAutoConfig(false)
                             .setCallbacks("added", null, null));
            
        Component c3 = m.createComponent()
                        .setInterface(Service2.class.getName(), null)
                        .setImplementation(Service2Impl.class);
       
        Hashtable h = new Hashtable();
        h.put("type", "xx");
        Component c4 = m.createComponent()
                        .setInterface(Service3.class.getName(), h)
                        .setImplementation(Service3Impl1.class);

        h = new Hashtable();
        h.put("type", "yy");
        Component c5 = m.createComponent()
                        .setInterface(Service3.class.getName(), h)
                        .setImplementation(Service3Impl2.class);


        System.out.println("\n+++ Adding c2 / MyComponent2");
        m.add(c2);
        System.out.println("\n+++ Adding c3 / Service2");
        m.add(c3);
        System.out.println("\n+++ Adding c4 / Service3(xx)");
        m.add(c4);
        System.out.println("\n+++ Adding c1 / MyComponent1");
        m.add(c1);

        // c1 have declared two extra dependency on Service3 (xx/yy).
        // So, because we have not yet added c5 (yy), c1 should not be started currently.
        // But, now, we'll add c5 (Service3/yy) and c1 should then be started ...
        System.out.println("\n+++ Adding c5 / Service3(yy)");
        m.add(c5);
        e.waitForStep(3, 3000);
        m.clear();
    }
View Full Code Here

            m_ensure = e;
        }

        void init(Component c) {
            m_ensure.step(1);
            DependencyManager dm = c.getDependencyManager();
            List l = new ArrayList();
            // Service3/xx currently available
            l.add(dm.createServiceDependency()
                     .setInstanceBound(true)
                     .setService(Service3.class, "(type=xx)")
                     .setRequired(true)
                     .setAutoConfig("m_service3_xx"));
           
            // Service3/yy not yet available
            l.add(dm.createServiceDependency()
                  .setInstanceBound(true)
                  .setService(Service3.class, "(type=yy)")
                  .setRequired(true)
                  .setAutoConfig("m_service3_yy"));
            c.add(l);
View Full Code Here

TOP

Related Classes of org.apache.felix.dm.DependencyManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.