1 Replies - 838 Views - Last Post: 06 August 2009 - 08:43 PM Rate Topic: -----

#1 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Getting a JComponent from an OSGi Bundle

Posted 06 August 2009 - 04:59 PM

I've looked into OSGi and have used Apache Felix, Equinox and Knopflerfish but for this I'm using Apache Felix. I have bundle which starts Felix and starts itself. But I want it to load another bundle that returns a JComponent. I just can't seem to figure out how to do this, but here's my source:
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.apache.felix.main.AutoActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;

/**
 *
 * @author paul
 */
public class RunFelix implements BundleActivator {

    public static final String PLUGIN = "plugins";
    public static final String PREFIX = "file:plugins/";  // Saved Directory of plugin

    private BundleContext context = null;
    Bundle bun = null;

    public void start(BundleContext context) throws Exception {
        System.out.println("Trying to load JComponent...");
        this.context = context;
        bun = this.context.installBundle("[path to other bundle]");
        bun.start();
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye!");
        this.context = null;
    }

    public BundleContext getContext() {
        return this.context;
    }

    private static List getActivators() {
        List activators = new ArrayList();
        activators.add(new RunFelix());
        return activators;
    }

    private static File[] getBundles() {
        File dir = new File(RunFelix.PLUGIN);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return dir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".jar");
            }
        });
    }

    private static String getPluginNames() {
        File[] bundles = getBundles();
        StringBuilder sb = new StringBuilder("");
        try {
            for (int i = 0; i < bundles.length; i++) {
                String name = RunFelix.PREFIX + bundles[i].getName();
                System.out.println("Install plugin:" + name);
                sb.append(name).append(" ");  // Plugin path seperate with space
            }
        } catch (Exception ex) {
            System.out.println("Install plugins failured");
            ex.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        List activators = getActivators();
        String names = getPluginNames();
        //configMap.put(AutoActivator.AUTO_START_PROP, "file:plugins/Plugin-xxx.jar");
        Map configMap = new HashMap();
        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
            "org.osgi.framework; version=1.3.0," +
            "org.osgi.service.packageadmin; version=1.2.0," +
            "org.osgi.service.startlevel; version=1.0.0," +
            "org.osgi.service.url; version=1.0.0," +
            "org.osgi.util.tracker; version=1.3.2,"
        );
        configMap.put(AutoActivator.AUTO_START_PROP, names);
        configMap.put(Constants.FRAMEWORK_STORAGE, "configuration" + File.separator + "felix-cache");
        activators.add(new AutoActivator(configMap));
        configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, activators);
        Felix felix = null;
        try {
            felix = new Felix(configMap);
            felix.start();
        } catch (Exception ex) {
            System.err.println("Could not create framework:" + ex);
            ex.printStackTrace();
        }
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting a JComponent from an OSGi Bundle

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Getting a JComponent from an OSGi Bundle

Posted 06 August 2009 - 08:43 PM

not sure it will help (I am only an OSGi novice myself) but take a look at this
http://code.google.c...ncher/Main.java
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1