170 WebSphere Application Server V6.1 Problem Determination IBM Redpaper Collection
Figure 4-5 Class loader policies: Example 3
There is now only a single application class loader loading classes for both
Application1 and Application2. Classes in Util1.jar can see classes in EJB2.jar,
Util2.jar, WAR2-1.war and WAR2-2.war. The classes loaded by the application
class loader still cannot, however, see the classes in the WAR1-1 and WAR1-2
modules, because a class loader can only find classes by going up the hierarchy,
never down.
4.1.8 Class loading/delegation mode
WebSphere’s application class loader and WAR class loader both have a setting
called the class loader order. This setting determines whether they should follow
the normal Java class loader delegation mechanism or override it.
There are two possible options for the class loading mode:
򐂰 Classes loaded with parent class loader first
򐂰 Classes loaded with application class loader first
In previous WebSphere releases, these settings were called PARENT_FIRST
and PARENT_LAST, respectively.
The default value for class loading mode is Classes loaded with parent class
loader first (PARENT_FIRST). This mode causes the class loader to first
delegate the loading of classes to its parent class loader before attempting to
WebSphere Extensions class loader
WAR1-1
class loader
WAR1-2
class loader
EJB1.jar
Util1.jar
EJB2.jar
Util2.jar
WAR2-1.war
WAR2-2.war
Application class loader
Chapter 4. Class loader problem determination 171
load the class from its local class path. This is the default policy for standard Java
class loaders.
If the class loading policy is set to Classes loaded with application class loader
first (PARENT_LAST), the class loader attempts to load classes from its local
class path before delegating the class loading to its parent. This policy allows an
application class loader to override and provide its own version of a class that
exists in the parent class loader.
Assume you have an application, similar to Application1 in the previous
examples, and it uses the popular log4j package to perform logging from both the
EJB module and the two Web modules. Also assume that each module has its
own, unique, log4j.properties file packaged into the module. It is tempting to
configure log4j as a utility JAR so you only have a single copy of it in your EAR
file.
However, if you do that, you might be surprised to see that all modules, including
the Web modules, load the log4j.properties file from the EJB module. The reason
is that when a Web module initializes the log4j package, the log4j classes are
loaded by the application class loader. log4j is configured as a utility JAR. log4j
then looks for a log4j.properties file on its class path and finds it in the EJB
module.
Even if you do not use log4j for logging from the EJB module and the EJB
module does not, therefore, contain a log4j.properties file, log4j does not find the
log4j.properties file in any of the Web modules anyway. The reason is that a class
loader can only find classes by going up the hierarchy, never down.
To solve this problem, you can do one of the following:
򐂰 Create a separate file, for example, Resource.jar, configure it as a utility JAR,
move all log4j.properties files from the modules into this file, and make their
names unique (like war1-1_log4j.properties, war1-2_log4j.properties, and
ejb1_log4j.properties). When initializing log4j from each module, tell it to load
the proper configuration file for the module instead of the default
(log4j.properties).
Note: The administrative console is a bit confusing at this point. On the
settings page for a Web module, the two options for class loader order are
Classes loaded with parent class loader first and Classes loaded with
application class loader first. However, in this context, the “application class
loader” really refers to the WAR class loader, so the option Classes loaded
with application class loader first should really be called Classes loaded with
WAR class loader first.

Get WebSphere Application Server V6.1 Problem Determination: IBM Redpaper Collection now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.