Re: ClassLoader vs GroovyClassLoader

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Nov 04 2010 - 13:16:04 UTC

  • Next message: Borut Bolčina: "Re: Writable FKs"

    Also be aware that most (all?) places in Cayenne 3.0.x were we load classes dynamically would use a thread ClassLoader if available:

    Thread.currentThread().getContextClassLoader()

    so you can force your ClassLoader via Thread.currentThread().setContextClassLoader(cl);

    Cayenne 3.1 uses a similar approach, and additionally allows to register a custom service via DI.

    Andrus

    On Nov 3, 2010, at 3:57 PM, caden whitaker wrote:

    > Think I got it, at least doing this gave me a ton of new errors that make
    > sense ;) please let me know if you think I'm on the wrong track:
    >
    > ClassLoader parent = getClass().getClassLoader();
    > GroovyClassLoader loader = new GroovyClassLoader(parent);
    > loader.parseClass(new
    > File("C:\\tutorial\\src\\main\\java\\org\\example\\cayenne\\persistent\\Store.groovy"));
    >
    > ResourceLocator rl = new ResourceLocator();
    > rl.setClassLoader(loader);
    >
    > Configuration c = new DefaultConfiguration("cayenne.xml", rl);
    > c.addDomain(new DataDomain("default"));
    >
    > On Wed, Nov 3, 2010 at 2:29 PM, caden whitaker <caden.whitake..mail.com>wrote:
    >
    >> Hey all, hope you aren't tired of me yet
    >>
    >> This in a nutshell is the problem:
    >> ClassLoader parent = getClass().getClassLoader();
    >> GroovyClassLoader loader = new GroovyClassLoader(parent);
    >> loader.parseClass(new
    >> File("C:\\tutorial\\src\\main\\java\\org\\example\\cayenne\\persistent\\Store.groovy"));
    >>
    >> loader.loadClass("main.java.org.example.cayenne.persistent.Store");
    >>
    >> System.out.println(Class.forName("main.java.org.example.cayenne.persistent.Store",
    >> true, loader).toString());
    >>
    >> DataDomain dd =
    >> Configuration.getSharedConfiguration().getDomain();
    >> ObjectContext context = dd.createDataContext();
    >>
    >> Error:
    >> java.lang.ClassNotFoundException: class
    >> main.java.org.example.cayenne.persistent.Store
    >> at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    >> at java.security.AccessController.doPrivileged(Native Method)
    >> at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    >> at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    >> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    >> at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    >> at
    >> main.java.org.example.cayenne.ut.CayenneUnitTest.testBuild(CayenneUnitTest.java:128)
    >>
    >> At this point the "Store" object (which is compiled at runtime through
    >> Groovy) does not exist in any context that teh DataContext can find it, the
    >> DataContext is looking for it in Class.forName, but that is looking in the
    >> default ClassLoader. This "Store" object does not exist in that context, it
    >> is in its own ClassLoader (GroovyClassLoader). So how do I tell the system
    >> to load the object from this GroovyClassLoader?? I know this is the issue
    >> because if I take that Store object, make it a Java class, compile it, and
    >> run the same test it works fine.
    >>
    >>
    >>



    This archive was generated by hypermail 2.0.0 : Thu Nov 04 2010 - 13:16:40 UTC