Search code examples
javaipojo

Update IPOJO component implementation


I have another problem relating to dynamic update in iPOJO:

1. Problem:

  • Suppose that I have 2 components A and B. These two components are implemented by A_impl.java and B_impl.java, respectively. They implement two services A_service and B_service.
  • the component “C” use the A_service declaring as following:

//

@Component(name="C", immediate=true)
@Instantiate
public class C_impl {
        @Requires
        A_Service service;
}

//

  • All three components are deployed and work fine.

2. Requirement:

  • Now, i would to update dynamically implementation of "C" by changing type of dependency (@Requires A_service service) in C_impl.java to (@Requires B_service service), i.e. it changes A_service to B_service in C_impl at runtime.

Question:

How can I develop AN INDEPENDENT COMPONENT to reconfigure (control) component implementation? I read (http://felix.apache.org/site/dive-into-the-ipojo-manipulation-depths.html) but I don’t understand well. Thanks in advance for your reply


Solution

  • Well, you want to change the component class of a component ? This will not really work.

    The only way would be to have the two implementation available without instance declared (no @instantiate) and create a component that required both Factories (org.apache.felix.ipojo.Factory service) and create the instances when needed. Obviously, if you need replacement, you would also need to dispose the first created instance when creating the second one.

    So, it would need a component like this (this is pseudo code):

    @Component(immediate=true)
    @Instantiate
    public class Controller {
    
        @Requires(filter="(factory.name=A)")
        Factory factoryOfA;
    
        @Requires(filter="(factory.name=B)")
        Factory factoryOfB;    
    
        ComponentInstance instance;
    
        @Validate
        public void createA() throws Exception {
            instance = factoryOfA.createInstance(null);
        }
    
        public void switchToB() throws Exception {
            instance.dispose();
            instance = factoryOfB.createInstance(null);
        }
    }