Apache Tomcat 6.0 Class Loader HOW-TO翻译

Like many server applications, Tomcat 6 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 6安装了各种不同类型的类加载器(即实现了java.lang.ClassLoader的类)来允许容器的不同部分,以及运行在容器上的web应用程序,可以访问不同贮存部分的类和资源。这个机制是用来提供Servlet Specification, 2.4版——特别是9.4 和 9.6章节里定义的功能。

    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.
    在J2SE 2 (即J2SE 1.2 或者以后的版本) 环境中, 类装载器们被组织在一个有父子关系的树状结构中。一般, 一个类装载器被请求载入一个类或者资源, 它先将这个请求转发给父亲类装载器, 并且只有在父亲不能装载这个类或者资源的时候才在自己的路径去搜寻。如下描述的,web应用程序类装载模型和这有一些小的差别,但是基本原理是一样的。
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:
当Tomcat 6启动时,它创建一组类装载器,这些类装载器被组织成如下的父子关系,父类装载器在子类装载器之上:

      Bootstrap
          |
       System
          |
       Common
       /     \
  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.
每一个类装载器的特征,包括可以被使用的类和资源的来源,在下面的章节有详细的讨论。
Class Loader Definitions
    As indicated in the diagram above, Tomcat 6 creates the following class loaders as it is initialized:
如上面的图表显示,Tomcat 6在它启动时产生如下的类装载器:
        * 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虚拟器提供的基本的运行时间类,加上在System Extensions 目录( $JAVA_HOME/jre/lib/ext )里的JAR文件中所有的类。注意——也许有些Java虚拟器(JVMs)包含不止一个Bootstrap类装载器,或者也许是不可见的(作为一个类装载器)。

        * 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 6 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:
        * System - 这个类装载器通常是以CLASSPATH环境变量的内容为基础来初始化的。所有的这些类既可被Tomcat内部classes使用,又可被web应用程序使用。不过,标准的Tomcat 6启动脚本( $CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%\bin\catalina.bat )完全忽略了CLASSPATH环境变量自身的内容,相反从下面的贮藏室去建造系统类装载器:
              o $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.包含用来初始化Tomcat 6服务器的main()方法,以及它所依赖的类装载器执行类。
              o $CATALINA_HOME/bin/tomcat-juli.jar - Package renamed Jakarta commons logging API, and java.util.logging LogManager.包含重命名的包Jakarta commons logging API,以及java.util.logging 的LogManager
        * 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/lib, as well as classes and resources in JAR files are made visible through this class loader. By default, that includes the following:
        * Common - 这个类加载器包含额外的类,这些类可以被Tomcat内部类使用也可以被所有的web应用程序使用. 一般的, 应用程序的类不要放在这里. 这个类加载器可以看到所有在$CATALINA_HOME/lib目录下没有打包的类和资源,和JAR文件中的类和资源,默认包含下面这些:
              o annotations-api.jar - JEE annotations classes.
              o catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 6.Tomcat 6中Catalina servlet容器的实现部分
              o catalina-ant.jar - Tomcat Catalina Ant tasks.
              o catalina-ha.jar - High availability package.
              o catalina-tribes.jar - Group communication package.
              o el-api.jar - EL 2.1 API.
              o jasper.jar - Jasper 2 Compiler and Runtime.
              o jasper-el.jar - Jasper 2 EL implementation.
              o jasper-jdt.jar - Eclipse JDT 3.2 Java compiler.
              o jsp-api.jar - JSP 2.1 API.
              o servlet-api.jar - Servlet 2.5 API.
              o tomcat-coyote.jar - Tomcat connectors and utility classes.
              o tomcat-dbcp.jar - package renamed database connection pool based on Commons DBCP.基于Commons DBCP的数据库连接池
              o 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.为其他语言提供的可选的资源包。由于默认的资源包的内容也被包含在各个单独的jar文件中,如果没有国际化的需求,这些可选的资源包可以被安全的删除
        * 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 /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.为在单个Tomcat 6实例中被部署的每一个网络程序所产生的类装载器。在你的网络程序档案/WEB-INF/classes目录里所有的未打包的类和资源,以及web应用程序档案/WEB-INF/lib目录下JAR文件里的类和资源,可被这个Webapp里的程序使用,而不能其他程序使用。

    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 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 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.
    正如上面所讲的,web应用程序类加载器和默认的java 2 委派模型不同。(这是参照Servlet规范的建议, version 2.3,section 9.7.2 Web Application Classloader)。当web应用程序的WebappX类加载器加载一个类的时候,这个WebappX类加载器先在本地路径中查找,而不是委派给父亲. 这里有个例外。那些属于JRE基础类的部分不能被覆盖. 对于有些类 (例如J2SE 1.4+中的XML解析组件), J2SE 1.4 "授权标准覆盖机制"可以被使用. 最后, 任何包含servlet API 类的JAR文件被类加载器忽略。Tomcat 6中所有其他的类加载器依照通用的委派模型.

    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/lib
        * $CATALINA_HOME/lib/*.jar

XML Parsers and JSE 5

    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.
    JSE 5 内置了JAXP APIs, 和Xerces的一个版本。这影响到了希望使用自己的XML解析器的应用程序。

    In previous versions of Tomcat, 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 JSE 5, because the usual class loader delegation process will always choose the implementation inside the JDK in preference to this one.
    在以前的Tomcat版本中, 你可以简单的覆盖$CATALINA_HOME/common/lib目录下的XML解析器,就可以改变所有的web应用程序的用到的解析器. 但是, 在你运行JSE 5时,这个办法没用 , 因为常规的类装载器委派过程将总是优先选择JDK内部的实现.

    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: http://java.sun.com/j2se/1.5/docs/guide/standards/index.html.
    JDK 1.5 支持一种叫做 "授权标准覆盖机制"的技术来允许替换为JCP的外部的API (例如:DOM and SAX from W3C). 这还可以被用来更新XML解析器的实现. 更多信息请参考: http://java.sun.com/j2se/1.5/docs/guide/standards/index.html.

    Tomcat utilizes this mechanism by including the system property setting -Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS in the command line that starts the container.
    Tomcat通过在启动容器的命令行里加入系统属性设置-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS 来利用这个机制。
Running under a security manager

    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 Security Manager HOW-TO for further information.
    当在一个安全管理器下面运行时,从什么地方来的类可以被装载依赖于你的政策文件的具体内容。更多信息请参看Security Manager HOW-TO

猜你喜欢

转载自lemanzk.iteye.com/blog/1025577
今日推荐