首先,调用init()方法逐级初始化,接着调用start()方法进行启动,同时,每次调用伴随着生命周期状态变更事件的触发。
- org.apache.catalina.startup.Bootstrap的程序入口main方法,具体实现如下:
- public static void main(String args[]) {
-
- 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;
- } else {
- // When running as a service the call to stop will be on a new
- // thread so make sure the correct class loader is used to prevent
- // a range of class not found exceptions.
- Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
- }
-
- 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) {
- // Unwrap the Exception for clearer error reporting
- if (t instanceof InvocationTargetException &&
- t.getCause() != null) {
- t = t.getCause();
- }
- handleThrowable(t);
- t.printStackTrace();
- System.exit(1);
- }
-
- }
- org.apache.catalina.startup.Bootstrap的初始化方法,具体实现如下:
- public void init() throws Exception {
- // 1、设置catalina.home的配置:将catalina.home系统属性设置为当前工作目录(如果尚未设置)。
- setCatalinaHome();
- // 2、设置catalina.base的配置:如果没有设置的话,将当前的工作目录为了catalina.base的设置
- setCatalinaBase();
- // 3、初始化类加载器:commonLoader、catalinaLoader、sharedLoader
- initClassLoaders();
-
- Thread.currentThread().setContextClassLoader(catalinaLoader);
-
- SecurityClassLoad.securityClassLoad(catalinaLoader);
-
- // 加载我们的启动类并调用其process()方法
- if (log.isDebugEnabled())
- log.debug("Loading startup class");
- //4、加载启动类
- Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");
- //5、实例化启动类
- Object startupInstance = startupClass.newInstance();
-
- if (log.isDebugEnabled())
- log.debug("Setting startup class properties");
-
- //6、设置方法参数
- 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);
- // 7、调用启动类的setParentClassLoader方法设置共享扩展类加载器
- method.invoke(startupInstance, paramValues);
-
- catalinaDaemon = startupInstance;
-
- }
- org.apache.catalina.startup.Bootstrap的start()方法,具体实现如下:
- /**
- * Start the Catalina daemon.
- */
- public void start() throws Exception {
- // 如果启动类为实例化,则调用init()方法
- if( catalinaDaemon==null ) init();
-
- //获取启动类的start方法
- Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null);
- //调用启动类的start方法,即调用org.apache.catalina.startup.Catalina的start()方法
- method.invoke(catalinaDaemon, (Object [])null);
-
- }
(编辑:青岛站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|