1

我创建了一些 OSGi 捆绑包。其中之一具有使用 XStream 将数据导出到 xml 的功能。它工作正常。当使用 Bundle 作为库而不是在 OSGi 上下文中时,也可以再次导入。

但是,如果我在另一个包中调用我的导入,我会得到一个“com.thoughtworks.xstream.converters.ConversionException”,并打印出以下调试信息:

---- Debugging information ----
message             : Cannot find class ChildDate
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot find class ChildData
class               : ChildData
required-type       : ChildData
path                : /ParentData/ChildData
-------------------------------
message             : Could not call ParentData.readObject()
cause-exception     : com.thoughtworks.xstream.converters.ConversionException
cause-message       : Cannot find class ParentData : Cannot find class ChildData
class               : ParentData
required-type       : ChildData
path                : /ParentData/ChildData
-------------------------------

我认为这是一个类似的问题:XStream and OSGi或这个:CannotResolveClassException in OSGi environment

所以我试图通过设置 ClassLoader 来解决它。但它不起作用。

我的 ParentData 类的一部分:

public class ParentData implements Serializable {
  // [...]
  private static ClassLoader classLoaderForImport = ParentData.class.getClassLoader();
  // [...]
  public static void setClassLoaderForImport(ClassLoader classLoaderForImport) {
    ParentData.classLoaderForImport = classLoaderForImport;
  }
  // [...]
  public static Lumicon importFromXMLFile(String path) {
    return importFromFile(new DomDriver(), path);
  }
  private static ParentData importFromFile(HierarchicalStreamDriver driver, String path) {
    try {
      XStream xstream = new XStream(driver);
      //set the classloader as the default one won't work in any case
      xstream.setClassLoader(ParentData.classLoaderForImport);
      xstream.alias("ParentData", classLoaderForImport.loadClass(ParentData.class.getName()));//ParentData.class);
      xstream.alias("ChildData", classLoaderForImport.loadClass(ChildData.class.getName()));//ChildData.class);
      Reader reader = new FileReader(path);
      Object object = xstream.fromXML(reader);
      return (ParentData) object;
    } catch (ClassNotFoundException ex) {
      System.out.println("This did not work.");
    } catch (FileNotFoundException e) {
      System.out.println("File " + path + " not found.");
    }
  }
// [...]
}

该功能xstream.fromXML(reader)不起作用,但classLoaderForImport.loadClass(ChildData.class.getName())不会失败。

这就是我从另一个 Bundle 中调用它的方式:

ParentData.setClassLoaderForImport(ParentData.class.getClassLoader());
data = ParentData.importFromXMLFile(path); // this is where the Exception happens

我也试过this.getClass().getClassLoader()ChildData.class.getClassLoader()

会不会因为这个函数是从第三个包调用的,所以这不起作用?

更多信息:

  • Java 版本:1.4(不,我不能升级到 1.5 或 1.6)
  • 执行环境:J2SE-1.6
  • Maven 版本:2.2.1
  • 使用 OPS4J 的 Pax Runner (1.5.0) 运行 - http://www.ops4j.org
  • OSGi:春分 3.6.0
  • XStream 1.3.1 (com.springsource.com.thoughtworks.xstream)

非常欢迎任何帮助!

4

1 回答 1

1

假设您的问题包含所有相关代码,则问题由以下内容给出:

Could not call ParentData.readObject()

您已声明您的ParentData implements Serializable. 尽管该Serializable接口不包含任何方法,但 Java 序列化和 XStream 都需要 areadObject和 a writeObject。我对这些是强制性的确切情况有点生疏,但我建议你implements SerializableParentData课堂上删除。

于 2011-02-03T21:03:00.663 回答