users@glassfish.java.net

Classloader Delegation Issue, Commons Logging

From: <glassfish_at_javadesktop.org>
Date: Wed, 16 May 2007 13:50:26 PDT

I'm wrestling with the correct way to organize an applicatiion in Glassfish 2 beta --

Commons-logging is included as part of glashfish, so that complicates things.

If I have a simple web application that uses commons logging and log4j, I can get it to work by putting both jars in the web-inf/lib directory, and specifying a delegation mode where it looks at the parent classloader last.

However, I am planning on using MDBs, so I need to organize around the idea that I'll have both a web app and an EJB module.

I've tried putting log4j and commons logging, as well jars containing common code and the log4j and commons logging property file inside the ear in a /lib directory, referenced by the application.xml "library-directory" setting.

The problem is that glassfish uses a parent-first delegation model (with the exception of the web module classloader), so it ends up using the commons logging that is loaded by the Shared Chain Classloader. Which then means that it can't see log4j. Although oddly, it knows that I want to use log4j, which means it is seeing the property file that is in the jar.

The error I get is

No suitable Log constructor for Log4jLogger, caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category

Note also that if I leave everything where it is and I modify the source code to use log4j directly it works. So I've definitely packaged everything correctly, its simply a matter of "parent first" versus "parent last" at the application level.

The behavior I see seems to agree with what the docs for glassfish say:

https://glassfish.dev.java.net/javaee5/docs/DG/beade.html#beade

However, IMHO that doesn't seem like it is useful behavior. E.g.: I can also see wanting to be able to override XML or web service classes, and if I'm running into this issue here I assume I'll run into it in those cases as well.

As a reference point, I currently use WebSphere 5.1, which allows you to configure "parent-first/parent-last" both at the web module level and at the ear application level.

Any suggestions would be appreciated.
[Message sent by forum member 'gefowler' (gefowler)]

http://forums.java.net/jive/thread.jspa?messageID=217486