The Ja-Jakarta ProjectTomcat Servlet/JSP コンテナ

リンク

ユーザガイド

リファレンス

Tomcat開発

Tomcat5 サーブレット/JSP コンテナ

Class Loader HOW-TO

クラスローダの手引き

Printer Friendly Version
印刷用
バージョン
Quick Start
手始めに

The following rules cover about 95% of the decisions that application developers and deployers must make about where to place class and resource files to make them available to web applications:

アプリケーション開発担当および配備担当が、クラスやリソースのファイルを Webアプリケーションで使えるようにするためにどこに置くかという決定の 95% をカバーするルールは以下の通りです。

  • For classes and resources specific to a particular web application, place unpacked classes and resources under /WEB-INF/classes of your web application archive, or place JAR files containing those classes and resources under /WEB-INF/lib of your web application archive.
  • For classes and resources that must be shared across all web applications, place unpacked classes and resources under $CATALINA_HOME/shared/classes, or place JAR files containing those classes and resources under $CATALINA_HOME/shared/lib.
  • 個々の Webアプリケーション固有のクラスやリソースについては、 Webアプリケーションの配置箇所の /WEB-INF/classes にアーカイブせずに置くか、これらのクラスやリソースを含む JAR ファイルを Webアプリケーションの配置箇所の /WEB-INF/lib に置きます。
  • すべての Webアプリケーションを通して共有すべきクラスやリソースについては、 $CATALINA_HOME/shared/classes にアーカイブせずに置くか、 これらのクラスやリソースを含む JAR ファイルを $CATALINA_HOME/shared/lib に置きます。
Overview
概要

