【发布时间】:2018-09-30 11:49:33
【问题描述】:
总结: 我的 OSGi 包无法在 OSGi 环境中查看运行时可用的类。
背景:
我的 RCP 应用程序将对象存储到磁盘,这涉及存储每个对象的类名。这个想法是在加载时,将使用写入磁盘的类名重新实例化对象。主AppBundle 执行存储操作。存储的对象中有其他包提供的类型的对象,例如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 的构建依赖。这个想法是应用程序的用户可以根据需要在运行时激活不同的捆绑包。
尝试的解决方案:
我怀疑问题与 OSGi 对每个捆绑包使用不同的类加载器和 AppBundle 的类加载器无法解析 Bundle1Class 有关,因为它不是通过捆绑包依赖项指定的编译时依赖项或包进口。所以,我尝试了以下方法。
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 可见。
实际行为: 奇怪的是,时不时地(相当随机地),这是有效的。但是,大多数时候,我遇到关于找不到类的错误。
【问题讨论】:
标签: java osgi eclipse-rcp