我們在代碼中為了獲取某個配置文件路徑下的文件經常會這么寫
String tomcatPath = System.getProperty("catalina.home") + "/webapps/axis2/WEB-INF/conf/"; tomcatPath = tomcatPath.replace("/", File.separator); //使用此方法是為了區分unix系統與windows, //File.separator UNIX中為/,而WINDOWS下為\
而在檢查環境變量的時候可能會發現,系統中並不存在catalina.home或者說並不存在CATALINA_HOME環境變量,那么我們在運行時成功的得到了CATALINA_HOME路徑,
並且如果多個tomcat實例存在,那么可以得到各自tomcat實例下安裝路徑對應的路徑。
這是因為System.getProperty嚴格意義上來講指AppClassLoader得到的property,即程序運行時的環境變量。
例如在TOMCAT運行時中,TOMCAT啟動之初便會設置環境變量catalina.home。
所以可以得到tomcat實例下對應的路徑。
參考:
[tomcat7源碼學習]初始化之catalina.home和catalina.base
初始化之catalina.home和catalina.base
首先先看一下這兩個參數在tomcat源碼中的注釋(參考類Globals
):
/** * Name of the system property containing * the tomcat product installation path */ public static final String CATALINA_HOME_PROP = "catalina.home"; /** * Name of the system property containing * the tomcat instance installation path */ public static final String CATALINA_BASE_PROP = "catalina.base";
就兩個差異,一個是product,一個是instance。
再看一下RUNNING.txt中的說明
================================================== Advanced Configuration - Multiple Tomcat Instances ================================================== In many circumstances, it is desirable to have a single copy of a Tomcat binary distribution shared among multiple users on the same server. To make this possible, you can set the CATALINA_BASE environment variable to the directory that contains the files for your 'personal' Tomcat instance. When running with a separate CATALINA_HOME and CATALINA_BASE, the files and directories are split as following: In CATALINA_BASE: * bin - Only the following files: * setenv.sh (*nix) or setenv.bat (Windows), * tomcat-juli.jar The setenv scripts were described above. The tomcat-juli library is documented in the Logging chapter in the User Guide. * conf - Server configuration files (including server.xml) * lib - Libraries and classes, as explained below * logs - Log and output files * webapps - Automatically loaded web applications * work - Temporary working directories for web applications * temp - Directory used by the JVM for temporary files (java.io.tmpdir) In CATALINA_HOME: * bin - Startup and shutdown scripts The following files will be used only if they are absent in CATALINA_BASE/bin: setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar * lib - Libraries and classes, as explained below * endorsed - Libraries that override standard "Endorsed Standards" libraries provided by JRE. See Classloading documentation in the User Guide for details. By default this "endorsed" directory is absent.
大概意思就是
CATALINA_BASE
:是實例配置位置,也就是一個tomcat可以配置多個實例,實例里面有自己的配置CATALINA_HOME
:是tomcat安裝位置
tomcat內部設置
1.從入口org.apache.catalina.startup.main方法開始跟蹤,
Bootstrap bootstrap = new Bootstrap(); try { bootstrap.init(); } catch (Throwable t) { handleThrowable(t); t.printStackTrace(); return; } daemon = bootstrap;
這是tomcat7第一次啟動時最先執行的代碼。
2.進入init,就是一堆初始化了,最先執行的
// Set Catalina path setCatalinaHome(); setCatalinaBase();
3.進入setCatalinaHome()
if (System.getProperty(Globals.CATALINA_HOME_PROP) != null) return; File bootstrapJar = new File(System.getProperty("user.dir"), "bootstrap.jar"); 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")); }
它的步驟是:
1.從system中取,如果有就直接返回。
2.如果bootstrap.jar在當前工作目錄,就取上一級,返回。
3.如果bootstrap.jar沒有在當前工作目錄,那么就設置
CATALINA_HOME
為當前工作目錄
4.進入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"));
它的步驟是:
1.從system中取,如果有就直接返回。
2.如果已經設置了
CATALINA_HOME
,就設置CATALINA_BASE
為CATALINA_HOME
,返回。3.設置
CATALINA_BASE
為當前工作目錄
總結
1.如果我們安裝的tomcat就只有一個實例,就只需要配置CATALINA_HOME
2.有時候項目太多,它們相互之間的一些配置又有些不同,我們就可以考慮用多個實例,而不必去復制多個tomcat了。實例配置了之后,在啟動時只需要指定CATALINA_BASE
就可以了