Loading of OSGi bundle dynamically from a file system

旧时模样 提交于 2019-11-29 02:45:12

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!