tomcat源码分析_Tomcat源码分析(一)--Tomcat的初始化
生活随笔
收集整理的這篇文章主要介紹了
tomcat源码分析_Tomcat源码分析(一)--Tomcat的初始化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Tomcat的初始化--init
Bootstrap入口
//整個Tomcat的入口,在這之前會先調用靜態代碼塊 public static void main(String args[]) {synchronized (daemonLock) {if (daemon == null) {// Don't set daemon until init() has completedBootstrap 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";//先load再startdaemon.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();if (null == daemon.getServer()) {System.exit(1);}} 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 reportingif (t instanceof InvocationTargetException &&t.getCause() != null) {t = t.getCause();}handleThrowable(t);t.printStackTrace();System.exit(1);} }Bootstrap的靜態代碼塊
//靜態代碼塊的主要功能是設置工作目錄catalinaBaseFile和安裝目錄catalinaHomeFile static {// Will always be non-nullString userDir = System.getProperty("user.dir");// Home firstString home = System.getProperty(Globals.CATALINA_HOME_PROP);File homeFile = null;if (home != null) {File f = new File(home);try {homeFile = f.getCanonicalFile();} catch (IOException ioe) {homeFile = f.getAbsoluteFile();}}if (homeFile == null) {// First fall-back. See if current directory is a bin directory// in a normal Tomcat installFile bootstrapJar = new File(userDir, "bootstrap.jar");if (bootstrapJar.exists()) {File f = new File(userDir, "..");try {homeFile = f.getCanonicalFile();} catch (IOException ioe) {homeFile = f.getAbsoluteFile();}}}if (homeFile == null) {// Second fall-back. Use current directoryFile f = new File(userDir);try {homeFile = f.getCanonicalFile();} catch (IOException ioe) {homeFile = f.getAbsoluteFile();}}catalinaHomeFile = homeFile;System.setProperty(Globals.CATALINA_HOME_PROP, catalinaHomeFile.getPath());// Then baseString base = System.getProperty(Globals.CATALINA_BASE_PROP);if (base == null) {catalinaBaseFile = catalinaHomeFile;} else {File baseFile = new File(base);try {baseFile = baseFile.getCanonicalFile();} catch (IOException ioe) {baseFile = baseFile.getAbsoluteFile();}catalinaBaseFile = baseFile;}System.setProperty(Globals.CATALINA_BASE_PROP, catalinaBaseFile.getPath()); }Bootstrap中的init方法
//這里的主要功能是初始化了三個類加載器和創建了Catalina對象//ClassLoader commonLoader = null;//ClassLoader catalinaLoader = null;//ClassLoader sharedLoader = null; public void init() throws Exception {//初始化三個類加載器initClassLoaders();Thread.currentThread().setContextClassLoader(catalinaLoader);SecurityClassLoad.securityClassLoad(catalinaLoader);// Load our startup class and call its process() methodif (log.isDebugEnabled())log.debug("Loading startup class");//通過反射創建Catalina對象Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");Object startupInstance = startupClass.getConstructor().newInstance();// Set the shared extensions class loaderif (log.isDebugEnabled())log.debug("Setting startup class properties");//為Catalina對象設置類加載器sharedLoaderString 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);catalinaDaemon = startupInstance; }- 回到main入口方法繼續看
Bootstrap中的load方法
//這里是通過反射轉調Catalina對象的load方法 private void load(String[] arguments) throws Exception {// Call the load() methodString 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); }Catalina中的load方法
public void load() {if (loaded) {return;}loaded = true;long t1 = System.nanoTime();//初始化temp目錄initDirs();// Before digester - it may be neededinitNaming();// Create and execute our DigesterDigester digester = createStartDigester();InputSource inputSource = null;InputStream inputStream = null;File file = null;try {try {file = configFile();inputStream = new FileInputStream(file);inputSource = new InputSource(file.toURI().toURL().toString());} catch (Exception e) {if (log.isDebugEnabled()) {log.debug(sm.getString("catalina.configFail", file), e);}}if (inputStream == null) {try {inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());} catch (Exception e) {if (log.isDebugEnabled()) {log.debug(sm.getString("catalina.configFail",getConfigFile()), e);}}}// This should be included in catalina.jar// Alternative: don't bother with xml, just create it manually.if (inputStream == null) {try {inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");inputSource = new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());} catch (Exception e) {if (log.isDebugEnabled()) {log.debug(sm.getString("catalina.configFail","server-embed.xml"), e);}}}if (inputStream == null || inputSource == null) {if (file == null) {log.warn(sm.getString("catalina.configFail",getConfigFile() + "] or [server-embed.xml]"));} else {log.warn(sm.getString("catalina.configFail",file.getAbsolutePath()));if (file.exists() && !file.canRead()) {log.warn("Permissions incorrect, read permission is not allowed on the file.");}}return;}try {inputSource.setByteStream(inputStream);digester.push(this);digester.parse(inputSource);} catch (SAXParseException spe) {log.warn("Catalina.start using " + getConfigFile() + ": " +spe.getMessage());return;} catch (Exception e) {log.warn("Catalina.start using " + getConfigFile() + ": " , e);return;}} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {// Ignore}}}getServer().setCatalina(this);getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());// Stream redirectioninitStreams();// Start the new servertry {getServer().init();} catch (LifecycleException e) {if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {throw new java.lang.Error(e);} else {log.error("Catalina.start", e);}}long t2 = System.nanoTime();if(log.isInfoEnabled()) {log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");} }總結
以上是生活随笔為你收集整理的tomcat源码分析_Tomcat源码分析(一)--Tomcat的初始化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 稀疏多项式的运算用链表_用漫画告诉你—什
- 下一篇: html 分割线_零基础网页设计/前端/