简体   繁体   中英

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. 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?

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
  • 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(...)

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.

On the end you should call refresh() to have the correct package wirings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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