简体   繁体   English

如何在equinox中获取bundle的类加载器?

[英]How to get classloader for a bundle in equinox?

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. 我已经为此阅读了很多equinox代码,但仍然无法弄清楚在eclipse equinox设置中获取osgi包的类加载器的非hacky方法。 Is there one? 有吗?

在OSGi 4.3中,您可以使用:

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

The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. 简短的回答(当然对于OSGi 4.1,不确定4.2)是你无法得到一个bundle的类加载器。 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. 但是Bundle接口公开了一个loadClass()方法,这将允许您编写一个包装bundle API并委托给该loadClass()方法的类加载器。 Or you can save some time and use Spring DM's BundleDelegatingClassLoader class instead. 或者你可以节省一些时间并使用Spring DM的BundleDelegatingClassLoader类。

The class loader of a bundle can be obtained through the BundleWiring interface. 可以通过BundleWiring接口获取bundle的类加载器。 Here a short example: 这是一个简短的例子:

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

In normal java code, you can get the class loader that loaded a given object with 在普通的java代码中,您可以获取加载给定对象的类加载器

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. 这同样适用于Equinox,只需使用来自您感兴趣的包的对象或类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置捆绑包开发环境(Eclipse Equinox Maven) - How to setup bundle development environment (Eclipse Equinox Maven) 如何在 OSGi/Equinox/Eclipse 中附加库包的源代码? - How do I attach the sources for a library bundle in OSGi/Equinox/Eclipse? Equinox OSGi激活同一捆绑包的两个版本 - Equinox OSGi activating two versions of the same bundle 尝试以编程方式在 Equinox 中安装包时出现 nullPointerException - nullPointerException when trying to programmatically install a bundle in Equinox 如何为Eclipse Equinox克隆存储库? - How to clone repository for Eclipse Equinox? Osgi应用-Equinox 3.9中缺少捆绑包“ org.eclipse.equinox.http” - Osgi app - Missing Bundle “org.eclipse.equinox.http” in Equinox 3.9 有没有一种将Equinox源添加到Eclipse的好方法,以帮助调试OSGi捆绑软件? - Is there a good way to add Equinox source to Eclipse to help debugging an OSGi bundle? gogo:BundleException:尝试使用Equinox读取OSGi中的捆绑软件时发生错误 - gogo: BundleException: An error occurred trying to read the bundle in OSGi with Equinox Eclipse PDE:重新捆绑捆绑到正在运行的Equinox osgi框架 - Eclipse PDE: Redeploy bundle to running equinox osgi framework Equinox中的LogService和LogReaderService如何工作? - How do LogService and LogReaderService in Equinox work along?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM