質問

I have 2 iPOJO Components.

1- A Provider bundle that provides "Hello" service. Below is the implementation of the component:

package helloipojo;


import helloipojo.service.HelloService;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Validate;


@Component(name="my-factory")
@Provides
public class HelloServiceImpl implements HelloService{

    @Override
    public void sayHello() {

        System.out.println("Hello iPojo!");

    }


    @Validate
    public void start() throws Exception {

        System.out.println("Hello, I am ipojo bundle start method");

    }

    @Invalidate
    public void stop() throws Exception {

        System.out.println("Bye Bye, I am ipojo bundle stop method");

    }



}

2- Consumer bundle that uses HelloService object as the follwing:

 package helloserviceconsumer;

import helloipojo.service.HelloService;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;

@Component(name="my-consumer-factory")
public class HelloConsumer {
              @Requires
              HelloService helloObject;

              @Validate
              private void start() {
                       // Starting method
                       //...
                       helloObject.sayHello();
                       //...
                }

                @Invalidate
                protected void stop() {
                        // Stopping method
                        if(helloObject!=null) { helloObject.sayHello(); }

                        else System.out.println("hello service GONE!");
                }
}

In a seperate Java application, I load these two bundles and start them on Apache Felix as the following:

Bundle b = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloService_1.0.0.201401222235.jar");
b.start();

Bundle c = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloServiceConsumer_1.0.0.201401222257.jar");
c.start();

All the above works fine.

Now, I would like to instantiate these two components dynamically and observe the consumption of the bundle provider service by the bundle consumer. I used Instance Declaration, as the following:

DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
                            providerDeclaration.start();

DefaultInstanceDeclaration consumerDeclaration = new DefaultInstanceDeclaration(c.getBundleContext(), "my-consumer-factory");
                            consumerDeclaration.start();

No errors when running the application. However, I could not see the "Hello" Messages that exists in the start() methods of both the service provider and consumer. I see absolutely NOTHING. That means the components are not instantiated correctly. Where did I go wrong? Thanks.

Update

I found out that I did not apply iPOJO manipulation on my bundle, so I did that using iPOJO Ant Task, as the following:

<project>
<target name="main">
    <!-- Change the path to point on the iPOJO Ant task jar-->
    <taskdef name="ipojo"
        classname="org.apache.felix.ipojo.task.IPojoTask"
        classpath="C:/Users/zaid.almahmoud/feasibility-codes/ipojo/ipojo-distribution-1.11.0/bundle/org.apache.felix.ipojo.ant-1.11.0.jar"/>
    <ipojo
        input="C:/Users/zaid.almahmoud/Desktop/plugins/HelloService_1.0.0.201401222235.jar"
        output="C:/Users/zaid.almahmoud/Desktop/plugins/Manipulated_HelloService.jar"   
    />
</target>
</project>

Now, I can see in my app that the factory is valid. This is the output in the command:

g! ipojo:factories
Factory my-factory (VALID)
Factory org.apache.felix.ipojo.arch.gogo.Arch (UNKNOWN) - Private

Therefore the factory "my-factory" is available unlike before.

However, my instance is not available, which was created as the following:

DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
                        providerDeclaration.start();

Again, this does not show an error, but it doesn't display the expected output in the start() method of the bundle, and on the command, I can see that:

g! ipojo:instance my-factory-0
Instance named 'my-factory-0' not found
g! ipojo:instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -> valid

Can you help please? Thanks.

役に立ちましたか?

解決

Using Instance Declaration did not work for me. Though, I was able to instantiate my component using Factory Service, as the following:

ServiceReference[] references = context.getServiceReferences(Factory.class.getName(),"(factory.name=my-factory)");

    if (references == null)
    System.out.println("No reference");


    else {

          System.out.println(references[0].toString());

          Factory factory =  context.getService(references[0]);
          x = factory.createComponentInstance(null); //here instantiating my component
          x.start(); //this starts my component service and executes start method


          System.out.println(x.getState());
          System.out.println(x.getInstanceName());


          x.dispose(); //this stops my component service and executes stop method

         }

I got this code to work only after placing it into a bundle which will be loaded by my java app.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top