Package org.apache.felix.dm

Examples of org.apache.felix.dm.Component


        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;
View Full Code Here


        /**
         * Method called from our superclass, when we need to create a service.
         */
        public Component createService(Object[] properties) {
            Dictionary settings = (Dictionary) properties[0];    
            Component newService = m_manager.createComponent();       
            Object impl = null;
           
            try {
                if (m_serviceImpl != null) {
                    impl = (m_serviceImpl instanceof Class) ? ((Class) m_serviceImpl).newInstance() : m_serviceImpl;
                }
                else {
                    impl = instantiateFromFactory(m_factory, m_factoryCreateMethod);
                }
                InvocationUtil.invokeCallbackMethod(impl, m_update,
                    new Class[][] {{ Dictionary.class }, {}},
                    new Object[][] {{ settings }, {}});
            }
           
            catch (Throwable t) {
               handleException(t);
            }

            // Merge adapter service properties, with CM settings
            Dictionary serviceProperties = getServiceProperties(settings);
            newService.setInterface(m_serviceInterfaces, serviceProperties);
            newService.setImplementation(impl);
            newService.setComposition(m_compositionInstance, m_compositionMethod); // if not set, no effect
            newService.setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy); // if not set, no effect
            configureAutoConfigState(newService, m_component);
           
            List dependencies = m_component.getDependencies();
            for (int i = 0; i < dependencies.size(); i++) {
                newService.add(((Dependency) dependencies.get(i)).createCopy());
            }
           
            for (int i = 0; i < m_stateListeners.size(); i ++) {
                newService.addStateListener((ComponentStateListener) m_stateListeners.get(i));
            }
           
            return newService;
        }
View Full Code Here

         * Method called from our superclass, when we need to update a Service, because
         * the configuration has changed.
         */
        public void updateService(Object[] properties) {
            Dictionary cmSettings = (Dictionary) properties[0];
            Component service = (Component) properties[1];
            Object impl = service.getService();
          
            try {
                InvocationUtil.invokeCallbackMethod(impl, m_update,
                    new Class[][] {{ Dictionary.class }, {}},
                    new Object[][] {{ cmSettings }, {}});
                if (m_serviceInterfaces != null && m_propagate == true) {
                    Dictionary serviceProperties = getServiceProperties(cmSettings);
                    service.setServiceProperties(serviceProperties);
                }
            }
           
            catch (Throwable t) {
                handleException(t);
View Full Code Here

            } else if (m_add == null && m_change == null && m_remove == null && m_swap == null) {
                // Since we have set callbacks, we must reactivate setAutoConfig because user has not specified any callbacks.
                aspectDependency.setAutoConfig(true);
            }
           
            Component service = m_manager.createComponent()
                .setInterface(serviceInterfaces, serviceProperties)
                .setImplementation(m_serviceImpl)
                .setFactory(m_factory, m_factoryCreateMethod) // if not set, no effect
                .setComposition(m_compositionInstance, m_compositionMethod) // if not set, no effect
                .setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy) // if not set, no effect
                .add(aspectDependency);
           
            configureAutoConfigState(service, m_component);
           
            for (int i = 0; i < dependencies.size(); i++) {
                service.add(((Dependency) dependencies.get(i)).createCopy());
            }

            for (int i = 0; i < m_stateListeners.size(); i++) {
                service.addStateListener((ComponentStateListener) m_stateListeners.get(i));
            }
            return service;               
        }
View Full Code Here

            Map services = super.getServices();
            Iterator it = services.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                ServiceReference originalServiceRef = (ServiceReference) entry.getKey();
                Component c = (Component) entry.getValue();
                // m_serviceProperties is already set to the new service properties; and the getServiceProperties will
                // merge m_serviceProperties with the original service properties.
                Dictionary newProps = getServiceProperties(originalServiceRef);               
                c.setServiceProperties(newProps);
            }
        }
View Full Code Here

    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
