Saturday, September 26, 2009

Classloaders, shared directory and endorsed directory of Tomcat 5.5 and 6.0

Tomcat 5.5 Class Loader: http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
Tomcat 6.0 Class Loader: http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

Tomcat 5.5

Class loader hierarchy
image  (from tomcat 5.5 document)

Class loaders:

  • Boostrap, System
  • Common:
    visible to both Tomcat internal classes and to all web applications.
    Related directories
    • classes and resources in: $CATALINA_HOME/common/classes
    • jar files in: $CATALINA_HOME/commons/endorsed, $CATALINA_HOME/commons/i18n and $CATALINA_HOME/common/lib
  • Catalina
    include all classes and resources required to implement Tomcat 5 itself. These classes and resources are TOTALLY invisible to web applications.
    Related directories:
    • classes and resources in: $CATALINA_HOME/server/classes
    • jars in: $CATALINA_HOME/server/lib
  • Shared
    Shared resources are shared across all web applications. (They are not used by Tomcat internal classes).
    Related directories
    • classes and resources in: $CATALINA_BASE/shared/classes
    • jars in: $CATALINA_BASE/shared/lib
  • WebappX
    Load web app specific resources and jars.
    Related directories
    • classes and resources: <web-app-dir>/WEB-INF/classes
    • jars: <web-app-dir>/WEB-INF/lib

Note: $CATALINA_BASE (not $CATALINA_HOME) is used class loader “Shared”.

#   CATALINA_HOME   May point at your Catalina "build" directory.
#
#   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
#                   of a Catalina installation.  If not present, resolves to
#                   the same directory that CATALINA_HOME points to.

Tomcat 6.0

Class loader hierarchy
image   (from tomcat 6.6 document)
Similar to the hierarchy in tomcat 5.5. 
The difference is that class loaders Catalina and Shared are not present in tomcat 6.6.
For Class loader Common, the related directories are different. In tomcat 6, classes, resources and jars under directory $CATALINA_HOME/lib (in tomcat 5.5, the directory is $CATALINA_HOME/common/classes, $CATALINA_HOME/common/lib, etc) are loaded by class loader Common.

These directories under which classes and resources are searched for can be changed by modifying configuration file $CATALINA_HOME/conf/catalina.properties.
Default values for class loader search directories are:

common.loader=${catalina.home}/lib,${catalina.home}/lib/*.jar
server.loader=
:shared.loader=

If you still want to use shared directory in the way specified in tomcat 5.5, you can change value of property shared.loader.
For example: shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar

Endorsed directory

Tomcat 5.5

“Tomcat utilizes this mechanism by including the system property setting
-Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed in the command line that starts the container. Therefore, you can replace the parser that is installed in this directory, and it will get used even on a JDK 1.4 system.”

Tomcat 6.0

Tomcat utilizes this mechanism by including the system property setting
-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS in the command line that starts the container.

Variable JAVA_ENDORSED_DIRS is specified in file setclasspath.sh: 
    JAVA_ENDORSED_DIRS="$BASEDIR"/endorsed

Variable BASEDIR is specified in file catalina.sh:
    BASEDIR="$CATALINA_HOME"

In other words,
    in tomcat 5.5, the default endorsed directory is $CATALINA_HOME/common/endorsed
    in tomcat 6.0, the default endorsed directory is $CATALINA_HOME/endorsed