Programmatically Start OSGi (Equinox)?

前端 未结 3 1889
野性不改
野性不改 2020-11-28 04:39

I\'d like to be able to easily start an OSGi framework (preferably Equinox) and load up any bundles listed in my pom from a java main.

Is this possible? If so, how?

3条回答
  •  囚心锁ツ
    2020-11-28 04:57

    Any OSGi framework (R4.1 or later) can be started programmatically using the FrameworkFactory API:

    ServiceLoader ffs = ServiceLoader.load(FrameworkFactory.class);
    FrameworkFactory ff = ffs.iterator().next();
    Map config = new HashMap();
    // add some params to config ...
    Framework fwk = ff.newFramework(config);
    fwk.start();
    

    The OSGi framework is now running. Since Framework extends Bundle you can call getBundleContext and call all of the normal API methods to manipulate bundles, register services, etc. For example

    BundleContext bc = fwk.getBundleContext();
    bc.installBundle("file:/path/to/bundle.jar");
    bc.registerService(MyService.class.getName(), new MyServiceImpl(), null);
    // ...
    

    Finally you should simply wait for the framework to shutdown:

    fwk.stop();
    fwk.waitForStop(0);
    

    To reiterate, this approach works for any OSGi framework including Equinox and Felix just by putting the framework JAR on the classpath.

提交回复
热议问题