Package org.apache.felix.dm

Examples of org.apache.felix.dm.DependencyManager


    /**
     * Test if an auto config extra dependency is injected in the expected order.
     */
    @Test
    public void testExtraDependencyWithAutoConfig() { 
        DependencyManager m = new DependencyManager(context);
        // Helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // Create a service provider
        Component sp = m.createComponent().setInterface(ProviderInterface.class.getName(), null).setImplementation(ProviderImpl.class);
        // Create a service consumer with a required/autoconfig dependency over the service provider.
        Client c1;
        Component sc1 = m.createComponent().setImplementation((c1 = new Client(e, true, 1)));
        // Create a second service consumer with an optional/autoconfig dependency over the service provider.
        Client c2;
        Component sc2 = m.createComponent().setImplementation(c2 = new Client(e, false, 3));

        // Add service provider and consumer sc1 (required dependency over provider)
        m.add(sc1);
        m.add(sp);
        e.waitForStep(2, 5000);
       
        // Remove provider and consumer
        m.remove(sc1);
        m.remove(sp);
       
        // Add consumer sc2 (optional dependency over provider)
        m.add(sc2);
        e.waitForStep(4, 5000);    
    }
View Full Code Here


            m_required = required;
            m_startStep = startStep;
        }
       
        public void init(Component s) {
            DependencyManager dm = s.getDependencyManager();
            s.add(dm.createServiceDependency()
                .setInstanceBound(true)
                .setService(ProviderInterface.class)
                .setRequired(m_required)
                .setAutoConfig("m_provider"));
            m_ensure.step(m_startStep);
View Full Code Here

        }
    }
   
    @Test
    public void testAdapterWithPropagation() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
       
        Dictionary s1Properties = new Hashtable();
        s1Properties.put("foo", "bar");
        Component s1 = m.createComponent()
                .setImplementation(new S1Impl(e))
                .setInterface(S1.class.getName(), s1Properties);
       
        Component s1Adapter = m.createAdapterService(S1.class, null, "add", "change", null)
                .setInterface(S2.class.getName(), null)
                .setImplementation(new S1Adapter(e));
       
        Component s3 = m.createComponent()
                .setImplementation(new S3(e))
                .add(m.createServiceDependency()
                     .setService(S2.class)
                     .setRequired(true)
                     .setCallbacks("add", "change", null));
                    
             
        m.add(s1);
        m.add(s1Adapter);
        m.add(s3);
       
        e.waitForStep(3, 5000);
       
        s1Properties = new Hashtable();
        s1Properties.put("foo", "bar2");
        s1.setServiceProperties(s1Properties);
       
        e.waitForStep(5, 5000);

        m.clear();
    }
View Full Code Here

@RunWith(PaxExam.class)
public class FELIX2369_ExtraDependencyTest extends TestBase
{
    @Test
    public void testExtraDependencies() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service consumer and provider
        Component sp1 = m.createComponent().setInterface(MyService1.class.getName(), null).setImplementation(new MyService1Impl());
        Component sc = m.createComponent().setImplementation(new MyClient(e, 1));
       
        // provides the MyService1 service (but not the MyService2, which is required by MyClient).
        m.add(sp1);
        // add MyClient (it should not be invoked in its start() method because MyService2 is not there
        m.add(sc);
        // remove MyClient (it should not be invoked in its stop() method because it should not be active, since MyService2 is not there.
        m.remove(sc);
        e.waitForStep(2, 5000);
    }
