从文件系统动态加载OSGi捆绑包 [英] Loading of OSGi bundle dynamically from a file system

查看:96
本文介绍了从文件系统动态加载OSGi捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用OSGi框架的模块化应用程序.在这里,我正在使用org.eclipse.equinox.common_3.4.0 OSGi容器.因此,现在该应用程序已经在安装了所有osgi捆绑软件且处于活动状态的情况下运行,并且我将基于某些操作循环遍历哈希映射,从而在UI上显示所有活动的OSGi捆绑软件. 现在的要求是,在应用程序已经运行的同时,我想从文件系统中安装一个新的OSGi软件包,方法是将该新软件包提供给应用程序的OSGi容器,以便它将启动该软件包.

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 the osgi bundles installed and active and I am displaying all the active OSGi bundles on the UI, by looping though a hash map, based on some action. Now the requirement is, while the application is already running, I want to instal a new OSGi bundle, from a file system, by giving this new bundle to the application's OSGi container so that it will start this bundle.

我该如何实现? 我尝试将OSGi捆绑包作为JarInputstream读取,并阅读捆绑包激活器的完全限定的类路径,并尝试使用Class.forName(")实例化此类型,并将其类型转换为BundleActivator接口.但是在启动它时,它将捆绑包上下文作为启动方法的参数.

How do I achieve this ? I have tried reading the OSGi bundle as a JarInputstream and read the bundle activator fully qualified class path and tried to instantiate this using Class.forName("") and type casted to BundleActivator interface. But while starting it, it is taking bundle context as a argument to start method.

在这种方式下,我可以实用地将OSGi捆绑软件提供给容器,以便它负责安装和启动捆绑软件,然后我的UI会在显示中自动选择这个新的捆绑软件名称.

Is there way where I can just give the OSGi bundle to the container pragmatically so that it will take care of installing and starting the bundle and then my UI will automatically picks up this new bundle name in the display.

推荐答案

假设您有要加载的文件,则可以像以下那样安装捆绑软件:

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();
}

您从激活或声明性服务组件的activate方法获取BundleContext.这些是推荐的方法,但在极端情况下,您也可以使用:

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

这篇关于从文件系统动态加载OSGi捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