View Full Code Here

    @Test
    public void testServiceDependencyPropagate() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        Component c1 = m.createComponent()
                      .setImplementation(new C1(e))
                      .add(m.createServiceDependency().setService(C2.class).setRequired(true).setCallbacks("bind", null));

        Component c2 = m.createComponent()
                      .setInterface(C2.class.getName(), new Hashtable() {{ put("foo", "bar"); }})
                      .setImplementation(new C2())
                      .add(m.createServiceDependency().setService(C3.class).setRequired(true).setPropagate(true));

        Component c3 = m.createComponent()
                      .setInterface(C3.class.getName(), new Hashtable() {{ put("foo2", "bar2"); }})
                      .setImplementation(new C3());
       
        m.add(c1);
        m.add(c2);
View Full Code Here

    @Test
    public void testServiceDependencyPropagateCallback() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        Component c1 = m.createComponent()
                      .setImplementation(new C1(e))
                      .add(m.createServiceDependency().setService(C2.class).setRequired(true).setCallbacks("bind", null));

        C2 c2Impl = new C2();
        Component c2 = m.createComponent()
                      .setInterface(C2.class.getName(), new Hashtable() {{ put("foo", "bar"); }})
                      .setImplementation(c2Impl)
                      .add(m.createServiceDependency().setService(C3.class).setRequired(true).setPropagate(c2Impl, "getServiceProperties"));
       
        Component c3 = m.createComponent()
                      .setInterface(C3.class.getName(), null)
                      .setImplementation(new C3());
       
        m.add(c1);
        m.add(c2);
View Full Code Here

    public void testAdapterWithAspectMultipleTimes() {
        DependencyManager m = new DependencyManager(context);
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();

        Component aspect1 = m.createAspectService(OriginalService.class, null, 10, null)
              .setImplementation(ServiceAspect.class);
         
      Component aspect2 = m.createAspectService(OriginalService.class, null, 15, null)
              .setImplementation(ServiceAspect.class);
     
        Component adapter = m.createAdapterService(OriginalService.class, null, "add", null, "remove", "swap")
              .setInterface(AdaptedService.class.getName(), null)
              .setImplementation(ServiceAdapter.class);
       
        m.add(adapter);
        m.add(aspect1);
       
        List originals = new ArrayList();
        List consumers = new ArrayList();
        int count = 100;
        for (int i = 0; i < count; i++) {
          // create a service provider and consumer
          Dictionary props = new Hashtable();
          props.put("number", "" + i);
          Component original = m.createComponent()
              .setImplementation(new ServiceProvider("" + i, e))
              .setInterface(OriginalService.class.getName(), props);
         
          Component consumer = m.createComponent()
              .setImplementation(new ServiceConsumer(e, "" + i))
              .add(m.createServiceDependency()
                  .setService(AdaptedService.class, "(number=" + i + ")")
                  .setCallbacks("add", null, "remove", "swap")
                  .setRequired(true)
View Full Code Here

        // helper class that ensures certain steps get executed in sequence
        m_ensure = new Ensure();
       
        // Create a Configuration instance, which will create/update/remove a configuration for factoryPid "MyFactoryPid"
        ConfigurationCreator configurator = new ConfigurationCreator("MyFactoryPid", "key", "value1");
        Component s1 = m.createComponent()
            .setImplementation(configurator)
            .add(m.createServiceDependency()
                .setService(ConfigurationAdmin.class)
                .setRequired(true));

        // Create an Adapter that will be instantiated, once the configuration is created.
        // This Adapter provides an AdapterService, and depends on an AdapterExtraDependency service.
        Component s2 = m.createFactoryConfigurationAdapterService("MyFactoryPid", "updated", true /* propagate CM settings */)
                      .setInterface(AdapterService.class.getName(), new Properties() {{ put("foo", "bar"); }})
                      .setImplementation(Adapter.class);

        s2.add(m.createServiceDependency()
            .setService(AdapterExtraDependency.class)
            .setRequired(true)
            .setAutoConfig(true));
       
        // Create extra adapter service dependency upon which our adapter depends on.
        Component s3 = m.createComponent()
            .setImplementation(new AdapterExtraDependency())
            .setInterface(AdapterExtraDependency.class.getName(), null);
       
        // Create an AdapterService Consumer
        Component s4 = m.createComponent()
            .setImplementation(AdapterServiceConsumer.class)
            .add(m.createServiceDependency()
                .setService(AdapterService.class)
                .setRequired(true)
                .setCallbacks("bind", "change", "remove"));
View Full Code Here

TOP

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

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.