alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Tomcat example source code file (class-loader-howto.xml)

This example Tomcat source code file (class-loader-howto.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Tomcat tags/keywords

all, j2se, jar, java, license, license, see, system, this, this, tomcat, tomcat, xml, xml

The Tomcat class-loader-howto.xml source code

<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!DOCTYPE document [
  <!ENTITY project SYSTEM "project.xml">
]>
<document url="class-loader-howto.html">

    &project;

    <properties>
        <author email="craigmcc@apache.org">Craig R. McClanahan
        <author email="yoavs@apache.org">Yoav Shapira
        <title>Class Loader HOW-TO
    </properties>

<body>


<section name="Overview">

<p>Like many server applications, Tomcat 6 installs a variety of class loaders
(that is, classes that implement <code>java.lang.ClassLoader) to allow
different portions of the container, and the web applications running on the
container, to have access to different repositories of available classes and
resources.  This mechanism is used to provide the functionality defined in the
Servlet Specification, version 2.4 -- in particular, Sections 9.4 and 9.6.</p>

<p>In a J2SE 2 (that is, J2SE 1.2 or later) environment, class loaders are
arranged in a parent-child tree.  Normally, when a class loader is asked to
load a particular class or resource, it delegates the request to a parent
class loader first, and then looks in its own repositories only if the parent
class loader(s) cannot find the requested class or resource.  The model for
web application class loaders differs slightly from this, as discussed below,
but the main principles are the same.</p>

<p>When Tomcat 6 is started, it creates a set of class loaders that are
organized into the following parent-child relationships, where the parent
class loader is above the child class loader:</p>

<source>
      Bootstrap
          |
       System
          |
       Common
       /     \
  Webapp1   Webapp2 ... 
</source>

<p>The characteristics of each of these class loaders, including the source
of classes and resources that they make visible, are discussed in detail in
the following section.</p>

</section>

<section name="Class Loader Definitions">

<p>As indicated in the diagram above, Tomcat 6 creates the following class
loaders as it is initialized:</p>
<ul>
<li>Bootstrap - This class loader contains the basic runtime
    classes provided by the Java Virtual Machine, plus any classes from JAR
    files present in the System Extensions directory
    (<code>$JAVA_HOME/jre/lib/ext).  NOTE - Some JVMs may
    implement this as more than one class loader, or it may not be visible
    (as a class loader) at all.</li>
<li>System - This class loader is normally initialized from
    the contents of the <code>CLASSPATH environment variable.  All such
    classes are visible to both Tomcat internal classes, and to web
    applications.  However, the standard Tomcat 5 startup scripts
    (<code>$CATALINA_HOME/bin/catalina.sh or
    <code>%CATALINA_HOME%\bin\catalina.bat) totally ignore the contents
    of the <code>CLASSPATH environment variable itself, and instead
    build the System class loader from the following repositories:
    <ul>
    <li>$CATALINA_HOME/bin/bootstrap.jar - Contains the main() method
        that is used to initialize the Tomcat 6 server, and the class loader
        implementation classes it depends on.</li>
    <li>$CATALINA_HOME/bin/tomcat-juli.jar - Package renamed Jakarta commons 
        logging API, and java.util.logging LogManager.</li>
    </ul>
<li>Common - This class loader contains additional classes
    that are made visible to both Tomcat internal classes and to all web
    applications.  Normally, application classes should <strong>NOT
    be placed here.  All unpacked classes and resources in
    <code>$CATALINA_HOME/lib, as well as classes and
    resources in JAR files are made visible through this
    class loader.  By default, that includes the following:
    <ul>
    <li>annotations-api.jar - JEE annotations classes.
    <li>catalina.jar - Implementation of the Catalina servlet
        container portion of Tomcat 6.</li>
    <li>catalina-ant.jar - Tomcat Catalina Ant tasks.
    <li>catalina-ha.jar - High availability package.
    <li>catalina-tribes.jar - Group communication package.
    <li>el-api.jar - EL 2.1 API.
    <li>jasper.jar - Jasper 2 Compiler and Runtime.
    <li>jasper-el.jar - Jasper 2 EL implementation.
    <li>jasper-jdt.jar - Eclipse JDT 3.2 Java compiler.
    <li>jsp-api.jar - JSP 2.1 API.
    <li>servlet-api.jar - Servlet 2.5 API.
    <li>tomcat-coyote.jar - Tomcat connectors and utility classes.
    <li>tomcat-dbcp.jar - package renamed database connection 
        pool based on Commons DBCP.</li>
    <li>tomcat-i18n-**.jar - Optional JARs containing resource bundles
        for other languages. As default bundles are also included in each 
        individual JAR, they can be safely removed if no internationalization
        of messages is needed.</li>
    </ul>
<li>WebappX - A class loader is created for each web
    application that is deployed in a single Tomcat 6 instance.  All unpacked
    classes and resources in the <code>/WEB-INF/classes directory of
    your web application archive, plus classes and resources in JAR files
    under the <code>/WEB-INF/lib directory of your web application
    archive, are made visible to the containing web application, but to
    no others.</li>
</ul>

<p>As mentioned above, the web application class loader diverges from the
default Java 2 delegation model (in accordance with the recommendations in the
Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader).  
When a request to load a
class from the web application's <em>WebappX class loader is processed,
this class loader will look in the local repositories <strong>first,
instead of delegating before looking.  There are exceptions. Classes which are
part of the JRE base classes cannot be overriden. For some classes (such as
the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be 
used.
Last, any JAR containing servlet API classes will be ignored by the 
classloader.
All other class loaders in Tomcat 6 follow the usual delegation pattern.</p>

<p>Therefore, from the perspective of a web application, class or resource
loading looks in the following repositories, in this order:</p>
<ul>
<li>Bootstrap classes of your JVM
<li>System class loader classses (described above)
<li>/WEB-INF/classes of your web application
<li>/WEB-INF/lib/*.jar of your web application
<li>$CATALINA_HOME/lib
<li>$CATALINA_HOME/lib/*.jar
</ul>

</section>


<section name="XML Parsers and JSE 5">

<p>Among many other changes, the JSE 5 release packages the JAXP APIs, and
a version of Xerces, inside the JRE.  This has impacts on applications that
wish to use their own XML parser.</p>

<p>In previous versions of Tomcat, you could simply replace the XML parser
in the <code>$CATALINA_HOME/common/lib directory to change the parser
used by all web applications.  However, this technique will not be effective
when you are running on JSE 5, because the usual class loader delegation
process will always choose the implementation inside the JDK in preference
to this one.</p>

<p>JDK 1.5 supports a mechanism called the "Endorsed Standards Override
Mechanism" to allow replacement of APIs created outside of the JCP (i.e.
DOM and SAX from W3C).  It can also be used to update the XML parser
implementation.  For more information, see:
<a href="http://java.sun.com/j2se/1.5/docs/guide/standards/index.html">
http://java.sun.com/j2se/1.5/docs/guide/standards/index.html</a>.

<p>Tomcat utilizes this mechanism by including the system property setting <code>-Djava.endorsed.dirs=$CATALINA_HOME/endorsed in the command line that starts the container.</p> </section> <section name="Running under a security manager"> <p>When running under a security manager the locations from which classes are permitted to be loaded will also depend on the contents of your policy file. See <a href="security-manager-howto.html">Security Manager HOW-TO for further information.</p> </section> </body> </document>

Other Tomcat examples (source code examples)

Here is a short list of links related to this Tomcat class-loader-howto.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.