StandardHost是StandardHost.Host的標(biāo)準(zhǔn)實(shí)現(xiàn),繼承自ContainerBase基容器,具備容器的功能和特征,在tomcat的結(jié)構(gòu)層次圖中位于Engine容器內(nèi)部,是Engine的子容器,是Context的父容器,負(fù)責(zé)管理多個(gè)Context,即webapp。Host是虛擬主機(jī),一個(gè)Engine下面可以有多個(gè)虛擬主機(jī)。虛擬主機(jī)主要做的任務(wù)就是負(fù)責(zé)將Engine中傳遞過(guò)來(lái)的請(qǐng)求選擇一個(gè)合適的Context處理,解決的是Context的選擇問(wèn)題,不同Conext之間數(shù)據(jù)隔離和共享、數(shù)據(jù)安全問(wèn)題,負(fù)責(zé)根據(jù)配置文件生成應(yīng)用程序的Context。
Host
是tomcat中定義的虛擬主機(jī)接口,定義了虛擬主機(jī)所需要具備的功能,例如應(yīng)用文件的磁盤路徑,Host的的基路徑,校驗(yàn)xml配置文件,發(fā)布應(yīng)用。
ContainerBase
Host繼承是基容器具備容器的功能,具有父容器引用,管理子容器和容器中的組件,還有后臺(tái)任務(wù)處理線程。
StandardHostValve
是StandHost的basicvalve,在Host容器pipeline valve鏈的末端,負(fù)責(zé)為請(qǐng)求信息選擇一個(gè)合適的Context處理,控制權(quán)轉(zhuǎn)移,控制數(shù)據(jù)流向。
Host中一些重要的屬性
aliases Host的別名列表 aliasesLock 在對(duì)aliase操作時(shí)需要加鎖 appBase Host基路徑,tomcat會(huì)自動(dòng)掃描路徑下面的應(yīng)用讓后啟動(dòng) configClass Host配置文件處理器類 contextClass Host的Context實(shí)例類,用于生產(chǎn)Context errorReportValveClass 請(qǐng)求錯(cuò)誤處理類,例如404,502 childClassLoaders 子容器類加載器,主要是Context使用,使用不同的類加載器,起到安全隔離的作用。map(String)
請(qǐng)求信息到了Host中,需要為請(qǐng)求選擇合適的Context處理,這個(gè)方法就是完成這個(gè)功能,根據(jù)uri選擇Context。分析請(qǐng)求URI地址,分解URI,根據(jù)URI來(lái)定位在當(dāng)前Host下面的Context,并返回Context。采用的匹配策略是最精確URI匹配規(guī)則。
/** * Return the Context that would be used to process the specified * host-relative request URI, if any; otherwise return null
. * 根據(jù)uri選擇合適的Context * @param uri * Request URI to be mapped */ public Context map(String uri) { if (log.isDebugEnabled()) log.debug("Mapping request URI \'" + uri + "\'"); if (uri == null) return (null); // Match on the longest possible context path prefix if (log.isTraceEnabled()) log.trace(" Trying the longest context path prefix"); Context context = null; String mapuri = uri; //最精確uri匹配原則 while (true) { context = (Context) findChild(mapuri); if (context != null) break; //定義最后一個(gè) \'/\' int slash = mapuri.lastIndexOf(\'/\'); if (slash < 0) break; mapuri = mapuri.substring(0, slash); } // If no Context matches, select the default Context if (context == null) { if (log.isTraceEnabled()) log.trace(" Trying the default context"); //默認(rèn)Context context = (Context) findChild(""); } // Complain if no Context has been selected if (context == null) { log.error(sm.getString("standardHost.mappingError", uri)); return (null); } // Return the mapped Context (if any) if (log.isDebugEnabled()) log.debug(" Mapped to context \'" + context.getPath() + "\'"); return (context); }
init()
負(fù)責(zé)初始化Host,主要完成以下幾個(gè)步驟:
public void init() { if (initialized) return; initialized = true; // already registered. if (getParent() == null) { try { // Register with the Engine ObjectName serviceName = new ObjectName(domain + ":type=Engine"); HostConfig deployer = new HostConfig(); addLifecycleListener(deployer); if (mserver.isRegistered(serviceName)) { if (log.isDebugEnabled()) log.debug("Registering " + serviceName + " with the Engine"); mserver.invoke(serviceName, "addChild", new Object[] { this }, new String[] { "org.apache.catalina.Container" }); } } catch (Exception ex) { log.error("Host registering failed!", ex); } } if (oname == null) { // not registered in JMX yet - standalone mode try { StandardEngine engine = (StandardEngine) parent; domain = engine.getName(); if (log.isDebugEnabled()) log.debug("Register host " + getName() + " with domain " + domain); oname = new ObjectName(domain + ":type=Host,host=" + this.getName()); controller = oname; Registry.getRegistry(null, null).registerComponent(this, oname, null); } catch (Throwable t) { log.error("Host registering failed!", t); } } }
start()
負(fù)責(zé)啟動(dòng)Host,主要完成以下幾個(gè)步驟:
/** * Start this host. * * @exception LifecycleException * if this component detects a fatal error that prevents it * from being started */ public synchronized void start() throws LifecycleException { if (started) { return; } if (!initialized) init(); // Look for a realm - that may have been configured earlier. // If the realm is added after context - it\'ll set itself. if (realm == null) { ObjectName realmName = null; try { realmName = new ObjectName(domain + ":type=Realm,host=" + getName()); if (mserver.isRegistered(realmName)) { mserver.invoke(realmName, "init", new Object[] {}, new String[] {}); } } catch (Throwable t) { log.debug("No realm for this host " + realmName); } } // Set error report valve if ((errorReportValveClass != null) && (!errorReportValveClass.equals(""))) { try { boolean found = false; if (errorReportValveObjectName != null) { ObjectName[] names = ((StandardPipeline) pipeline) .getValveObjectNames(); for (int i = 0; !found && i < names.length; i++) if (errorReportValveObjectName.equals(names[i])) found = true; } if (!found) { Valve valve = (Valve) Class.forName(errorReportValveClass) .newInstance(); addValve(valve); errorReportValveObjectName = ((ValveBase) valve) .getObjectName(); } } catch (Throwable t) { log.error(sm.getString( "standardHost.invalidErrorReportValveClass", errorReportValveClass), t); } } if (log.isDebugEnabled()) { if (xmlValidation) log.debug(sm.getString("standardHost.validationEnabled")); else log.debug(sm.getString("standardHost.validationDisabled")); } super.start(); }
stop()負(fù)責(zé)停止Host,使用基類中的stop()方法
destroy()
負(fù)責(zé)銷毀Host,清理資源占用,主要有以下幾個(gè)步驟:
public void destroy() throws Exception { // destroy our child containers, if any Container children[] = findChildren(); super.destroy(); for (int i = 0; i < children.length; i++) { if (children[i] instanceof StandardContext) ((StandardContext) children[i]).destroy(); } }
MemoryLeakTrackingListener
該類的是用來(lái)基類Context類加載的,不同的Context可能由不同的類加載器。
Host是tomcat中定義用來(lái)管理Context的容器,StandardHost是Host的標(biāo)準(zhǔn)實(shí)現(xiàn),完成對(duì)Context的管理,接收父容器Engine傳遞過(guò)來(lái)的請(qǐng)求信息,然后根據(jù)URI選擇合適的Context處理請(qǐng)求,Host還維護(hù)在基路徑下面的所有的webapp,為他們生成Context。通過(guò)類加載的特性來(lái)隔離不同Context在應(yīng)用層面上面的數(shù)據(jù)安全,不同Context直接不能直接訪問(wèn)。