How can I use BundleWiring to lookup Bundle/Class relationships (previously done via PackageAdmin)?
I'm in the process of upgrading my application the latest release of Eclipse Equinox 3.7 and associated libraries. I'm excited to have the generic support in OSGi 4.3 and digging the new Bundle.adapt() concepts. I've run into one stumbling block.
Previously, I've used PackageAdmin.getBundle(Class) to find out which bundle loaded which classes. I have to do this due to some RMI object serialization usage.
QUESTION: Is there an way to accomplish the same result, ma开发者_如何学Pythonpping Class to Bundle, using the new BundleWiring API?
I realize that PackageAdmin is probably not going away anytime soon, but I hate deprecation warnings.
Kinda embarrassed that I didn't find this the first time I looked through the document. Answering my own question for completeness.
From the core specification PDF ...
Section 3.9.9 - Finding an Object’s Bundle
There are scenarios where a bundle is required in code that has no access to a Bundle Context. For this reason, the framework provides the following methods:
Framework Util – Through the FrameworkUtil class with the getBundle(Class) method. The framework provides this method to allow code to find the bundle of an object without having the permission to get the class loader. The method returns null when the class does not originate from a bundle.
Class Loader – An OSGi framework must ensure that the class loader of a class that comes from a bundle implements the BundleReference interface. This allows legacy code to find an object’s bundle by getting its class loader and casting it to a BundleReference object, which provides access to the Bundle. However, this requires the code to have the permission to access the class loader. The following code fragment shows how to obtain a Bundle object from any object.
ClassLoader cl = target.getClassLoader();
if ( cl instanceof BundleReference ) {
BundleReference ref = (BundleReference) cl;
Bundle b = ref.getBundle();
...
}
In an OSGi system, not all objects belong to the framework. It is therefore possible to get hold of a class loader that does not implement the BundleReference interface, for example the boot class path loader.
精彩评论