Domanda

How can I create a IPOJO app like Java app that can be opened in double click?

I have some code:

//App.java
package app;
import app.testipojo.HelloComponent;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.framework.FrameworkFactory;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.apache.felix.main.AutoProcessor;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;


@Component
@Instantiate
public class HelloComponentApp {

@Requires
HelloComponent c;
public HelloComponentApp() {
}


    @Validate
    public void start(){
        c.test();
    }

    public static void main(String args[]) throws BundleException, InterruptedException{




    FrameworkFactory ff = new FrameworkFactory ();
    Map<String,Object> config;
        config = new HashMap<>();

         config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,"org.osgi.service.log;version=1.3, org.apache.felix.ipojo.architecture;version=1.11.0, org.apache.felix.ipojo;version=1.11.0,"+
                 "org.osgi.service.cm;version=1.2,"+"app.testipojo;version=1.0.0.SNAPSHOT");

     config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");

    Framework fwk = ff.newFramework(config);
    fwk.start();
    BundleContext context = fwk.getBundleContext();


      String home_dir="file:/G:/HOW_TO_PRONOUNCE/install/jar/";
   AutoProcessor.process(config, context);
     Bundle bundle = context.installBundle(home_dir+"testipojo/target/testipojo-1.0-SNAPSHOT.jar");
     bundle.start();

     System.out.println("Started");

        bundle.stop();


        fwk.stop();
        fwk.waitForStop(1000);

        }
}

IPOJO bundle to start

package app.testipojo;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;

@Component
@Instantiate
public class HelloComponent {

public HelloComponent() {

}

    public void test(){
        System.out.println("Hello world!");
    }
}

It runs without any error, but it prints only 'started.' It does not print 'Hello world'. Please help me to solve this issue.

È stato utile?

Soluzione

I believe that you will have to have an osgi context to run the application from. See https://ilikeorangutans.github.io/2012/10/23/osgi-bootstrapping/ for different ways of bootstrapping a felix instance.

Once you have the felix instance running then you would install and start the ipojo bundles and your component will run.

So in summary what needs to happen when you double click on the link is to setup the environment for ipojo to run in (The osgi container: felix, equinox etc), then install your application bundle. Once that happens and everything resolves the container will call start on your component.

And then after really reading the code :) Take a look at http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html. I think you are missing somethings that will help in this process.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top