Search code examples
javaosgiapache-felixosgi-bundle

Update Apache Felix Bundles during framework start up in Java


So I have this code which I call to update bundles in Apache Felix Framework during start up, but I only update bundles that I purposely set to level 7 and greater. Whenever this code is executed, the updated bundle is remove from the list and the console hangs. Other times, the updated bundle's status is ACTIVE but the console also hangs.

Arrays.stream(bundleContext.getBundles())
            .filter((bundle) ->  return bundle.adapt(BundleStartLevel.class).getStartLevel() > 6;
            .forEach((Bundle bundle) -> {
                try {
                    this.logger.log(LOG_DEBUG, "updating : " + bundle.getSymbolicName());
                    if (bundle.getState() == Bundle.ACTIVE) {
                        bundle.stop();
                    }
                    bundle.update();
                    bundle.start(Bundle.ACTIVE);
                } catch (BundleException exception) {
                    this.logger.log(LOG_ERROR, "Bundle update for " + name + " failed.", exception);
                }
            });

Is there a proper way to update bundles in Apache Felix Framework?


Solution

  • From the description you give and code example it is impossible to tell exactly what goes wrong:

    • When is this code exactly invoked?
    • You say in your description only bundles that have a startlevel of 7 and greater are updated. I don't see that in the code anywhere. In fact it only updates bundles which have a symbolic name that equals name.
    • There is no need to invoke bundle.stop() and bundle.start() at all when doing the update. Furthermore, that logic in this code is flawed: if a bundle was not active before it will become so now.
    • If this code is invoked from a start method of a `BundleActivator, it might try to update itself, which leads to all kinds of unpredictable behaviour.