RCP应用程序中捆绑包之间类的java运行时可见性
摘要: 我的OSGi包在OSGi环境中看到运行时可用的类时遇到了问题
上下文:
我的RCP应用程序将对象存储到磁盘,这涉及到存储每个对象的类名。其想法是,加载时,将使用写入磁盘的类名重新实例化对象。主AppBundle
执行存储操作。存储的对象中包括由其他bundle提供的类型的对象,例如Bundle1
和Bundle2
问题:
当我试图根据存储的名称读取类信息时,尽管相同的包及其提供的类在OSGi运行时中,但从我的AppBundle
中看不到类信息。我已经包括了一个处理存储和读取的代码示意图
public class Storage{
transient Object[] objs = new Object[3];
{
// Something link the following happens dynamically during the application
objs[0] = new AppBundleClass();
objs[1] = new Bundle1Class();
objs[2] = new Bundle2Class();
}
// Persisted to disk
private String[] objTypes;
public void saveToDisk(){
objTypes = new String[objs.length];
for (int i = 0; i < objTypes.length; i++)
objTypes[i] = objs[i].getClass().getCanonicalName();
}
public void afterLoadingFromDisk(){
objs = new Object[objTypes.length];
for (int i = 0; i < objs.length; i++){
Class<?> klass = Class.forName(objTypes[i]);
// **** Throws error above ****
objs = loadByClass(klass);
// Custom method that works for classes withing the app.
}
}
}
AppBundle
对Bundle1
或Bundle2
没有生成依赖关系。其想法是,应用程序的用户可以根据需要在运行时激活不同的bundle
尝试的解决方案:
我怀疑这个问题与OSGi对每个bundle使用不同的类加载器有关,而AppBundle
的类加载器无法解析Bundle1Class
,因为它不是通过bundle依赖项或包导入指定的编译时依赖项。所以,我尝试了以下方法
Bundle1/MANIFEST.MF
DynamicImport-Package: *
(to allow packages from this bundle to be dynamically visible)
AppBundle/MANIFEST.MF
Eclipse-BuddyPolicy: global
(to allow this bundle to be able to resolve any class available in the OSGi runtime)
预期行为:
来自Bundle1
和Bundle2
的类现在应该对AppBundle
可见
实际行为: 奇怪的是,偶尔(相当随机地),这种方法会奏效。然而,大多数时候,我都会遇到关于找不到类的错误
共 (0) 个答案