View Full Code Here

            m_ensure = e;
            m_startStep = startStep;
        }
       
        public void init(Component s) {
            DependencyManager dm = s.getDependencyManager();
            m_ensure.step(m_startStep);
            List extra = Arrays.asList(new ServiceDependency[] {
                    dm.createServiceDependency() // this dependency is available at this point
                      .setInstanceBound(true)
                      .setService(MyService1.class)
                      .setRequired(false)
                      .setCallbacks("bind", null),
                    dm.createServiceDependency() // not available: we should not be started
                      .setInstanceBound(true)
                      .setService(MyService2.class)
                      .setRequired(true)
                      .setAutoConfig("m_myService2")
            });
View Full Code Here

@RunWith(PaxExam.class)
public class CustomDependencyTest extends TestBase {
    @Test
    public void testCustomDependency() {
        Ensure e = new Ensure();
        DependencyManager dm = new DependencyManager(context);
       
        // create a toggle that can be used to turn on/off our custom dependency
        Toggle toggle = new Toggle();
       
        // create a service that has our custom dependency as its only dependency
        dm.add(dm.createComponent()
            .setImplementation(new ServiceImpl(e))
            .add(new CustomDependency(toggle))
            );
       
        // make the toggle, therefore the dependency, therefore the service available
View Full Code Here

@RunWith(PaxExam.class)
public class FELIX3008_FilterIndexStartupTest extends TestBase {
    @Test
    public void testNormalStart() throws Exception {
        System.setProperty("org.apache.felix.dependencymanager.filterindex", "objectClass");
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a provider
        Provider provider = new Provider();
        // activate it
        Component p = m.createComponent()
            .setInterface(Service.class.getName(), null)
            .setImplementation(provider);
       
        Consumer consumer = new Consumer(e);
        Component c = m.createComponent()
            .setImplementation(consumer)
            .add(m.createServiceDependency()
                .setService(Service.class)
                .setRequired(true)
                );
       
        m.add(p);
        m.add(c);
        e.waitForStep(1, 5000);
        m.remove(p);
        e.waitForStep(2, 5000);
        m.remove(c);
       
        Assert.assertEquals("Dependency manager bundle should be active.", Bundle.ACTIVE, context.getBundle().getState());
    }
View Full Code Here

@RunWith(PaxExam.class)
public class AbstractServiceDependencyTest extends TestBase {

   @Test
   public void testAbstractClassDependency() {
       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()
           .setInterface(ServiceAbstract.class.getName(), null)
           .setImplementation(new ServiceProvider(e))
           ;
       Component sc = m.createComponent()
           .setImplementation(new ServiceConsumer(e))
           .add(m.createServiceDependency()
               .setService(ServiceAbstract.class)
               .setRequired(true)
               .setCallbacks("bind", "unbind")
               );
       m.add(sp);
       m.add(sc);
       m.remove(sp);
       // ensure we executed all steps inside the component instance
       e.step(8);
   }
View Full Code Here

    public void testAdapterWithAspectMultipleTimes() {
        // TODO this test is broken, it assumes that the order in which listeners are added to the BundleContext will also
        // be the order in which they're invoked (which from a spec point of view is not true)
       
       
        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 provider = m.createComponent()
            .setImplementation(new ServiceProvider())
            .setInterface(OriginalService.class.getName(), null);

        Component consumer = m.createComponent()
            .setImplementation(new ServiceConsumer(e))
            .add(m.createServiceDependency()
                .setService(AdaptedService.class)
                .setCallbacks("add", null, "remove", "swap")
            );
        Component adapter = m.createAdapterService(OriginalService.class, null, "add", null, "remove", "swap")
            .setInterface(AdaptedService.class.getName(), null)
            .setImplementation(new ServiceAdapter(e,1));
       
        Component adapter2 = m.createAdapterService(OriginalService.class, null, "add", null, "remove", "swap")
            .setInterface(AdaptedService.class.getName(), null)
            .setImplementation(new ServiceAdapter(e,2));
       
        Component aspect = m.createAspectService(OriginalService.class, null, 10, null)
            .setImplementation(ServiceAspect.class);
       
        m.add(provider);

        int stepsInLoop = 10;
        int loops = 10;
        for (int loop = 0; loop < loops; loop++) {
            int offset = stepsInLoop * loop;
           
            System.out.println("add adapter");
            m.add(adapter);
            System.out.println("add consumer");
            m.add(consumer);
            e.waitForStep(1 + offset, 5000);
            System.out.println("add aspect");
            m.add(aspect);
            // a swap is expected on the adapter
            e.waitForStep(2 + offset, 5000);
            System.out.println("add adapter2");
            m.add(adapter2);
            // another aspect adapter will appear
            e.waitForStep(4 + offset, 5000);
            System.out.println("remove provider");
            m.remove(provider);
            // two times:
            // the aspect adapter will disappear
            // the original adapter will (briefly) appear
            // the original adapter will disappear
           
            // TODO the test will fail somewhere here most of the time
           
            e.waitForStep(8 + offset, 5000);
            System.out.println("remove consumer");
            m.remove(consumer);
           
            // nothing should happen, all consumed services were already gone
            System.out.println("add provider");
            m.add(provider);
            // still nothing should happen
            System.out.println("remove adapter");
            m.remove(adapter);
            System.out.println("remove adapter2");
            m.remove(adapter2);
            System.out.println("remove aspect");
            m.remove(aspect);
        }
        m.remove(provider);
        e.waitForStep(stepsInLoop * loops, 5000);
    }
View Full Code Here

@RunWith(PaxExam.class)
public class AspectDynamicsTest extends TestBase {
    @Test
    public void testDynamicallyAddAndRemoveAspect() {
        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 provider = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
        Component provider2 = m.createComponent().setImplementation(new ServiceProvider2(e)).setInterface(ServiceInterface2.class.getName(), null);
        Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true));
        Component aspect = m.createAspectService(ServiceInterface.class, null, 1, null).setImplementation(new ServiceAspect(e));
       
        m.add(consumer);
        m.add(provider);
        // the consumer should invoke the provider here, and when done, arrive at step 3
        // finally wait for step 6 before continuing
        e.waitForStep(3, 15000);
       
        m.add(aspect);
        // after adding the aspect, we wait for its init to be invoked, arriving at
        // step 4 after an instance bound dependency was added (on a service provided by
        // provider 2)
        e.waitForStep(4, 15000);
       
        m.add(provider2);
       
        // after adding provider 2, we should now see the aspect being started, so
        // we wait for step 5 to happen
        e.waitForStep(5, 15000);
       
        // now we continue with step 6, which will trigger the next part of the consumer's
        // run method to be executed
        e.step(6);
       
        // invoking step 7, 8 and 9 when invoking the aspect which in turn invokes the
        // dependency and the original service, so we wait for that to finish here, which
        // is after step 10 has been reached (the client will now wait for step 12)
        e.waitForStep(10, 15000);
       
        m.remove(aspect);
        // removing the aspect should trigger step 11 (in the stop method of the aspect)
        e.waitForStep(11, 15000);
       
        // step 12 triggers the client to continue
        e.step(12);
       
        // wait for step 13, the final invocation of the provided service (without aspect)
        e.waitForStep(13, 15000);
       
        // clean up
        m.remove(provider2);
        m.remove(provider);
        m.remove(consumer);
    }
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.