简体   繁体   English

OSGi:如何获取其他捆绑软件的Classloader

[英]OSGi: How to get Classloader of other bundle

I have a Virgo-Tomcat-Server running. 我正在运行一个Virgo-Tomcat-Server。 There is an EnumMap, whose key is 有一个EnumMap,其键为

bundle.a.MyEnum

Context from this map is received via 该地图的上下文是通过接收的

bundle.b

and Spring expression language using a SpelExpressionParser, a sample expression would be "get(T(bundle.a.MyEnum).SAMPLEKEY)". 和使用SpelExpressionParser的Spring表达式语言,示例表达式将为“ get(T(bundle.a.MyEnum).SAMPLEKEY)”。 The Parser (respectively its TypeLocator) needs access to the ClassLoader of bundle.a. 解析器(分别是其TypeLocator)需要访问bundle.a的ClassLoader。

So I did: 所以我做了:

TypeLocator typeLocator = new StandardTypeLocator(getBundleAClassLoader());
StandardEvaluationContext evaluationContext  = new StandardEvaluationContext();
evaluationContext.setTypeLocator(typeLocator);
spelExpressionParser = new SpelExpressionParser();
spelExpressionParser.parseExpression(expression)).getValue(evaluationContext, context);

The question is, what is the proper way to get the class loader of bundle.a in a class of bundle.b? 问题是,在bundle.b的类中获取bundle.a的类加载器的正确方法是什么? After a couple of attempts, the only working solution I found is: 经过几次尝试,我发现的唯一可行的解​​决方案是:

private static ClassLoader getBundleAClassLoader() {
    MyEnum bundleARef = MyEnum.SAMPLEKEY;
    return bundleARef.getClass().getClassLoader();
}

Edit: Solution 编辑:解决方案

getBundleAClassLoader()

is not necessary, 没有必要,

TypeLocator typeLocator = new StandardTypeLocator(this.getClass().getClassLoader());

works fine. 工作正常。

This sounds too complicated. 这听起来太复杂了。 Just do an Import-Package in the Manifest of bundle.b and you can access the type in the same way as your own type. 只需在bundle.b的清单中执行Import-Package,就可以像访问自己的类型一样访问该类型。

eg 例如

SomeClassOfBundle.class.getClassLoader()

or 要么

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM