java - 使用 XStream 反序列化 XML 在 OSGi (Java 1.4) 中不起作用

标签 java maven osgi xstream java1.4

我创建了一些 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

所以我尝试通过设置类加载器来解决这个问题。但这不起作用。

我的 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:Equinox 3.6.0
  • XStream 1.3.1 (com.springsource.com.thoughtworks.xstream)

非常欢迎任何帮助!

最佳答案

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

Could not call ParentData.readObject()

您已声明您的 ParentData 实现了 Serialized。尽管 Serialized 接口(interface)不包含任何方法,但 Java 序列化和 XStream 都需要 readObjectwriteObject。我对强制执行这些操作的具体情况有点生疏,但我建议您从 ParentData 类中删除 implements Serialized

关于java - 使用 XStream 反序列化 XML 在 OSGi (Java 1.4) 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889153/

相关文章:

java - H2数据库TIMESTAMP列的默认值

java - 在 Eclipse 中为 Java 和 Scala 设置不同的选项卡空间

java - Spring Boot - 无法启动嵌入式容器

java - 如何在 Camel route 使用 OSGI 包?

java - 尝试在 virgo tomcat 中配置自定义 session 管理器时获取 ClassNotFoundException

java - 关于爬虫礼貌并交叉验证我做得是否正确?

java - application.properties 被忽略,取自别处

maven - 如何在 OpenShift 中正确配置 h2

java - ClassNotFound - 将第三方 Jar 添加到 Maven

java - Apache Felix OSGI 中不存在软件包