How to get classloader for a bundle in equinox?

和自甴很熟 提交于 2019-11-30 07:08:18

问题


I have read a lot of equinox code for this, but still can't figure out a non-hacky way of getting the classloader for a osgi bundle in eclipse equinox setup. Is there one?


回答1:


The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. However the Bundle interface exposes a loadClass() method and this would allow you to write a classloader that wraps the bundle API and delegates to that loadClass() method. Or you can save some time and use Spring DM's BundleDelegatingClassLoader class instead.




回答2:


In OSGi 4.3 you can use:

bundle.adapt(BundleWiring.class).getClassLoader()



回答3:


The class loader of a bundle can be obtained through the BundleWiring interface. Here a short example:

Bundle bundle = bundleContext.getBundle();
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
ClassLoader classLoader = bundleWiring.getClassLoader();



回答4:


In normal java code, you can get the class loader that loaded a given object with

object.getClass().getClassLoader();

Or even just

SomeType.class.getClassLoader();

The same applies to Equinox, just use an object or type that comes from the bundle you are interested in.



来源:https://stackoverflow.com/questions/1503304/how-to-get-classloader-for-a-bundle-in-equinox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!