Search code examples
javaosgiosgi-bundle

How to clean OSGi bundles cache at runtime


I have following sample scenario ... (please note that I can implement the scenario using services but I'm trying some things)

An OSGi bundle with an Activator class (name it "client") and another OSGi bundle that is a simple library (name it "server") exposing a method named callMe(). In the start() method of the "client" there is a call to the callMe() method of the "server". Of course the "server" bundle exposes the package where we have the class with callMe() method and the "client" bundle has it as imported package. Starting the OSGi framework without the two bundles installed, I install the "client" bundle first and when I try to execute start() method the following error is shown :

org.osgi.framework.BundleException: Unresolved constraint in bundle org.example.helloworld [4]: Unable to resolve 4.0: missing requirement [4.0] osgi.wiring.package; (osgi.wiring.package=org.example.helloworldlib)

It's right because the "server" bundle isn't neither installed nor resolved. Now, I install "server" bundle and recall start() on "client" : this time all works fine because the "server" bundle is installed so the "client" can be resolved, activated and it can call callMe() method on "server".

After that I uninstall the "server" bundle leaving the "client". The strange thing is now ... stop the "client" and re-start() it ... the "client" works ! It's able to call the callMe() method of "server" even if I uninstalled it before ! I know that OSGi framework has a bundle cache so it seems that "server" bundle is in the cache but why it's not showed in the bundles list ?

Is it possible to clean the cache at runtime ?

Paolo.


Solution

  • OSGi builds the wiring to needed packages when a bundles goes to the state resolved. It then keeps these wirings until refresh is called.

    So it is completely normal that the client keeps working when you uninstall the server bundle.

    When you call refresh the resolve step is done again. So at this point the client fails to resolve. As far as I know the wiring is not cached on disk.

    So if you uninstall server and restart the framework client should also fail to resolve.