Tomact的启动开始于Bootstrap.java,在其init()中,首先要做的就是
setCatalinaHome();
setCatalinaBase();
initClassLoaders();
目的就是将tomcat启动的环境设置好,在进行classloader。
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.setProperty("catalina.home",
System.getProperty("user.dir"));
}
}
判断user.dir\bootstrap.jar存在否来设置catalina.home。
在initClassLoaders();中调用createClassLoader。loader class之前又要读取properties,于是看String value = CatalinaProperties.getProperty(name + ".loader");
public class CatalinaProperties {
// ------------------------------------------------------- Static Variables
private static final org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( CatalinaProperties.class );
private static Properties properties = null;
static {
loadProperties();
}
// --------------------------------------------------------- Public Methods
/**
* Return specified property value.
*/
public static String getProperty(String name) {
return properties.getProperty(name);
}
/**
* Return specified property value.
*/
public static String getProperty(String name, String defaultValue) {
return properties.getProperty(name, defaultValue);
}
// --------------------------------------------------------- Public Methods
/**
* Load properties.
*/
private static void loadProperties() {
InputStream is = null;
Throwable error = null;
try {
String configUrl = getConfigUrl();
if (configUrl != null) {
is = (new URL(configUrl)).openStream();
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
if (is == null) {
try {
File home = new File(getCatalinaBase());
File conf = new File(home, "conf");
File properties = new File(conf, "catalina.properties");
is = new FileInputStream(properties);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
if (is == null) {
try {
is = CatalinaProperties.class.getResourceAsStream
("/org/apache/catalina/startup/catalina.properties");
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
if (is != null) {
try {
properties = new Properties();
properties.load(is);
is.close();
} catch (Throwable t) {
error = t;
}
}
if ((is == null) || (error != null)) {
// Do something
log.warn("Failed to load catalina.properties", error);
// That's fine - we have reasonable defaults.
properties=new Properties();
}
// Register the properties as system properties
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
String value = properties.getProperty(name);
if (value != null) {
System.setProperty(name, value);
}
}
}
/**
* Get the value of the catalina.home environment variable.
*/
private static String getCatalinaHome() {
return System.getProperty("catalina.home",
System.getProperty("user.dir"));
}
/**
* Get the value of the catalina.base environment variable.
*/
private static String getCatalinaBase() {
return System.getProperty("catalina.base", getCatalinaHome());
}
/**
* Get the value of the configuration URL.
*/
private static String getConfigUrl() {
return System.getProperty("catalina.config");
}
}
CatalinaProperties 的核心是静态函数loadProperties(),在这里读取真正意义上的properties并注册