#10 Please allow for setting a custom class loader for deseriali

open
nobody
None
5
2009-03-13
2009-03-13
Antti Haapala
No

We are currently using simple framework in an Eclipse based project. Eclipse, based on OSGI bundles, uses separate classloaders for each plugins. That means, that we are not currently able to deserialize any classes referred to by class attributes that reside in a bundle separate from Simple Framework.

The problem is that ReadGraph uses the class loader that loaded the framework to resolve overridden classes from "class" attributes; alas, that loader is not able to see any classes that are further down in the dependency graph.

Discussion

  • Antti Haapala
    Antti Haapala
    2009-03-13

    The stack backtrace:

    java.lang.ClassNotFoundException: com.acme.DerivedClass
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at org.eclipse.core.runtime.internal.adaptor.ContextFinder.loadClass(ContextFinder.java:129)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at org.simpleframework.xml.graph.ReadGraph.getElement(ReadGraph.java:105)
    at org.simpleframework.xml.graph.CycleStrategy.getElement(CycleStrategy.java:189)

     
  • Antti Haapala
    Antti Haapala
    2009-03-13

    Workaround:

    ClassLoader original = Thread.currentThread()
    .getContextClassLoader();

    Thread.currentThread().setContextClassLoader(
    getClass().getClassLoader());

    Strategy strategy = new CycleStrategy("id", "ref");
    Serializer serializer = new Persister(strategy);
    ContainerClass set = serializer.read(ContainerClass.class, in);

    Thread.currentThread().setContextClassLoader(original);

    However, changing context class loader has other, possibly serious implications; for example
    without hacks, the simple framework was not able to load its own classes.

     
  • Hi,

    You could always create your own composite class loader. The use of the thread context class loading was done to cater for the scenario you describe.

    Niall