Package org.apache.felix.dm

Examples of org.apache.felix.dm.DependencyManager


        iterator.next();
    }

    @Test
    public void testConcurrentGetComponentsMultipleThreads() {
        final DependencyManager m = new DependencyManager(context);
        final AtomicInteger errors = new AtomicInteger(0);
        final AtomicInteger componentsAdded = new AtomicInteger(0);
        final int max = 10000;
        final AtomicBoolean isRunning = new AtomicBoolean(true);

        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
        Runnable readTask = new Runnable() {
            public void run() {
                while (isRunning.get()) {
                    try {
                        List components = m.getComponents();
                        for (Object component : components) {
                            // Just iterating the components should check for concurrent modifications
                        }
                    }
                    catch (Exception ex) {
                        errors.addAndGet(1);
                        ex.printStackTrace();
                    }
                }
            }
        };

        Callable<Boolean> modifyTask = new Callable<Boolean>() {
            public Boolean call() throws Exception {
                try {
                    m.add(m.createComponent().setImplementation(Object.class));
                    componentsAdded.addAndGet(1);
                    return true;
                }
                catch (Exception ex) {
                    return false;
View Full Code Here


@RunWith(PaxExam.class)
public class ResourceAdapterDependencyAddAndRemoveTest2 extends TestBase {
    @Test
    public void testBasicResourceAdapter() 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
        Properties props = new Properties();
        props.setProperty("id", "1");
        m.add(m.createComponent()
        .setInterface(ServiceInterface.class.getName(), props)
        .setImplementation(new ServiceProvider(e))
    );
       
        props = new Properties();
        props.setProperty("id", "2");
        m.add(m.createComponent()
        .setInterface(ServiceInterface.class.getName(), props)
        .setImplementation(new ServiceProvider(e))
    );
       
        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
        ResourceAdapter service = new ResourceAdapter(e);
        CallbackInstance callbackInstance = new CallbackInstance(e);
        Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed")
            .setImplementation(new ResourceAdapter(e))
            .setCallbacks(callbackInstance, "init", "start", "stop", "destroy")
            .add(m.createServiceDependency()
            .setService(ServiceInterface.class, "(id=1)")
            .setRequired(true)
            .setInstanceBound(true)
        );
        component.addStateListener(new ComponentStateListenerImpl(e));
        m.add(component);
        // wait until the single resource is available
        e.waitForStep(1, 5000);
        // trigger a 'change' in our resource
        provider.change();
        // wait until the changed callback is invoked
        e.waitForStep(2, 5000);
       
        System.out.println("Done!");
        m.clear();
     }
View Full Code Here

@RunWith(PaxExam.class)
public class ConfigurationDependencyTest extends TestBase {
    @Test
    public void 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 ConfigurationConsumer(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 FELIX2696_ConfigurationAndServiceDependencyTest extends TestBase {
    @Test
    public void 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 ConfigurationConsumer(e))
            .add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true))
            .add(m.createConfigurationDependency().setPid("test"));
        Component s2 = m.createComponent()
            .setImplementation(new ConfigurationCreator(e))
            .add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true));
        Component s3 = m.createComponent()
            .setInterface(ServiceInterface.class.getName(), null)
            .setImplementation(DependentServiceProvider.class);
        m.add(s1);
        m.add(s2);
        m.add(s3);
        e.waitForStep(2, 5000);
        m.remove(s3);
        m.add(s3);
        // after adding the required dependency again, the issue in FELIX-2696 means that the
        // updated() method is not invoked for the new instance, and init() is, so our step
        // count will only go up to 3 (not 4) causing this test to fail
        e.waitForStep(4, 5000);
        m.remove(s3);
        m.remove(s2);
        m.remove(s1);
    }
View Full Code Here

        }
    }
   
    @Test
    public void testComponentWithRequiredUpdatedConfigurationAndServicePropertyPropagation() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();       
        ConfigurationCreator confCreator = new ConfigurationCreator();
        Component s1 = m.createComponent()
                .setImplementation(new S1(e))
                .setInterface(S1.class.getName(), null)
                .add(m.createConfigurationDependency()
                     .setPid("test")
                     .setPropagate(true));
        Component s2 = m.createComponent()
                .setImplementation(new S2(e))
                .add(m.createServiceDependency()
                     .setService(S1.class, ("(testkey=testvalue)"))
                     .setRequired(true)
                     .setCallbacks("add", "change", null));
        Component s3 = m.createComponent()
                .setImplementation(confCreator)
                .add(m.createServiceDependency()
                     .setService(ConfigurationAdmin.class)
                     .setRequired(true));

        m.add(s1);
        m.add(s2);
        m.add(s3);
        e.waitForStep(2, 15000);
        confCreator.update();
        e.waitForStep(4, 15000);
        m.remove(s1);
        m.remove(s2);
        m.remove(s3);
    }