Like many server applications, Tomcat 5 installs a variety of class loaders (that is, classes that implement 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.

多くのサーバアプリケーション同様、Tomcat 5 も様々なクラスローダ (つまり java.lang.ClassLoader を実装したクラス) をインストールします。それは、コンテナの様々な部分やコンテナ上で動作する Webアプリケーションが、 利用可能なクラスやリソースの様々なリポジトリにアクセスできるようになるためです。 このメカニズムは、Servlet 仕様バージョン 2.4 で定義された機能 --特に9.4節と9.6節-- を提供するのに使われています。

In a Java 2 (that is, JDK 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.

Java 2 (つまり JDK 1.2 以降) 環境では、 クラスローダは親子関係のツリーとして構成されています。通常、 クラスローダが個々のクラスやリソースのロードの要求を受けると、 まずは要求を親のクラスローダに委譲し、 親のクラスローダ(複数の場合あり)が要求されたクラスやリソースを見つけられなかった場合だけ自分のリポジトリを見に行きます。 以下で説明するようにこれとは若干異なりますが、 Webアプリケーション用クラスローダのモデルも基本原則は同じです。

When Tomcat 5 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:

Tomcat 5 を起動すると、 以下のような親子関係に編成された一連のクラスローダを生成します。 以下の図では、親のクラスローダが子のクラスローダの上に表記されています。

      Bootstrap
          |
       System
          |
       Common
      /      \
 Catalina   Shared
             /   \
        Webapp1  Webapp2 ... 
      Bootstrap
         |
       System
         |
       Common
     /      \
 Catalina   Shared
            /   \
        Webapp1  Webapp2 ... 

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.

これらのクラスローダのそれぞれの特徴については、 クラスローダが参照可能にするクラスやリソースの配置場所も含めて、 以下の節で詳細を説明します。

[訳者コメント: sourceは「配置場所」と訳しましたが自信がありません。]
[訳者コメント: それでいいんじゃないんでしょうか。ytp]
Class Loader Definitions
クラスローダ定義

As indicated in the diagram above, Tomcat 5 creates the following class loaders as it is initialized:

上の図で示した通り Tomcat 5 は、 以下のクラスローダを初期化の際に生成します。

  • 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 ($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.
  • Bootstrap - このクラスローダには Java 仮想マシン (JVM) が提供する基本ランタイムクラスと、加えてシステム拡張ディレクトリ ($JAVA_HOME/jre/lib/ext) のすべてのクラスが含まれています。 注意 - JVM によっては2つ以上のクラスローダとして実装されている場合もありますし、 (クラスローダとして)まったく参照できない場合もあります。
  • System - This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat 5 startup scripts ($CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%\bin\catalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:
    • $CATALINA_HOME/bin/bootstrap.jar - Contains the main() method that is used to initialize the Tomcat 5 server, and the class loader implementation classes it depends on.
    • $JAVA_HOME/lib/tools.jar - Contains the "javac" compiler used to convert JSP pages into servlet classes.
    • $CATALINA_HOME/bin/commons-logging-api.jar - Jakarta commons logging API.
    • $CATALINA_HOME/bin/commons-daemon.jar - Jakarta commons daemon API.
  • System - このクラスローダは通常、CLASSPATH 環境変数の内容を元に初期化されます。こうしたクラスは Tomcat 内部クラスからも、 Webアプリケーションからも参照できます。しかし、標準の Tomcat 5 起動スクリプト ($CATALINA_HOME/bin/catalina.sh または %CATALINA_HOME%\bin\catalina.bat) では、 CLASSPATH 環境変数そのものの内容を完全に無視し、 代わりに以下のリポジトリから System クラスローダを構築します。
    • $CATALINA_HOME/bin/bootstrap.jar - Tomcat 5 サーバを初期化するのに使う main() メソッドと、 サーバが依存するクラスローダ実装クラスが含まれます。
    • $JAVA_HOME/lib/tools.jar - JSP ページを Servlet クラスに変換する "javac" コンパイラが入っています。
    • $CATALINA_HOME/bin/commons-logging-api.jar - Jakarta Commons Logging の API。
    • $CATALINA_HOME/bin/commons-daemon.jar - Jakarta Commons Daemon の API。
  • 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 NOT be placed here. All unpacked classes and resources in $CATALINA_HOME/common/classes, as well as classes and resources in JAR files under the $CATALINA_HOME/commons/endorsed and $CATALINA_HOME/common/lib directories, are made visible through this class loader. By default, that includes the following:
    • ant.jar - Apache Ant.
    • commons-collection.jar - Jakarta commons collection.
    • commons-dbcp.jar - Jakarta commons DBCP, providing a JDBC connection pool to web applications.
    • commons-el.jar - Jakarta commons el, implementing the expression language used by Jasper.
    • commons-pool.jar - Jakarta commons pool.
    • jasper-compiler.jar - The JSP 2.0 compiler.
    • jasper-runtime.jar - The JSP 2.0 runtime.
    • jmx.jar - The JMX 1.2 implementation.
    • jsp-api.jar - The JSP 2.0 API.
    • naming-common.jar - The JNDI implementation used by Tomcat 5 to represent in-memory naming contexts.
    • naming-factory.jar - The JNDI implementation used by Tomcat 5 to resolve references to enterprise resources (EJB, connection pools).
    • naming-resources.jar - The specialized JNDI naming context implementation used to represent the static resources of a web application.
    • servlet-api.jar - The Servlet and JSP API classes.
    • xerces.jar - The XML parser that is visible by default to Tomcat internal classes and to web applications.
  • Common - このクラスローダには、Tomcat 内部クラスからも、 すべての Webアプリケーションからも参照可能になっている追加クラスが含まれます。 アプリケーションクラスは、 通常はここに置くべきではありません$CATALINA_HOME/common/classes にある アーカイブされていないクラスやリソースのすべて、それに $CATALINA_HOME/commons/endorsed$CATALINA_HOME/common/libにある JAR ファイル内のクラスやリソースのすべてをこのクラスローダから参照できます。 デフォルトでは以下のものが含まれます。
    • ant.jar - Apache Ant。
    • commons-collection.jar - Jakarta Commons Collection。
    • commons-dbcp.jar - Jakarta Commons DBCP。JDBC コネクションプールを Webアプリケーションに提供します。
    • commons-el.jar - Jakarta Commons EL。Jasper で使う EL (Expression Language) を実装します。
    • commons-pool.jar - Jakarta Commons Pool。
    • jasper-compiler.jar - JSP 2.0 コンパイラ
    • jasper-runtime.jar - JSP 2.0 ランタイム。
    • jmx.jar - JMX 1.2 実装。
    • jsp-api.jar - JSP 2.0 API。
    • naming-common.jar - Tomcat 5 で使われる JNDI 実装。 メモリ上のネーミングコンテキストに相当します。
    • naming-factory.jar - The JNDI Tomcat 5 で使われる JNDI 実装。 エンタープライズリソース (EJBやコネクションプール) への参照を解決します。
    • naming-resources.jar - 特殊な JNDI ネーミングコンテキスト実装。 Webアプリケーションの静的リソースに相当するものとして使われます。
    • servlet-api.jar - Servlet と JSP の API クラス。
    • xerces.jar - XML パーサ。Tomcat 内部クラスと Webアプリケーションから参照可能です。
  • Catalina - This class loader is initialized to include all classes and resources required to implement Tomcat 5 itself. These classes and resources are TOTALLY invisible to web applications. All unpacked classes and resources in $CATALINA_HOME/server/classes, as well as classes and resources in JAR files under $CATALINA_HOME/server/lib, are made visible through this class loader. By default, that includes the following:
    • catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 5.
    • jakarta-regexp-X.Y.jar - The binary distribution of the Jakarta Regexp regular expression processing library, used in the implementation of request filters.
    • servlets-xxxxx.jar - The classes associated with each internal servlet that provides part of Tomcat's functionality. These are separated so that they can be completely removed if the corresponding service is not required, or they can be subject to specialized security manager permissions.
    • tomcat-coyote.jar - Coyote connector for Tomcat 5.
    • tomcat-http11.jar - Standalone Java HTTP/1.1 connector.
    • tomcat-jk2.jar - Classes for the Java portion of the JK 2 web server connector, which allows Tomcat to run behind web servers such as Apache and iPlanet iAS and iWS.
    • tomcat-util.jar - Utility classes required by some Tomcat connectors.
  • Catalina - このクラスローダーは初期化時に、Tomcat 5 そのものを実装するのに必要なクラスやリソースすべてを含みます。 これらのクラスやリソースは Web アプリケーションからまったく参照できません。
    [訳者コメント: この原文は正しいのでしょうか? 例えばorg.apache.catalina.Containerなどもサーブレットから参照できますよね? ytp]
    $CATALINA_HOME/server/classes にあるアーカイブされないクラスやリソースすべて、 それに $CATALINA_HOME/server/lib にある JAR ファイル内のクラスやリソースがこのクラスローダで参照可能となります。 デフォルトでは以下のものが含まれます。
    • catalina.jar - Tomcat 5 の Catalina Servlet コンテナの部分の実装。
    • jakarta-regexp-X.Y.jar - 正規表現処理ライブラリである Jakarta Regexp のバイナリ配布。リクエストフィルタの実装で使われています。
    • servlets-xxxxx.jar - Tomcat の機能の一部として提供される内部 Servlet のそれぞれに関連するクラス。 対応するサービスが不要な場合に完全に削除できるように、 またセキュリティマネージャの特別な許可の対象にできるように、 これらは別々になっています。
    • tomcat-coyote.jar - Tomcat 5 の Coyote コネクタ。
    • tomcat-http11.jar - スタンドアロンの Java HTTP/1.1 コネクタ。
    • tomcat-jk2.jar - JK 2 Web サーバコネクタの Java の部分のクラス。 ApacheやiPlanet の iAS と iWSといった Web サーバの背後で Tomcat を実行できるようにします。
    • tomcat-util.jar - Tomcat コネクタの一部で必要なユーティリティクラス。
  • Shared - This class loader is the place to put classes and resources that you wish to share across ALL web applications (unless Tomcat internal classes also need access, in which case you should put them in the Common class loader instead). All unpacked classes and resources in $CATALINA_HOME/shared/classes, as well as classes and resources in JAR files under $CATALINA_HOME/shared/lib, are made visible through this class loader. If multiple Tomcat instances are run from the same binary using the $CATALINA_BASE environment variable, then this classloader repositories are relative to $CATALINA_BASE rather than $CATALINA_HOME.
  • Shared - このクラスローダは、すべての Webアプリケーションを通して共有したいクラスやリソースを配置する場所です (Tomcat 内部クラスでもアクセスが必要な場合のみ例外で、 代わりに Common クラスローダに配置すべきです)。 $CATALINA_HOME/shared/classes 上のアーカイブされないクラスやリソースすべて、 それに $CATALINA_HOME/shared/lib にある JAR ファイル内のクラスやリソースがこのクラスローダで参照可能になります。 $CATALINA_BASE 環境変数を使って同じバイナリから複数の Tomcat インスタンスを実行する場合のこのクラスローダのリポジトリは、 $CATALINA_HOME ではなく $CATALINA_BASE を基準とします。
  • WebappX - A class loader is created for each web application that is deployed in a single Tomcat 5 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.
  • WebappX - 単一の Tomcat 5 インスタンスに配備された Webアプリケーションごとにクラスローダが1つずつ生成されます。 Webアプリケーションの配置場所の /WEB-INF/classes ディレクトリにあるアーカイブされないクラスやリソースすべて、 加えて Webアプリケーションの配置場所の /WEB-INF/lib にある JAR ファイル内のクラスやリソースは、これらを含んでいる Webアプリケーションから参照可能ですが、 他の Webアプリケーションからは参照できません。

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.6). When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories 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 JDK 1.4+), the JDK 1.4 endorsed feature can be used used (see the common classloader definition above). In addition, for the following class patterns, the classloader will always delegate first (and load the class itself if no parent classloader loads it):
  • javax.*
  • org.xml.sax.*
  • org.w3c.dom.*
  • org.apache.xerces.*
  • org.apache.xalan.*
Last, any JAR containing servlet API classes will be ignored by the classloader. All other class loaders in Tomcat 5 follow the usual delegation pattern.

上で説明した通り、Webアプリケーションクラスローダは、 デフォルトの Java 2 委譲モデルとは異なっています (これは Servlet 仕様バージョン 2.3 の 9.6 節での勧告に基づくものです)。Webアプリケーションの WebappX クラスローダからのクラスのロード要求が処理される際、 このクラスローダは最初に委譲を行なわずに、 ローカルのリポジトリをチェックします。ただし例外があり、 JRE の基本クラスに属するクラスはオーバーライドできません。(JDK 1.4 以降の XML パーサコンポーネントなど) いくつかのクラスについては、JDK 1.4 の Endorsed 機能が適用可能です (詳細については上の Common クラスローダ定義をご覧下さい)。それに加えて、以下のクラスパターンでは、 クラスローダは常に最初に委譲を行ないます (さらに親のクラスローダがロードしないクラスは自らロードを行ないます)。

  • javax.*
  • org.xml.sax.*
  • org.w3c.dom.*
  • org.apache.xerces.*
  • org.apache.xalan.*
最後に、Servlet API のクラスを含む JAR ファイルすべてをこのクラスローダは無視します。 Tomcat 5 の他のクラスローダはすべて通常の委譲パターンにしたがいます。

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

したがって、クラスやリソースのロードの際のリポジトリのチェックは、 Webアプリケーションから見ると以下の順序になります。

  • Bootstrap classes of your JVM
  • System class loader classses (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_HOME/shared/classes
  • $CATALINA_HOME/shared/lib/*.jar
  • JVM のブートストラップクラス
  • System クラスローダの各クラス (上述)
  • Webアプリケーションの /WEB-INF/classes
  • Webアプリケーションの /WEB-INF/lib/*.jar
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_HOME/shared/classes
  • $CATALINA_HOME/shared/lib/*.jar
XML Parsers and JDK 1.4
XML パーサと JDK 1.4

Among many other changes, the JDK 1.4 release packages the JAXP APIs, and a version of Xerces, inside the JDK. This has impacts on applications that wish to use their own XML parser.

JDK 1.4 リリースでは多くの変更がありましたが、その一環として、JAXP API と特定のバージョンの Xerces が JDK に同梱されるようになりました。これにより、 独自の XML パーサをアプリケーションで使いたい場合に影響が出ます。

In previous versions of Tomcat 5, you could simply replace the XML parser in the $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 JDK 1.4, because the usual class loader delegation process will always choose the implementation inside the JDK in preference to this one.

Tomcat 5 以前のバージョンでは、$CATALINA_HOME/common/lib ディレクトリの XML パーサを置き換えるだけで、 すべての Webアプリケーションによって使われているパーサを変更できました。 しかしこのテクニックは、JDK 1.4 上で動かす場合には使えません。 なぜなら JDK 1.4 では、$CATALINA_HOME/common/lib よりも JDK 内の実装が通常のクラスローダの委譲過程において常に優先されるからです。

JDK 1.4 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: http://java.sun.com/j2se/1.4/docs/guide/standards/index.html.

JDK 1.4 は、JCP (Java Community Process) 以外で定義された API (つまり W3C の DOM や SAX) の置き換えを可能にする "Endorsed Standards Override Mechanism" (推奨規格標準オーバーライド機構) をサポートしています。このメカニズムは XML パーサ実装の更新でも使えます。 詳細については、 http://java.sun.com/j2se/1.4/ja/docs/ja/guide/standards/index.html (原文) をご覧下さい。

[訳者コメント: "Endorsed Standards Override Mechanism"の訳はSunのサイトでも 「推奨規格標準オーバーライド機構」と「承認済み標準のオーバーライド機構」の 2種類あります。]
[訳者コメント: Endorsed Standards API には OMG の CORBA も含まれているようなので、 i.e.(つまり)ではなくe.g.(例えば) が正しいかもしれません。]

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 では、システムプロパティ設定として
-Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed をコンテナを起動するコマンドラインに含めることによってこのメカニズムを使えます。 したがって、このディレクトリにインストールされたパーサを置き換えることができます。 この方法は JDK 1.4 のシステムでも有効です。


[訳注: この文書は高橋達男が翻訳し、 田中 ラ・マンチャ 良浩が校正しました。 日本語訳に対するコメントがあれば、report@jajakarta.orgに送って下さい。]
Copyright © 1999-2003, Apache Software Foundation