简体   繁体   English

在osgi包中动态加载类

[英]dynamically loading class in osgi bundle

I've OSGI bundle (say A) which depends on Non-osgi library (Say B). 我有OSGI捆绑包(例如A),它依赖于Non-osgi库(说B)。 B is using Class.forName to load one of the class (ClassA from library A is of type ClassB from library B). B使用Class.forName加载类之一(库A中的ClassA是库B中的ClassB类型)。 I've wrapped the library B and made it osgi bundle and imported the packages which is needed in library A but I'm not able to load the class using Class.forName. 我包装了库B并将其与osgi捆绑在一起,并导入了库A中所需的软件包,但是我无法使用Class.forName加载该类。 Note that library B is third party library and i don't have any control on this. 请注意,库B是第三方库,对此我没有任何控制权。

Here is the manifest file of library B which i made OSGI enabled library - 这是库B的清单文件,我将其制成启用OSGI的库-

Manifest-Version: 1.0 清单版本:1.0
Bnd-LastModified: 1420745798993 上次修改时间:1420745798993
Build-Jdk: 1.6.0_51 Build-Jdk:1.6.0_51
Built-By: xyz 内置:xyz
Bundle-ManifestVersion: 2 Bundle-ManifestVersion:2
Bundle-Name: dapclient 捆绑名称:dapclient
Bundle-SymbolicName: dapclient Bundle-SymbolicName:dapclient
Bundle-Vendor: dapclient 捆绑销售商:dapclient
Bundle-Version: 1.0.0.SNAPSHOT 捆绑版本:1.0.0。
Created-By: Apache Maven Bundle Plugin 创建者:Apache Maven捆绑插件
DynamicImport-Package: com.xxx.zzz.wi.shared.datacache.model DynamicImport-Package:com.xxx.zzz.wi.shared.datacache.model
Export-Package: com.xxx.platform.yyy.persistence.parser.entity;versio n="2.0.2.8",com.intuit.xxx.yyy.persistence.utils;version="2.0.2.8" ,com.xxx.platform.yyy.persistence.parser.domain;version="2.0.2.8",co m.xxx.platform.yyy.persistence;version="2.0.2.8",com.xxx.platform .yyy.persistence.types;version="2.0.2.8",com.xxx.platform.yyy.persis tence.annotations;version="2.0.2.8",com.xxx.platform.yyy.persistence .parser;version="2.0.2.8" 导出包:com.xxx.platform.yyy.persistence.parser.entity; versio n =“ 2.0.2.8”,com.intuit.xxx.yyy.persistence.utils; version =“ 2.0.2.8”,com.xxx .platform.yyy.persistence.parser.domain; version =“ 2.0.2.8”,co m.xxx.platform.yyy.persistence; version =“ 2.0.2.8”,com.xxx.platform .yyy.persistence.types;版本=“ 2.0.2.8”,com.xxx.platform.yyy.persis tence.annotations; version =“ 2.0.2.8”,com.xxx.platform.yyy.persistence .parser; version =“ 2.0.2.8”
Tool: Bnd-2.1.0.20130426-122213 工具:Bnd-2.1.0.20130426-122213

In this library, we're loading this package using ClasspathHelper.forPackage(packageName) where packageName is "com.xxx.zzz.wi.shared.datacache.model". 在此库中,我们使用ClasspathHelper.forPackage(packageName)加载此程序包,其中packageName为“ com.xxx.zzz.wi.shared.datacache.model”。 ClasspatheHelper is used from reflections library. 从反射库使用ClasspatheHelper。

classForName uses 'the defining class loader of the current class', so you might have to import the package in A and B to make this work. classForName使用“当前类的定义类加载器”,因此您可能必须将包导入到A和B中才能完成此工作。 So in general, ClassA must be in a separate bundle. 因此,通常,ClassA必须在单独的捆绑软件中。 But if you are that tightly coupled to the lib, it might be better to embeed 'B' int Bundle A. 但是,如果您与lib紧密相连,则最好在'B'int Bundle A中嵌入'B'int BundleA。

Beside that, you can open a ticket to the creator of B to allow more generic classloading, like providing the class itself instead of a string, support some kind of resolver-hooks or at least use context-class-loading in which case you can use something like 除此之外,您可以向B的创建者开票,以允许进行更通用的类加载,例如提供类本身而不是字符串,支持某种类型的解析器挂钩或至少使用上下文类加载,在这种情况下,您可以使用类似

Thread thread = Thread.currentThread();
ClassLoader oldLoader = thread.getContextClassLoader();
try {
    thread.setContextClassLoader(getClass().getClassLoader());
    //call lib here that supports context-class-loading
} finally {
    thread.setContextClassLoader(oldLoader);
}

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

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