简体   繁体   English

OSGI:从jar安装时更新已安装的捆绑软件?

[英]Osgi: update already installed bundle when installing from jar?

I implemented a feature that my osgi is installing bundles that are represented as jars on the filesystem programmatically when starting or when receiving a new one. 我实现了一项功能,即我的osgi在启动时或在接收新的捆绑包时以编程方式在文件系统上安装表示为jar的捆绑包。 However, when I receive one, that already has been installed during start-up it crashes due to the conflict. 但是,当我收到一个时,它在启动过程中已经安装,由于冲突而崩溃。 Is there a way (I don't know the ID nor the name) in case of conflict to update instead of install programmatically? 如果发生冲突而不是通过编程安装,是否有办法(我不知道ID或名称)?

BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Bundle bundle = context.getBundle("file:/Path.jar");

Hope somebody knows... 希望有人知道...

What I did in a similar case: 我在类似情况下的操作:

  • I wrote a BundleTracker that saved every bundle into a Map in addingBundle and removed it in removeBudnle 我编写了一个BundleTracker,将每个捆绑包保存在addBundle中的Map中,并在removeBudnle中将其删除
  • When I wanted to install a bundle programmatically, I checked if a bundle with the same symbolic name already existed and if it did I used the bundle.update(inputStream) method instead of using bundleContext.install(...) 当我想以编程方式安装捆绑软件时,我检查了是否存在具有相同符号名的捆绑软件,如果确实存在,则我使用bundle.update(inputStream)方法代替了bundleContext.install(...)

In case you want to allow multiple versions of the same bundle you can check also the version of the bundle before updating or installing. 如果要允许同一捆绑包的多个版本,则可以在更新或安装之前也检查捆绑包的版本。 In this case I would do the update if 在这种情况下,如果

  • the location of the new bundle is the same (if you use the correct path during first install) 新捆绑包的位置是相同的(如果在首次安装时使用正确的路径)
  • the version is the same 版本是一样的
  • the bundle is a singleton bundle (does not matter if the version is different) 捆绑包是单例捆绑包(版本不同无关紧要)

If you care about the location and it is changed, instead of update(inputstream) use a bundle.uninstall() and bundlecontext.install(...) which reinstalls the bundle. 如果您关心位置并且位置已更改,请使用bundle.uninstall()和bundlecontext.install(...)代替update(inputstream),它们会重新安装捆绑包。

On the end you should call refresh() to have the correct package wirings. 最后,您应该调用refresh()以具有正确的包装接线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM