读书人

tomcat源码学习二(tomcat的启动1)

发布时间: 2012-11-20 09:55:43 作者: rapoo

tomcat源码学习2(tomcat的启动1)

1.入口类及main方法

tomcat的入口类是org.apache.catalina.startup.BootStrap,进入其main方法,可以看到它首先实例化,然后进行初始化。

         if (daemon == null) {            daemon = new Bootstrap();            try {            //因为在启动时设置了catalina.home属性,所以System.getProperty("catalina.home")可以直接获取到这个属性值.                daemon.init();            } catch (Throwable t) {                t.printStackTrace();                return;            }        }
?

2.init方法

init方法首先设置catalina的家目录,这里调用了两个方法

setCatalinaHome();
setCatalinaBase();

 private void setCatalinaHome() {        if (System.getProperty("catalina.home") != null)            return;        File bootstrapJar =             new File(System.getProperty("user.dir"), "bootstrap.jar");        if (bootstrapJar.exists()) {            try {                System.setProperty                    ("catalina.home",                      (new File(System.getProperty("user.dir"), ".."))                     .getCanonicalPath());            } catch (Exception e) {                // Ignore                System.setProperty("catalina.home",                                   System.getProperty("user.dir"));            }        } else {        System.getProperty("catalina.config");        }    }?

?在前一篇文件中我讲到在启动tomcat时,会添加一个参数-Dcatalina.home="**************************",这个的意思是说为系统添加一个属性,相当于System.setProperty("catalina.home","**************************"),可见此方法在执行到第一句时无意外的话就会返回了,因为catalina.home己经有值了。对于另一个方法,也很明确

private void setCatalinaBase() {        if (System.getProperty("catalina.base") != null)            return;        if (System.getProperty("catalina.home") != null)            System.setProperty("catalina.base",                               System.getProperty("catalina.home"));        else            System.setProperty("catalina.base",                               System.getProperty("user.dir"));    }

?如果catalina.home有值的话就把它给catalina.base

设置好家目录后,就开始初始化类加载器,这个时候前面设置的家目录就开始起作用了。

这个方法会被调用,initClassLoaders()它本身没什么,它又接着调用createClassLoader()方法,在这个方法里会去读conf/catalina.properties里面的内容

String value = CatalinaProperties.getProperty(name + ".loader");

?这里的CatalinaProperties会首先执行一个static方法loadProperties(),用于加载conf/catalina.properties里面的内容,

?先得到catalina.properties的输入流,然后加载,最后将所有的属性添加到系统属性中

 File home = new File(getCatalinaBase());                File conf = new File(home, "conf");                File properties = new File(conf, "catalina.properties");                is = new FileInputStream(properties); properties = new Properties();                properties.load(is);                is.close();
?
Enumeration enumeration = properties.propertyNames();        while (enumeration.hasMoreElements()) {            String name = (String) enumeration.nextElement();            String value = properties.getProperty(name);            if (value != null) {                System.setProperty(name, value);//一个一个添加到系统属性当中            }        }

?接下来通过反射机制得到Catalina类的实例:

Class startupClass =            catalinaLoader.loadClass            ("org.apache.catalina.startup.Catalina");        Object startupInstance = startupClass.newInstance();

?3.开启执行启动方法

找到main方法中的

 daemon.setAwait(true);                daemon.load(args);                daemon.start();

?这几个方法内部都是通过反射机制调用Catalina的相关方法实现,这里我们主要看start方法,

public void start()        throws Exception {        if( catalinaDaemon==null ) init();//这里的catalinaDaemon是一个Catalina类的实例,在init方法中可以看出来。        Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null);        method.invoke(catalinaDaemon, (Object [])null);    }

?可以看出此方法最后调用了Catalina实例的start方法。

进入start方法,可以看到它首先会执行load方法,load方法首先会去获取conf/server.xml配置文件的路径,

file = configFile();            inputStream = new FileInputStream(file);            inputSource = new InputSource("file://" + file.getAbsolutePath());//上面得到路径inputSource.setByteStream(inputStream);            digester.push(this);            digester.parse(inputSource);            inputStream.close();//下面负责解析server.xml并同时实例化Server

?实例化是通过Digester类完成,它可以实现xml与javaBean之间的转化,想了解的可以参考这里

http://software.ccidnet.com/art/322/20021125/31671_2.html

最后调用server的start方法启动tomcat

if (server instanceof Lifecycle) {            try {                ((Lifecycle) server).start();            } catch (LifecycleException e) {                log.error("Catalina.start: ", e);            }        }
?

?

?

?

/** * Return specified property value. */ public static String getProperty(String name) { //为什么使用properties而不使用System.getProperty(name)呢 return properties.getProperty(name); } /** * Return specified property value. */ public static String getProperty(String name) { //为什么使用properties而不使用System.getProperty(name)呢 return properties.getProperty(name); }
好久没看这个了,如果没记错的话,除了系统属性之外在启动时还添加了一些其它的属性进来了,如果你用System.getProperty(name)来获取的话,有些属性就拿不到了啊 3 楼 EnjoyInwind 2012-08-17 谢谢分享,学习啦

读书人网 >软件架构设计

热点推荐