Loading of OSGi bundle dynamically from a file system

前端 未结 1 1741
情歌与酒
情歌与酒 2020-12-16 08:39

I have a modular application which uses OSGi framework. Here I\'m using org.eclipse.equinox.common_3.4.0 OSGi container. So now the application is already running with all t

相关标签:
1条回答
  • 2020-12-16 09:16

    Assuming you have the file to load, you can install the bundle like:

    void install( BundleContext context, File file) throws Exception {
        Bundle b = context.installBundle( file.toURI().toString() );
        b.start();
    }
    

    And you can uninstall it (if the file is gone):

    void uninstall( BundleContext context, File file) throws Exception {
        Bundle b = context.getBundle( file.toURI().toString() );
        b.uninstall();
    }
    

    You get the BundleContext from your activate or Declarative services component's activate method. These are the recommended methods but in dire cases you can also use:

    BundleContext context = FrameworkUtil.getBundle( this.getClass() ).getBundleContext();
    

    Though handy it bypasses some mechanism that you might want to use in the future so getting the context in the recommended way is much better

    0 讨论(0)
提交回复
热议问题