URLClassLoader and Tomcat

Discussion in 'Java' started by Romain, Sep 28, 2005.

  1. Romain

    Romain Guest

    Hi everybody,

    I'm trying to write a plugin-like system for Tomcat. The plugin is a
    class with some default method. All plugins must extend this base
    class.

    Here is my architecure :
    /WEB-INF/classes/GenericRule.class
    ....
    /somewhere-not-related-to-tomcat/classes/MyRule.class

    I don't want to put the MyRule.class inside the WEB-INF repository
    because MyRule doesn't belong to me. It's written by somebody else.
    WEB-INF should contain only my proper code and all specific code should
    go elsewhere.

    Problem womes with the Tomcat ClassLoader. Everything works correctly
    in a standalone application but not inside Tomcat because Tomcat
    ClassLoader is not really a standard one.

    Here is a snippet of my code (factory of rule) :

    String myRuleClassname;
    Object obj = null;
    URL[] urls = new URL[1];
    urls[0] = (new
    String("/somewhere-not-related-to-tomcat/classes")).toURL();

    ClassLoader cl = GenericRule.class.getClass().getClassLoader();
    if (cl != null) {
    System.out.println("Default ClassLoader : " + cl);

    URLClassLoader urlc = new URLClassLoader(urls, cl);
    obj = urlc.loadClass(myRuleClassname);
    } else {
    ClassLoader cl2 = ClassLoader.getSystemClassLoader();
    if (cl2 != null) {
    System.out.println("System ClassLoader : " + cl2);

    URLClassLoader urlc = new URLClassLoader(urls, cl2);
    obj = urlc.loadClass(myRuleClassname);
    } else {
    System.out.println("No ClassLoader found !");

    URLClassLoader urlc = new URLClassLoader(urls);
    obj = urlc.loadClass(myRuleClassname);
    }
    }

    if( obj instanceof GenericRule ){
    // Good
    } else {
    // Bad : I didn't instanciate correctly my Object.
    }


    Result of this is : default classLoader is void. System class loader is
    "sun.misc.Launcher$AppClassLoader@1813fac".

    If I try to load myRule with the SystemClassLoader, there is an
    exception because GenericRule cannot be found. Which mean that System
    class loader is not the one with the webapp specific classpath.
    If I try to load myRule without any parent ClassLoader, the same
    exception occurs which is normal.
    If I modify the array of URL in the URlClassLoader to add the
    WEB-INF/lib and WEB-INF/classes path, the instanciation goes correctly,
    but myRule extends the GenericRule from the URLClassLoader, not the one
    from Tomcat (in this cas, there is 2 types GenericRule, one for each
    classloader). So, obj is not of type GenericRule. If I cast to
    genericRule, I have a ClassCastException.

    The last two behaviors are normal. The only way to load properly the
    plugin is to be sure that the taken GenericRule is the one defined in
    the first ClassLoader. My problem is that I can't find a way to get the
    Tomcat ClassLoader with the GenericRule class defined.

    I hope I've been clear enough...
    If anyone has any idea, let me know, you will save my week :)
     
    Romain, Sep 28, 2005
    #1
    1. Advertising

  2. Romain

    Romain Guest

    I finally find the problem.
    The ClassLoader I must load is not :
    GenericRule.class.getClass().getClassLoader(); but
    GenericRule.class.getClassLoader();

    It now works as it should.
    The new ClassLoader is defined with the WebappClassLoader as a parent.
     
    Romain, Sep 28, 2005
    #2
    1. Advertising

  3. Romain

    pkriens Guest

    Isnt it easier to have a plugin management system that runs Tomcat? It
    looks like you have reversed the control her?

    Take a look at Eclipse, Oscar/Felix (Apache) or Knopflerfish that allow
    you to return Tomcat in a very well specified plugin environment
    (OSGi). It will probably shield you completely from having to muck
    around with class loaders. And you really get hot pluggability.

    Kind regards,

    Peter Kriens
     
    pkriens, Sep 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. netpro2k
    Replies:
    2
    Views:
    1,091
    netpro2k
    Aug 21, 2003
  2. Cory
    Replies:
    13
    Views:
    5,742
    swick
    May 31, 2011
  3. Sundar

    help.! Java URLClassloader

    Sundar, Nov 3, 2003, in forum: Java
    Replies:
    1
    Views:
    4,292
    Anton Spaans
    Nov 4, 2003
  4. Timothy Fosdike

    URLClassLoader slow in 1.4.2

    Timothy Fosdike, Aug 26, 2004, in forum: Java
    Replies:
    0
    Views:
    532
    Timothy Fosdike
    Aug 26, 2004
  5. iras

    applet and URLClassLoader

    iras, Aug 9, 2006, in forum: Java
    Replies:
    0
    Views:
    1,251
Loading...

Share This Page