1.首先下载tomcat7源码,下载地址http://tomcat.apache.org/download-70.cgi
2.Bootstrap类是容器启动和停止的执行类。
/* * 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. */ package org.apache.catalina.startup; import java.io.File; import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.StringTokenizer; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import org.apache.catalina.Globals; import org.apache.catalina.security.SecurityClassLoad; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; /** * Bootstrap loader for Catalina. This application constructs a class loader * for use in loading the Catalina internal classes (by accumulating all of the * JAR files found in the "server" directory under "catalina.home"), and * starts the regular execution of the container. The purpose of this * roundabout approach is to keep the Catalina internal classes (and any * other classes they depend on, such as an XML parser) out of the system * class path and therefore not visible to application level classes. * * @author Craig R. McClanahan * @author Remy Maucherat * @version $Id: Bootstrap.java 1031043 2010-11-04 15:47:56Z funkman $ */ public final class Bootstrap { private static final Log log = LogFactory.getLog(Bootstrap.class); // -------------------------------------------------------------- Constants protected static final String CATALINA_HOME_TOKEN = "${" + Globals.CATALINA_HOME_PROP + "}"; protected static final String CATALINA_BASE_TOKEN = "${" + Globals.CATALINA_BASE_PROP + "}"; // ------------------------------------------------------- Static Variables /** * Daemon object used by main. */ private static Bootstrap daemon = null; // -------------------------------------------------------------- Variables /** * Daemon reference. */ private Object catalinaDaemon = null; protected ClassLoader commonLoader = null; protected ClassLoader catalinaLoader = null; protected ClassLoader sharedLoader = null; // -------------------------------------------------------- Private Methods // 初始化common,server,shared这三个类加载器,common类加载器是server和shared加载器的父加载器,类加载器就像一个沙箱一样隔离加载器类. private void initClassLoaders() { try { // common加载器加载的类对tomcat容器和web应用都可见 commonLoader = createClassLoader("common", null); if( commonLoader == null ) { // 如果没有配置文件或者配置文件值为空,则得到加载当前Bootstrap对象的类加载器. commonLoader=this.getClass().getClassLoader(); } // server加载器加载的类只对tomcat容器可见 catalinaLoader = createClassLoader("server", commonLoader); // shared加载器加载的类只对web应用可见 sharedLoader = createClassLoader("shared", commonLoader); } catch (Throwable t) { handleThrowable(t); log.error("Class loader creation threw exception", t); System.exit(1); } } private ClassLoader createClassLoader(String name, ClassLoader parent) throws Exception { /** * 得到conf/catalina.properties属性文件中name.loader的值.详见CatalinaProperties类loadProperties方法 * common类加载器的默认值是: common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar * server类加载器的默认值是: server.loader= * share类加载器的默认值是: shared.loader= */ String value = CatalinaProperties.getProperty(name + ".loader"); // 如果值为空则返回父类加载器,默认情况下server类加载器和share类加载器都是空. if ((value == null) || (value.equals(""))) return parent; // 类加载器所加载的类所在的仓库位置 ArrayList<String> repositoryLocations = new ArrayList<String>(); // 类加载器所加载的类的仓库类型,有4种类型ClassLoaderFactory.IS_URL,ClassLoaderFactory.IS_DIR,ClassLoaderFactory.IS_JAR,ClassLoaderFactory.IS_GLOB ArrayList<Integer> repositoryTypes = new ArrayList<Integer>(); int i; /** * 不同仓库以","号分隔,比如${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar */ StringTokenizer tokenizer = new StringTokenizer(value, ","); while (tokenizer.hasMoreElements()) { // 得到其中一个仓库 String repository = tokenizer.nextToken(); // 判断仓库值是否被替换过,用于记录日志 boolean replace = false; // 记录仓库的初始值 String before = repository; // 如果仓库值包含${catalina.home}字符,则用真实的值替换掉 while ((i=repository.indexOf(CATALINA_HOME_TOKEN))>=0) { replace=true; // i大于0的情况是仓库为ClassLoaderFactory.IS_URL类型的时候,比如http://localhost/${catalina.base}/lib if (i>0) { repository = repository.substring(0,i) + getCatalinaHome() + repository.substring(i+CATALINA_HOME_TOKEN.length()); } else { repository = getCatalinaHome() + repository.substring(CATALINA_HOME_TOKEN.length()); } } // 如果仓库值包含${catalina.base}字符,则用真实的值替换掉 while ((i=repository.indexOf(CATALINA_BASE_TOKEN))>=0) { replace=true; if (i>0) { repository = repository.substring(0,i) + getCatalinaBase() + repository.substring(i+CATALINA_BASE_TOKEN.length()); } else { repository = getCatalinaBase() + repository.substring(CATALINA_BASE_TOKEN.length()); } } if (replace && log.isDebugEnabled()) log.debug("Expanded " + before + " to " + repository); // Check for a JAR URL repository try { /** * 如果仓库不是ClassLoaderFactory.IS_URL类型的时候就会发生MalformedURLException异常,不会执行try下面的语句,如果是ClassLoaderFactory.IS_URL类型则记录下 * 仓库位置和仓库类型并返回解析下一个仓库 */ new URL(repository); repositoryLocations.add(repository); repositoryTypes.add(ClassLoaderFactory.IS_URL); continue; } catch (MalformedURLException e) { // Ignore } if (repository.endsWith("*.jar")) { repository = repository.substring (0, repository.length() - "*.jar".length()); repositoryLocations.add(repository); repositoryTypes.add(ClassLoaderFactory.IS_GLOB); } else if (repository.endsWith(".jar")) { repositoryLocations.add(repository); repositoryTypes.add(ClassLoaderFactory.IS_JAR); } else { repositoryLocations.add(repository); repositoryTypes.add(ClassLoaderFactory.IS_DIR); } } String[] locations = repositoryLocations.toArray(new String[0]); Integer[] types = repositoryTypes.toArray(new Integer[0]); // 根据仓库位置,类型,父类加载器创建类加载器 ClassLoader classLoader = ClassLoaderFactory.createClassLoader (locations, types, parent); // 得到Mbean服务器. MBeanServer mBeanServer = null; if (MBeanServerFactory.findMBeanServer(null).size() > 0) { mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0); } else { mBeanServer = ManagementFactory.getPlatformMBeanServer(); } // 在服务器中注册类加载器以便管理 ObjectName objectName = new ObjectName("Catalina:type=ServerClassLoader,name=" + name); mBeanServer.registerMBean(classLoader, objectName); return classLoader; } /** * Initialize daemon. */ public void init() throws Exception { // 设置catalina.home setCatalinaHome(); // 设置catalina.base setCatalinaBase(); // 初始化common,server,share类加载器,并在Mbean服务器中注册 initClassLoaders(); // 设置当前的类加载器上下文为server类加载器 Thread.currentThread().setContextClassLoader(catalinaLoader); // 通过server类加载器加载核心类 SecurityClassLoad.securityClassLoad(catalinaLoader); // Load our startup class and call its process() method if (log.isDebugEnabled()) log.debug("Loading startup class"); // 动态加载org.apache.catalina.startup.Catalina类,并创建一个实例,Catalina是一个守护进程 Class<?> startupClass = catalinaLoader.loadClass ("org.apache.catalina.startup.Catalina"); Object startupInstance = startupClass.newInstance(); // Set the shared extensions class loader,利用反射调用Catalina类的setParentClassLoader方法 if (log.isDebugEnabled()) log.debug("Setting startup class properties"); String methodName = "setParentClassLoader"; Class<?> paramTypes[] = new Class[1]; paramTypes[0] = Class.forName("java.lang.ClassLoader"); Object paramValues[] = new Object[1]; paramValues[0] = sharedLoader; Method method = startupInstance.getClass().getMethod(methodName, paramTypes); method.invoke(startupInstance, paramValues); // Catalina实例的引用 catalinaDaemon = startupInstance; } /** * 利用发射调用Catalina的load方法 */ private void load(String[] arguments) throws Exception { // Call the load() method String methodName = "load"; Object param[]; Class<?> paramTypes[]; if (arguments==null || arguments.length==0) { paramTypes = null; param = null; } else { paramTypes = new Class[1]; paramTypes[0] = arguments.getClass(); param = new Object[1]; param[0] = arguments; } Method method = catalinaDaemon.getClass().getMethod(methodName, paramTypes); if (log.isDebugEnabled()) log.debug("Calling startup class " + method); method.invoke(catalinaDaemon, param); } /** * getServer() for configtest */ private Object getServer() throws Exception { String methodName = "getServer"; Method method = catalinaDaemon.getClass().getMethod(methodName); return method.invoke(catalinaDaemon); } // ----------------------------------------------------------- Main Program /** * Load the Catalina daemon. */ public void init(String[] arguments) throws Exception { init(); load(arguments); } /** * Start the Catalina daemon. */ public void start() throws Exception { if( catalinaDaemon==null ) init(); Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null); method.invoke(catalinaDaemon, (Object [])null); } /** * Stop the Catalina Daemon. */ public void stop() throws Exception { Method method = catalinaDaemon.getClass().getMethod("stop", (Class [] ) null); method.invoke(catalinaDaemon, (Object [] ) null); } /** * Stop the standalone server. */ public void stopServer() throws Exception { Method method = catalinaDaemon.getClass().getMethod("stopServer", (Class []) null); method.invoke(catalinaDaemon, (Object []) null); } /** * 利用反射调用Catalina类的stopServer方法 */ public void stopServer(String[] arguments) throws Exception { Object param[]; Class<?> paramTypes[]; if (arguments==null || arguments.length==0) { paramTypes = null; param = null; } else { paramTypes = new Class[1]; paramTypes[0] = arguments.getClass(); param = new Object[1]; param[0] = arguments; } Method method = catalinaDaemon.getClass().getMethod("stopServer", paramTypes); method.invoke(catalinaDaemon, param); } /** * 利用反射调用Catalina类的setAwait方法 */ public void setAwait(boolean await) throws Exception { Class<?> paramTypes[] = new Class[1]; paramTypes[0] = Boolean.TYPE; Object paramValues[] = new Object[1]; paramValues[0] = Boolean.valueOf(await); Method method = catalinaDaemon.getClass().getMethod("setAwait", paramTypes); method.invoke(catalinaDaemon, paramValues); } public boolean getAwait() throws Exception { Class<?> paramTypes[] = new Class[0]; Object paramValues[] = new Object[0]; Method method = catalinaDaemon.getClass().getMethod("getAwait", paramTypes); Boolean b=(Boolean)method.invoke(catalinaDaemon, paramValues); return b.booleanValue(); } /** * Destroy the Catalina Daemon. */ public void destroy() { // FIXME } /** * Main method, used for testing only. * * @param args Command line arguments to be processed */ public static void main(String args[]) { // deamon是bootstrap初始化的标识 if (daemon == null) { // Don't set daemon until init() has completed Bootstrap bootstrap = new Bootstrap(); try { // 进行初始化操作 bootstrap.init(); } catch (Throwable t) { handleThrowable(t); t.printStackTrace(); return; } daemon = bootstrap; } try { String command = "start"; if (args.length > 0) { command = args[args.length - 1]; } if (command.equals("startd")) { args[args.length - 1] = "start"; daemon.load(args); daemon.start(); } else if (command.equals("stopd")) { args[args.length - 1] = "stop"; daemon.stop(); } else if (command.equals("start")) { daemon.setAwait(true); daemon.load(args); daemon.start(); } else if (command.equals("stop")) { daemon.stopServer(args); } else if (command.equals("configtest")) { daemon.load(args); if (null==daemon.getServer()) { System.exit(1); } System.exit(0); } else { log.warn("Bootstrap: command \"" + command + "\" does not exist."); } } catch (Throwable t) { handleThrowable(t); t.printStackTrace(); System.exit(1); } } public void setCatalinaHome(String s) { System.setProperty(Globals.CATALINA_HOME_PROP, s); } public void setCatalinaBase(String s) { System.setProperty(Globals.CATALINA_BASE_PROP, s); } /** * 如果catalina.base没有设置过,则设置catalina.home为当前工作目录 */ private void setCatalinaBase() { if (System.getProperty(Globals.CATALINA_BASE_PROP) != null) return; if (System.getProperty(Globals.CATALINA_HOME_PROP) != null) System.setProperty(Globals.CATALINA_BASE_PROP, System.getProperty(Globals.CATALINA_HOME_PROP)); else System.setProperty(Globals.CATALINA_BASE_PROP, System.getProperty("user.dir")); } /** * 如果catalina.home没有设置过,则设置catalina.home为当前工作目录 */ private void setCatalinaHome() { // if (System.getProperty(Globals.CATALINA_HOME_PROP) != null) return; File bootstrapJar = new File(System.getProperty("user.dir"), "bootstrap.jar"); // 如果当前目录下存在bootstrap.jar,则设置catalina.home为当前工作目录的上一级目录 if (bootstrapJar.exists()) { try { System.setProperty (Globals.CATALINA_HOME_PROP, (new File(System.getProperty("user.dir"), "..")) .getCanonicalPath()); } catch (Exception e) { // Ignore System.setProperty(Globals.CATALINA_HOME_PROP, System.getProperty("user.dir")); } } else { System.setProperty(Globals.CATALINA_HOME_PROP, System.getProperty("user.dir")); } } /** * Get the value of the catalina.home environment variable. */ public static String getCatalinaHome() { return System.getProperty(Globals.CATALINA_HOME_PROP, System.getProperty("user.dir")); } /** * Get the value of the catalina.base environment variable. */ public static String getCatalinaBase() { return System.getProperty(Globals.CATALINA_BASE_PROP, getCatalinaHome()); } // Copied from ExceptionUtils since that class is not visible during start private static void handleThrowable(Throwable t) { if (t instanceof ThreadDeath) { throw (ThreadDeath) t; } if (t instanceof VirtualMachineError) { throw (VirtualMachineError) t; } // All other instances of Throwable will be silently swallowed } }