Search code examples
urlosgiosgi-bundle

How to obtain the referenced Bundle of a bundle-entry URL?


In an OSGi environment ClassLoader.getResource() typically returns a bundle-entry/bundle-resource URL (using a corresponding protocol). The OSGi specification chapter 3.9.6 - "Resource Loading" specifies only a few aspects how those URLs have to be constructed (for example the path) and leafs everything else to the implementation.

Is it possible, and if yes how, to obtain the Bundle to which a bundle-entry URL is pointing, using only implementation-independent OSGi API?

For example for a jar-URL one can open a connection, cast it to JarURLConnection and use JarURLConnection.getJarFile() to obtain the JarFile.


Solution

  • Is it possible, and if yes how, to obtain the Bundle to which a bundle-entry URL is pointing, using only implementation-independent OSGi API?

    Not in a specified way. The OSGi specification deliberately leaves URL details opaque to allow implementation freedom to the framework.

    For specific framework implementations, you may be able to discern the bundle id in the URL. But this is subject to change at the desires of the framework implementation.