View Full Code Here

        }                   
    }
   
    @Test
    public void testAdapterWithChangedInstanceBoundDependency() {
        DependencyManager m = new DependencyManager(context);
        Ensure e = new Ensure();

        Component a = m.createComponent()
                .setImplementation(new AImpl())
                .setInterface(A.class.getName(), null);
       
        Component b = m.createComponent()
                .setInterface(B.class.getName(), null)
                .setImplementation(new BImpl(e))
                .add(m.createBundleDependency()
                        .setFilter("(Bundle-SymbolicName=org.apache.felix.dependencymanager.shell)")
                        .setStateMask(Bundle.INSTALLED|Bundle.ACTIVE|Bundle.RESOLVED|Bundle.STARTING)
                        .setRequired(true)
                        .setCallbacks("add", "change", "remove"));
                   
        Bundle dmtest = getBundle("org.apache.felix.dependencymanager.shell");
        try {
            dmtest.stop();
        } catch (BundleException e1) {
            Assert.fail("couold not find dependencymanager shell bundle");
        }
       
        m.add(a);
        m.add(b);
       
        e.waitForStep(4, 5000);       
        m.remove(a); // B will loose A and will enter into "waiting for required (instantiated)" state.
        System.out.println("Starting dependency manager shell ...");       
        try {
            dmtest.start();
        } catch (BundleException e1) {
            Assert.fail("could not start dependencymanager shell bundle");
        }
        e.waitForStep(7, 5000);    
        m.remove(b);       
        e.waitForStep(10, 5000);               
    }
View Full Code Here

            m_e.step(1);
        }

        void init(Component c) {
            m_e.step(2);
            DependencyManager dm = c.getDependencyManager();
            c.add(dm.createServiceDependency().setService(A.class).setRequired(true).setInstanceBound(true).setCallbacks("add", "remove"));
        }     
View Full Code Here

@RunWith(PaxExam.class)
public class ServiceDependencyComponentLifeCycleTest extends TestBase {
    @Test
    public void testComponentLifeCycleWhenAddingAndRemovingDependencies() 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
       
        Component component = m.createComponent().setInterface(MyService2.class.getName(), null).setImplementation(new MyComponent(e));
        ServiceDependency dependency = m.createServiceDependency().setService(MyService.class).setRequired(true);
        ServiceDependency dependency2 = m.createServiceDependency().setService(MyService.class).setRequired(true);
        ServiceTracker st = new ServiceTracker(context, MyService2.class.getName(), null);
        st.open();
        Component component2 = m.createComponent().setInterface(MyService.class.getName(), null).setImplementation(new MyImpl(e));
       
        // add the component: it has no dependencies so it should be activated immediately
        m.add(component);
        Assert.assertNotNull("service should be available", st.getService());
               
        // add a required dependency that is not available, so the component should be deactivated
        component.add(dependency);
        Assert.assertNull("service should no longer be available", st.getService());
        // remove the dependency again, so the component should be activated again
        component.remove(dependency);
        Assert.assertNotNull("service should be available", st.getService());
        // make the dependency instance bound
        dependency.setInstanceBound(true);
       
        // add it again, the component was already active so even though the dependency
        // is required, the component will *NOT* go through the destroy life cycle methods
        component.add(dependency);
        Assert.assertNull("service should no longer be available", st.getService());
        component.remove(dependency);
        Assert.assertNotNull("service should be available", st.getService());
       
        // make the second dependency instance bound too
        dependency2.setInstanceBound(true);
       
        // activate the service we depend on
        m.add(component2);
        // init and start should be invoked here, so wait for them to complete
        e.waitForStep(10, 5000);
       
        component.add(dependency);
        Assert.assertNotNull("service should be available", st.getService());
        component.add(dependency2);
        Assert.assertNotNull("service should be available", st.getService());
        component.remove(dependency);
        Assert.assertNotNull("service should be available", st.getService());
       
        e.step(11);
       
        // remove the service again, the component still has an instance bound
        // dependency on it, so stop() should be invoked, but the component
        // should not be destroyed
        m.remove(component2);
        e.step(15);
        Assert.assertNull("service should no longer be available", st.getService());
        component.remove(dependency2);
        Assert.assertNotNull("service should be available", st.getService());
        m.remove(component);
        e.step(19);
    }
View Full Code Here

    /**
     * Checks if an extra optional/required dependency is properly injected into a consumer, using callbacks.
     */
    @Test
    public void testExtraDependencyWithCallback() {
        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 sp = m.createComponent().setInterface(ProviderInterface.class.getName(), null).setImplementation(ProviderImpl.class);
        Component sc = m.createComponent().setImplementation(new Client(e, false, 1));
        Component sc2 = m.createComponent().setImplementation(new Client(e, true, 5));
        Component sc3 = m.createComponent().setImplementation(new Client(e, true, 9));
       
        // add the provider first, then add the consumer which initially will have no dependencies
        // but via the init() method an optional dependency with a callback method will be added
        m.add(sp);
        m.add(sc);
        // remove the consumer again
        m.remove(sc);
        e.waitForStep(4, 5000);

        // next up, add a second consumer, identical to the first, but with a required dependency
        // with a callback method which will be added in the init() method
        m.add(sc2);
        // remove the consumer again
        m.remove(sc2);
        e.waitForStep(8, 5000);

        // now remove the provider, add a third consumer, identical to the second, and after the
        // consumer has started, add the provider again
        m.remove(sp);
        m.add(sc3);
        m.add(sp);
        e.waitForStep(12, 5000);
        m.clear();
    }
View Full Code Here

            m_required = required;
            m_startStep = startStep;
        }

        public void init(Component s) {
            DependencyManager dm = s.getDependencyManager();
            m_ensure.step(m_startStep);
            s.add(dm.createServiceDependency()
                .setInstanceBound(true)
                .setService(ProviderInterface.class)
                .setRequired(m_required)
                .setCallbacks("bind", null));
        }
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.