tomcat(15)Digester库
生活随笔
收集整理的這篇文章主要介紹了
tomcat(15)Digester库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【0】README 0.1)本文部分文字描述轉(zhuǎn)自 “how tomcat works”,旨在學(xué)習(xí)?“tomcat(15)Digester庫”?的基礎(chǔ)知識;
2)problem+solution:
2.1)problem:如下面的代碼,Bootstrap類實例化連接器,servlet容器,Wrapper容器和其它組件,如設(shè)置連接器的關(guān)聯(lián)頂層容器,通過set方法將它們聯(lián)系起來;如監(jiān)聽器組件通過addLifecycleListener來設(shè)置等等。這種配置應(yīng)用程序的方法有一個明顯的缺點:即所有的配置都必須硬編碼。調(diào)整組件配置或?qū)傩灾刀急仨氁匦戮幾gBootstrap類。 2.2)solution:Tomcat使用了一種更加優(yōu)雅的配置方式,即使用一個名為server.xml 的XML 文檔來對應(yīng)用程序進行配置。server.xml文件中的每個元素都會轉(zhuǎn)換為一個java 對象,元素的屬性會用于設(shè)置java對象 的屬性。這樣,就可以通過簡單地編輯 server.xml文件來修改tomcat的配置了;
看個荔枝)如server.xml文件中的 Context元素表示一個Context實例:<context/>;若要為 Context實例設(shè)置path屬性和 docBase屬性,使用這樣的配置:
<context docBase="myApp" path="/myApp" />
Attention)
A1)tomcat使用了開源庫Digester來將XML 文檔中的元素轉(zhuǎn)換成 java 對象;(干貨——開源庫Digester的作用) A2)用來配置web 應(yīng)用程序的XML 文件的名稱是 web.xml,該文件位于web 應(yīng)用程序的WEB-INF 目錄下;(干貨——引入了大家熟悉的web.xml)
public final class Bootstrap1 {public static void main(String[] args) {//invoke: http://localhost:8080/app1/Primitive or http://localhost:8080/app1/ModernSystem.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Wrapper wrapper1 = new StandardWrapper();wrapper1.setName("Primitive");//wrapper1.setServletClass("servlet.PrimitiveServlet");wrapper1.setServletClass("PrimitiveServlet");Wrapper wrapper2 = new StandardWrapper();wrapper2.setName("Modern");//wrapper2.setServletClass("servlet.ModernServlet");wrapper2.setServletClass("ModernServlet");Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/app1");context.setDocBase("app1");context.addChild(wrapper1);context.addChild(wrapper2);LifecycleListener listener = new SimpleContextConfig();((Lifecycle) context).addLifecycleListener(listener);Host host = new StandardHost();host.addChild(context);host.setName("localhost");host.setAppBase("webapps");Loader loader = new WebappLoader();context.setLoader(loader);// context.addServletMapping(pattern, name);context.addServletMapping("/Primitive", "Primitive");context.addServletMapping("/Modern", "Modern");connector.setContainer(host);try {connector.initialize();((Lifecycle) connector).start();((Lifecycle) host).start(); // 與以往的Bootstrap.java不同的是,這里是host.start() 而不是 context.start()// make the application wait until we press a key.System.in.read();((Lifecycle) host).stop();}catch (Exception e) {e.printStackTrace();}}
}
【1】Digester庫
1)intro:Digester是 Apache 下Jakarta項目下的子項目Commons項目下的一個開源項目;
2)Digester API包含3個包:三者都被打包到 commons-digester.jar 文件中;(package list)
package1)org.apache.commons.digester:該包提供了基于規(guī)則的,可處理任意XML 文檔的類; package2)org.apache.commons.digester.rss:該包包含一些可以用來解析與很多新聞源使用的RSS(Rich Site Summary,富站點摘要)格式兼容的XML文檔的例子; package3)org.apache.commons.digester.xmlrules:該包為 Digester庫提供了一些基于XML規(guī)則的定義;
【1.1】Digester類 1)intro:Digester類可用于解析XML 文檔;對于XMl 文檔中的每個元素,Digester對象都會檢查它是否要做事先預(yù)定義的事件。在調(diào)用Digester.parse()方法之前,需要先定義好Digester對象執(zhí)行哪些動作; 2)如何定義在Digester對象遇到某個XMl 元素時它應(yīng)該執(zhí)行什么動作呢?—— 程序員先定義好模式,然后將每個模式與一條或多條規(guī)則相關(guān)聯(lián)。XML 文檔中根元素的模式與元素的名字相同。(干貨——引入了模式,且XML 文檔中根元素的模式與元素的名字相同) 3)看個荔枝:考慮下面的 XML文檔:(example.xml) <?xml version=1.0" encoding="ISO-8859-1"> <employee firstName="pacoson" lastName="xiao"><office><address streetName="Wellington Street" streetNumber="110" /></office> </employee> 對上述代碼的分析(Analysis): A1)該XML文檔中的根元素是 employee,employee元素有一個模式, 名為 employee; A2)office元素是 employee元素的子元素,子元素的模式是由該元素的父元素的模式再加上 “/” 符號,以及該元素名稱拼接而成的,所以office元素的模式是 ?employee/office;(干貨——我們這就了解了如何從XML 文檔中推導(dǎo)出元素的模式) 4)下面討論一下規(guī)則(rules):(干貨——規(guī)則的定義,非常重要) rule1)一條規(guī)則指明了當(dāng)Digester 對象遇到了某個特殊的模式時要執(zhí)行的一個或多個動作;規(guī)則是 org.apache.commons.digester.Rule 類;Digester類可以包含0個或多個對象; rule2)Rule類有begin()方法 和 end() 方法。在開始標簽調(diào)用start()方法,結(jié)束標簽調(diào)用 end() 方法;
5)自定義自己的規(guī)則:包括創(chuàng)建對象和設(shè)置屬性值等的規(guī)則;(干貨——自定義規(guī)則包括創(chuàng)建對象+設(shè)置屬性+調(diào)用方法+創(chuàng)建對象間的關(guān)系+驗證 XML 文檔) 5.1)創(chuàng)建對象:若想要Digester對象在遇到某個特殊字符時創(chuàng)建對象,則需要調(diào)用其 addObjectCreate()方法,該方法有4個重載版本;(干貨——引入addObjectCreate()方法) public void addObjectCreate(String pattern, String className) {addRule(pattern, new ObjectCreateRule(className)); } public void addObjectCreate(String pattern, Class clazz) {addRule(pattern, new ObjectCreateRule(clazz)); } public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName)); } public void addObjectCreate(String pattern, String attributeName, Class clazz) { addRule(pattern, new ObjectCreateRule(attributeName, clazz)); } 對上述代碼的分析(Analysis): A1)需要傳入一個模式和一個Class對象或類名來調(diào)用該方法; 看個荔枝) 如我們想讓Digester對象在遇到模式employee 時,創(chuàng)建一個 mydiy.Employee 對象,則使用下面的代碼來調(diào)用 addObjectCreate()方法:
digester.addObjectCreate("employee", "mydiy.Employee.class"); 或者 digester.addObjectCreate("employee", "mydiy.Employee"); A2)addObjecdtCreate()方法的最后兩個重載版本允許在xml 文檔中定義類的名字,而無須將其作為參數(shù)傳入,這使得類名可以在運行時決定;在上述最后兩個重載方法中,參數(shù) attributeName參數(shù)指明了 XML 元素的屬性的名字,該屬性包含了將要實例化的類的名字; 看個荔枝) step1)添加創(chuàng)建對象的一條規(guī)則:digester.addObjectCreate("employee",null,"className");(屬性名是 className); step2)傳入XML 元素中的類名:?<employee firstName="pacoson" lastName="xiao" className="mydiy.employee">;如果employee元素包含 className屬性,那么該屬性指定的值會用來作為待實例化的類的名字,如果沒有包含 className屬性,則會使用默認的類名;(干貨——顯然意思是說Emplyee類需要依賴 名為className的類對象) Attention)addObjectCreate()方法創(chuàng)建的對象會被壓入到一個內(nèi)部棧中;
5.2)設(shè)置屬性:addSetProperties()方法,該方法可以使用Digester對象為創(chuàng)建的對象設(shè)置屬性。該方法的重載版本有:(干貨——引入addSetProperties()方法) public void addSetProperties(String pattern) {addRule(pattern, new SetPropertiesRule()); }public void addSetProperties( String pattern, String attributeName, String propertyName) { addRule(pattern, new SetPropertiesRule(attributeName, propertyName)); }public void addSetProperties(String pattern, String [] attributeNames, String [] propertyNames) { addRule(pattern, new SetPropertiesRule(attributeNames, propertyNames)); } 看個荔枝)考慮下面的代碼: digester.addObjectCreate("employee", "mydiy.Employee"); digester.addSetProperties("employee"); 對以上代碼的分析(Analysis): A1)上面的Digester有兩個Rule 對象,分別用來創(chuàng)建對象和設(shè)置屬性,他們都是通過employee模式觸發(fā)的。而Rule對象按照其添加到Digester實例中的順序逐個執(zhí)行。 A2)對于下面XMl 文檔中的employee 元素(該元素匹配 employee模式):?<employee firstName="pacoson", lastName="Xiao">;依據(jù)Digester的第一條rule,會創(chuàng)建 diy.Employee類的一個實例,依據(jù)第二條Rule,調(diào)用已經(jīng)實例化的Employee.setFirstName() and Employee.setLastName(),分別傳入pacoson 和 Xiao 來設(shè)置屬性; 5.3)調(diào)用方法:Digetser類允許通過添加一條Rule,使Digester 在遇到與該規(guī)則相關(guān)聯(lián)的模式時調(diào)用內(nèi)部棧最頂端對象的 某個方法。這需要用到 addCallMethod()方法,重載版本如下:(干貨——引入addCallMethod()方法) public void addCallMethod(String pattern, String methodName) {addRule( pattern, new CallMethodRule(methodName)); }public void addCallMethod(String pattern, String methodName, int paramCount) { addRule(pattern, new CallMethodRule(methodName, paramCount)); }public void addCallMethod(String pattern, String methodName, int paramCount, String paramTypes[]) { addRule(pattern, new CallMethodRule( methodName,paramCount,paramTypes));}public void addCallMethod(String pattern, String methodName,int paramCount, Class paramTypes[]) {addRule(pattern,new CallMethodRule(methodName,paramCount,paramTypes));} 5.4)創(chuàng)建對象之間的關(guān)系(干貨——Digester實例有一個內(nèi)部棧,用于臨時存儲創(chuàng)建的對象)(干貨——引入addSetNext()方法) 5.4.1)addSetNext()方法:若棧中有兩個對象,那么該方法會調(diào)用第1個對象的指定方法并將第2個對象作為參數(shù)傳入該方法來創(chuàng)建第1個對象和第2個對象的關(guān)系; public void addSetNext(String pattern, String methodName) {addRule(pattern,new SetNextRule(methodName));}public void addSetNext(String pattern, String methodName,String paramType) {addRule(pattern,new SetNextRule(methodName, paramType));} 對以上代碼的分析(Analysis):參數(shù)pattern 指明了觸發(fā)該規(guī)則的具體模式,參數(shù)methodName 是將要調(diào)用的第1個對象的方法的名稱。模式是如下格式:firstObject/secondObject; 看個荔枝)如何創(chuàng)建對象間的關(guān)系: step1)創(chuàng)建兩個對象; digester.addObjectCreate("employee", "mydiy.Employee"); digester.addObjectCreate("employee/office", "mydiy.Office"); step2)創(chuàng)建對象間的關(guān)系:需要另外定義一條規(guī)則,使用 addSetNext()方法來建立關(guān)系(調(diào)用addOffice()方法建立關(guān)系): digetster.addSetNext("employee/office", 'addOffice'); 5.5)驗證XML文檔:Digester.validating屬性指明了是否要對 XML 文檔進行有效性驗證。默認case下,其為false;setValidating()方法可以設(shè)置其值;(干貨——引入setValidating()方法)
【1.2】Digester庫荔枝1(如何使用 Digester庫動態(tài)地創(chuàng)建對象,并設(shè)置相應(yīng)的屬性) 1)源代碼 public class Test01 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee1.xml");Digester digester = new Digester();// add rules (為模式 employee 添加3條規(guī)則)digester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addCallMethod("employee", "printName");try {Employee employee = (Employee) digester.parse(file);System.out.println("First name : " + employee.getFirstName());System.out.println("Last name : " + employee.getLastName());} catch (Exception e) {e.printStackTrace();}} } <?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Brian" lastName="May"> </employee> 2)console info Creating Employee Setting firstName : Brian Setting lastName : May My name is Brian May First name : Brian Last name : May 3)info analysis:當(dāng)調(diào)用parse()方法時,它會打開指定的xml 文檔,開始解析它;(干貨——當(dāng)調(diào)用parse()方法時,它會打開指定的xml 文檔,開始解析它,只需要parse方法就可以創(chuàng)建根元素(模式)的相應(yīng)對象和其關(guān)聯(lián)對象) step1)Digester類查看 employee 元素的開始標簽,這會觸發(fā)與 employee模式關(guān)聯(lián)的3條規(guī)則,按照其被添加到 Digester 對象中的順序逐個執(zhí)行; step2)第一條規(guī)則用于創(chuàng)建Employee對象,調(diào)用構(gòu)造函數(shù),打印Creating Employee; step3)第二條規(guī)則設(shè)置 Employee對象的屬性,在employee 元素中包含兩個屬性:分別是 firstName 和 lastName, 這會調(diào)用調(diào)用的set方法,打印 Setting firstName : Brian ?Setting lastName : May; step4)第三條規(guī)則調(diào)用 Employee.printName()方法,打印M y name is Brian May; 【1.3】Digester庫荔枝2(如何創(chuàng)建兩個對象,并建立他們的關(guān)系) 1)源代碼 public class Test02 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee2.xml");Digester digester = new Digester();// add rules,添加規(guī)則(key)digester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addObjectCreate("employee/office","com.tomcat.chapter15.digestertest.Office");digester.addSetProperties("employee/office");digester.addSetNext("employee/office", "addOffice");digester.addObjectCreate("employee/office/address","com.tomcat.chapter15.digestertest.Address");digester.addSetProperties("employee/office/address");digester.addSetNext("employee/office/address", "setAddress");try {Employee employee = (Employee) digester.parse(file);ArrayList offices = employee.getOffices();Iterator iterator = offices.iterator();System.out.println("-------------------------------------------------");while (iterator.hasNext()) {Office office = (Office) iterator.next();Address address = office.getAddress();System.out.println(office.getDescription());System.out.println("Address : " + address.getStreetNumber()+ " " + address.getStreetName());System.out.println("--------------------------------");}} catch (Exception e) {e.printStackTrace();}} } <?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Freddie" lastName="Mercury"><office description="Headquarters"><address streetName="Wellington Avenue" streetNumber="223"/></office><office description="Client site"><address streetName="Downing Street" streetNumber="10"/></office> </employee> 2)console info Creating Employee Setting firstName : Freddie Setting lastName : Mercury ..Creating Office ..Setting office description : Headquarters ....Creating Address ....Setting streetName : Wellington Avenue ....Setting streetNumber : 223 ..Setting office address : ....223 Wellington Avenue Adding Office to this employee ..Creating Office ..Setting office description : Client site ....Creating Address ....Setting streetName : Downing Street ....Setting streetNumber : 10 ..Setting office address : ....10 Downing Street Adding Office to this employee ------------------------------------------------- Headquarters Address : 223 Wellington Avenue -------------------------------- Client site Address : 10 Downing Street -------------------------------- Attention)本文不對 荔枝2的實例程序進行分析了,結(jié)合荔枝1的分析,理解這個不難;
【1.4】org.apache.commons.digester.Rule類(最重要的方法start() + end())
1)intro to begin() :當(dāng)Digester實例 遇到某個XML 元素的開始標簽時,會調(diào)用它所包含的匹配Rule 對象的begin()方法:? public void begin(Attributes attributes) throws Exception {; // The default implementation does nothing} public void begin(String namespace, String name, Attributes attributes)throws Exception {begin(attributes);} 2)intro to end():當(dāng)Digester實例 遇到某個XML 元素的結(jié)束標簽時,會調(diào)用它所包含的匹配Rule 對象的end()方法: public void end() throws Exception {; // The default implementation does nothing} public void end(String namespace, String name)throws Exception {end();} 3)Digester對象是如何完成這些工作的? 當(dāng)調(diào)用Digester.addObjectCreate()方法,addCallMethod()方法,addSetNext()方法或其他方法時,都會間接地調(diào)用 Digester.addRule()方法; public void addRule(String pattern, Rule rule) {rule.setDigester(this);getRules().add(pattern, rule);}4)再次review Digester.addObjectCreate()方法的重載version: public void addObjectCreate(String pattern, String className) {addRule(pattern, new ObjectCreateRule(className)); } public void addObjectCreate(String pattern, Class clazz) {addRule(pattern, new ObjectCreateRule(clazz)); } public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName)); } public void addObjectCreate(String pattern, String attributeName, Class clazz) { addRule(pattern, new ObjectCreateRule(attributeName, clazz)); } 對以上代碼的分析(Analysis): A1)這4個重載方法都調(diào)用了addRule()方法,ObjectCreateRule類 是 Rule 類的子類,該類的實例都作為 addRule()方法的參數(shù); A2)ObjectCreateRule.start()方法 和?ObjectCreateRule.end()方法的實現(xiàn)如下: public void begin(Attributes attributes) throws Exception { //org.apache.commons.digester.ObjectCreateRule.begin().// Identify the name of the class to instantiateString realClassName = className;if (attributeName != null) {String value = attributes.getValue(attributeName);if (value != null) {realClassName = value;}}if (digester.log.isDebugEnabled()) {digester.log.debug("[ObjectCreateRule]{" + digester.match +"}New " + realClassName);}// Instantiate the new object and push it on the context stackClass clazz = digester.getClassLoader().loadClass(realClassName);Object instance = clazz.newInstance();digester.push(instance); //highlight line.}public void end() throws Exception {Object top = digester.pop(); //highlight line.if (digester.log.isDebugEnabled()) {digester.log.debug("[ObjectCreateRule]{" + digester.match +"} Pop " + top.getClass().getName());}} 對以上代碼的分析(Analysis):begin()方法的最后三行會創(chuàng)建Digester對象的一個實例,并將其壓入到 Digester對象的內(nèi)部棧中。end()方法 會將內(nèi)部站的棧頂元素彈出; 【1.5】Digester庫荔枝3:使用RuleSet(org.apache.commons.digester.RuleSet) 1)要向Digester實例添加 Rule對象,還可以調(diào)用其 addRuleSet()方法; 2)Rule對象集合是 org.apache.commons.digester.RuleSet接口的實例,該接口定義了兩個方法,分別是 addRuleInstance()方法 和 getNamespaceURI()方法; 2.1)addRuleInstance方法:將在當(dāng)前RuleSet 中的Rule對象的集合作為該方法的參數(shù)添加到 Digester實例中; 2.2)getNamespaceURI方法:返回將要應(yīng)用在 RuleSet中所有Rule 對象的命名空間的URI; 3)RuleSetBase implements RuleSet。RuleSetBase 是一個抽象類,提供了getNamespaceURI的實現(xiàn),你只需要提供addRuleInstances()方法的實現(xiàn)就可以了; public abstract class RuleSetBase implements RuleSet { protected String namespaceURI = null; public String getNamespaceURI() {return (this.namespaceURI);} public abstract void addRuleInstances(Digester digester); }4)測試用例 public class Test03 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee2.xml");Digester digester = new Digester();digester.addRuleSet(new EmployeeRuleSet());try {Employee employee = (Employee) digester.parse(file);ArrayList offices = employee.getOffices();Iterator iterator = offices.iterator();System.out.println("-------------------------------------------------");while (iterator.hasNext()) {Office office = (Office) iterator.next();Address address = office.getAddress();System.out.println(office.getDescription());System.out.println("Address : " + address.getStreetNumber()+ " " + address.getStreetName());System.out.println("--------------------------------");}} catch (Exception e) {e.printStackTrace();}} } public class EmployeeRuleSet extends RuleSetBase {public void addRuleInstances(Digester digester) {// add rulesdigester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addObjectCreate("employee/office","com.tomcat.chapter15.digestertest.Office");digester.addSetProperties("employee/office");digester.addSetNext("employee/office", "addOffice");digester.addObjectCreate("employee/office/address","com.tomcat.chapter15.digestertest.Address");digester.addSetProperties("employee/office/address");digester.addSetNext("employee/office/address", "setAddress");} } Attention)參考前面做的分析,應(yīng)該不難;
【2】ContextConfig類 1)在前面章節(jié)中,我們使用了 SimpleContextConfig 作為 StandardContext的監(jiān)聽器:其唯一用途是設(shè)置configure變量,這樣StandardContext.start()方法才能繼續(xù)執(zhí)行; public class SimpleContextConfig implements LifecycleListener {public void lifecycleEvent(LifecycleEvent event) {if (Lifecycle.START_EVENT.equals(event.getType())) {Context context = (Context) event.getLifecycle();context.setConfigured(true);}} } 2)而tomcat 的標準監(jiān)聽器:是 org.apache.catalina.startup.ContextConfig類的實例;(干貨——Tomcat標準監(jiān)聽器) 3)ContextConfig會執(zhí)行很多對 StandardContext實例來說必不可少的任務(wù)。如,與某個 StandardContext實例關(guān)聯(lián)的 ContextConfig 實例會安裝一個驗證器閥到 StandardContext的管道中。它還會添加一個許可閥(org.apache.catalina.valves.CertificateValve)到管道對象中; 4)更重要的是:ContextConfig類的實例還要讀取和解析默認的 web.xml 文件和應(yīng)用程序自定義的web.xml文件,并將xml 元素轉(zhuǎn)換為 java 對象;(干貨——引入了默認的和自定義的web.xml文件) 4.1)默認的web.xml:位于 CATALINA_HOME/conf 目錄中,其中定義并映射了很多默認的 servlet,配置了很多 MIME類型文件的映射,定義了默認的session超時時間,以及定義了歡迎文件的列表; 4.2)應(yīng)用程序的web.xml文件:位于 WEB-INF 目錄中; 5)以上兩個文件都不是必須的,即使這兩個文件沒有找到, ContextConfig 實例仍然會繼續(xù)執(zhí)行; 6)ContextConfig實例:會為每個 servlet元素創(chuàng)建一個 StandardWrapper類;(干貨——ContextConfig的作用) 7)在BootStrap程序中,需要實例化一個 ContextConfig類,并調(diào)用?addLifecycleListener方法; LifecycleListener listener = new ContextConfig();((Lifecycle) context).addLifecycleListener(listener); 7.1)在啟動 StandardContext實例時,會觸發(fā)以下事件(events): event1)BEFORE_START_EVENT; event2)START_EVENT; event3)AFTER_START_EVENT; 7.2)當(dāng)程序停止時,會觸發(fā)以下事件: event1)BEFORE_STOP_EVENT; event2)STOP_EVENT; event3)AFTER_STOP_EVENT; 8)ContextConfig 實例會對兩種事情做出相應(yīng):分別是START_EVENT and?STOP_EVENT。每次 StandardContext實例觸發(fā)事件時,會調(diào)用 ContextConfig.lifecycleEvent()方法; public void lifecycleEvent(LifecycleEvent event) { //org.apache.catalina.startup.ContextConfig.lifecycleEvent().// Identify the context we are associated withtry {context = (Context) event.getLifecycle();if (context instanceof StandardContext) {int contextDebug = ((StandardContext) context).getDebug();if (contextDebug > this.debug)this.debug = contextDebug;}} catch (ClassCastException e) {log(sm.getString("contextConfig.cce", event.getLifecycle()), e);return;}// Process the event that has occurredif (event.getType().equals(Lifecycle.START_EVENT))start(); //highlight line.else if (event.getType().equals(Lifecycle.STOP_EVENT))stop(); //highlight line.} 8.1)start()方法 (干貨——start方法會調(diào)用?defaultConfig() and?applicationConfig()方法,兩者分別用于讀取和解析默認的web.xml 和 應(yīng)用程序自定義的 web.xml) private synchronized void start() { //org.apache.catalina.startup.ContextConfig.start().if (debug > 0)log(sm.getString("contextConfig.start"));context.setConfigured(false);ok = true;// Set properties based on DefaultContextContainer container = context.getParent();if( !context.getOverride() ) {if( container instanceof Host ) {((Host)container).importDefaultContext(context);container = container.getParent();}if( container instanceof Engine ) {((Engine)container).importDefaultContext(context);}}// Process the default and application web.xml filesdefaultConfig(); // highlight line.applicationConfig(); // highlight line.if (ok) {validateSecurityRoles();}// Scan tag library descriptor files for additional listener classesif (ok) {try {tldScan();} catch (Exception e) {log(e.getMessage(), e);ok = false;}}// Configure a certificates exposer valve, if requiredif (ok)certificatesConfig();// Configure an authenticator if we need oneif (ok)authenticatorConfig();// Dump the contents of this pipeline if requestedif ((debug >= 1) && (context instanceof ContainerBase)) {log("Pipline Configuration:");Pipeline pipeline = ((ContainerBase) context).getPipeline();Valve valves[] = null;if (pipeline != null)valves = pipeline.getValves();if (valves != null) {for (int i = 0; i < valves.length; i++) {log(" " + valves[i].getInfo());}}log("======================");}// Make our application available if no problems were encounteredif (ok)context.setConfigured(true);else {log(sm.getString("contextConfig.unavailable"));context.setConfigured(false);}} 【2.1】 defaultConfig()方法 1)intro: 該方法負責(zé)讀取并解析位于 CATALINA_HOME/conf目錄下的默認web.xml 文件; private void defaultConfig() { //org.apache.catalina.startup.ContextConfig.defaultConfig().// Open the default web.xml file, if it existsFile file = new File(Constants.DefaultWebXml);// public static final String DefaultWebXml = "conf/web.xml";if (!file.isAbsolute())file = new File(System.getProperty("catalina.base"),Constants.DefaultWebXml);FileInputStream stream = null;try {stream = new FileInputStream(file.getCanonicalPath());stream.close();stream = null;} catch (FileNotFoundException e) {log(sm.getString("contextConfig.defaultMissing"));return;} catch (IOException e) {log(sm.getString("contextConfig.defaultMissing"), e);return;}// Process the default web.xml file (鎖定webDigester變量,并解析默認的 web.xml 文件)synchronized (webDigester) {try {InputSource is =new InputSource("file://" + file.getAbsolutePath());stream = new FileInputStream(file);is.setByteStream(stream);webDigester.setDebug(getDebug());if (context instanceof StandardContext)((StandardContext) context).setReplaceWelcomeFiles(true);webDigester.clear();webDigester.push(context);webDigester.parse(is);webDigester.push(null); // 解析結(jié)束.} catch (SAXParseException e) {log(sm.getString("contextConfig.defaultParse"), e);log(sm.getString("contextConfig.defaultPosition","" + e.getLineNumber(),"" + e.getColumnNumber()));ok = false;} catch (Exception e) {log(sm.getString("contextConfig.defaultParse"), e);ok = false;} finally {try {if (stream != null) {stream.close();}} catch (IOException e) {log(sm.getString("contextConfig.defaultClose"), e);}}}} 【2.2】applicationConfig()方法 1)intro:該方法處理的是 應(yīng)用程序自定義的web.xml,位于 WEB-INF 目錄中; private void applicationConfig() { //org.apache.catalina.startup.ContextConfig.applicationConfig().// Open the application web.xml file, if it existsInputStream stream = null;ServletContext servletContext = context.getServletContext();if (servletContext != null)stream = servletContext.getResourceAsStream(Constants.ApplicationWebXml); // ?public static final String ApplicationWebXml = "/WEB-INF/web.xml";if (stream == null) {log(sm.getString("contextConfig.applicationMissing"));return;}// Process the application web.xml filesynchronized (webDigester) {try {URL url =servletContext.getResource(Constants.ApplicationWebXml);InputSource is = new InputSource(url.toExternalForm());is.setByteStream(stream);webDigester.setDebug(getDebug());if (context instanceof StandardContext) {((StandardContext) context).setReplaceWelcomeFiles(true);}webDigester.clear();webDigester.push(context);webDigester.parse(is);webDigester.push(null);} catch (SAXParseException e) {log(sm.getString("contextConfig.applicationParse"), e);log(sm.getString("contextConfig.applicationPosition","" + e.getLineNumber(),"" + e.getColumnNumber()));ok = false;} catch (Exception e) {log(sm.getString("contextConfig.applicationParse"), e);ok = false;} finally {try {if (stream != null) {stream.close();}} catch (IOException e) {log(sm.getString("contextConfig.applicationClose"), e);}}}}
【2.3】創(chuàng)建 Web Digester 1)在ContextConfig 類中,使用變量 webDigester來引用一個 Digester類型的對象; private static Digester webDigester = createWebDigester();private static Digester createWebDigester() { //org.apache.catalina.startup.ContextConfig.createWebDigester().URL url = null;Digester webDigester = new Digester();webDigester.setValidating(true);url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_22);webDigester.register(Constants.WebDtdPublicId_22,url.toString());url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_23);webDigester.register(Constants.WebDtdPublicId_23,url.toString());webDigester.addRuleSet(new WebRuleSet()); // highlight line.return (webDigester);} 2)這個Digester對象用來解析默認的 web.xml 文件和應(yīng)用程序自定義的 web.xml 文件。在調(diào)用了?createWebDigester() 方法時會添加用來處理 web.xml 文件的規(guī)則;(干貨——在調(diào)用了?createWebDigester() 方法時會添加用來處理 web.xml 文件的規(guī)則,什么是規(guī)則,你懂的,前面已經(jīng)詳細介紹了Rule) Attention) A1)createWebDigester()方法調(diào)用了變量webDigester的 addRuleSet()方法,傳入了一個 org.apache.catalina.startup.WebRuleSet 類型的對象作為參數(shù); A2)WebRuleSet?類是 org.apache.commons.digester.RuleSetBase的子類; public class WebRuleSet extends RuleSetBase { //org.apache.catalina.startup.WebRuleSet A3)org.apache.catalina.startup.WebRuleSet的定義代碼見 文末,特別要注意其addRuleInstances()方法,其添加了很多規(guī)則集合;(干貨——addRuleInstances()方法添加了很多規(guī)則集合)
【3】應(yīng)用程序(本測試用例重在說明如何使用ContextConfig實例作為一個監(jiān)聽器來配置StandardContext對象)? Attention)通過以下實例,你會發(fā)現(xiàn),這與之前的Bootstrap 測試用例大有不同,以前是顯式地創(chuàng)建StandardWrapper(創(chuàng)建具體的servlet實例),而下面的測試用例采用Digester從 xml 中讀取 servlet的配置信息創(chuàng)建servlet實例,這就是為什么本文之前講那么多 Digester庫的原因; 1)測試用例 public final class Bootstrap {// invoke: http://localhost:8080/app1/Modern or // http://localhost:8080/app2/Primitive// note that we don't instantiate a Wrapper here,// ContextConfig reads the WEB-INF/classes dir and loads all servlets.public static void main(String[] args) {System.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/app1");context.setDocBase("app1");LifecycleListener listener = new ContextConfig();((Lifecycle) context).addLifecycleListener(listener);Host host = new StandardHost();host.addChild(context);host.setName("localhost");host.setAppBase("webapps");Loader loader = new WebappLoader();context.setLoader(loader);connector.setContainer(host);try {connector.initialize();((Lifecycle) connector).start();((Lifecycle) host).start();Container[] c = context.findChildren();int length = c.length;for (int i=0; i<length; i++) {Container child = c[i];System.out.println(child.getName());}// make the application wait until we press a key.System.in.read();((Lifecycle) host).stop();}catch (Exception e) {e.printStackTrace();}} } Supplement)本文習(xí)慣性地總結(jié)了上述測試用例的調(diào)用過程,如下:
S0)上述調(diào)用過程涉及到的變量ApplicationWebXml 和?DefaultWebXml,其值為: public static final String ApplicationWebXml = "/WEB-INF/web.xml";public static final String DefaultWebXml = "conf/web.xml"; // both of them are defined in org.apache.catalina.startup.Constant; S1)自定義的web.xml如下所示:該文件的文件路徑為 ?System.getProperty("user.dir")\webapps\app1\WEB-INF; S2)(tomcat使用了開源庫Digester來將XML 文檔中的元素轉(zhuǎn)換成 java 對象,觸發(fā)相應(yīng)規(guī)則如調(diào)用設(shè)置器來配置StandardContext的子容器(StandardWrapper包裝了servlet))(干貨——具體的規(guī)則包含在 WebRuleSet類中,見文末) <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-appPUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><servlet><servlet-name>Modern</servlet-name><servlet-class>ModernServlet</servlet-class></servlet><servlet><servlet-name>Primitive</servlet-name><servlet-class>PrimitiveServlet</servlet-class></servlet><servlet-mapping><servlet-name>Modern</servlet-name><url-pattern>/Modern</url-pattern></servlet-mapping><servlet-mapping><servlet-name>Primitive</servlet-name><url-pattern>/Primitive</url-pattern></servlet-mapping> </web-app> 2)console info E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src>java -cp .;lib/servlet.jar;lib/catalina_4_1_24.jar;lib/catalina-5.5.4.jar;lib/naming-common. jar;lib/commons-collections.jar;lib/naming-resources.jar;lib/commons-digester.jar;lib/catalina.jar;lib/commons-logging.jar;lib/commons-beanutils.jar;E :\bench-cluster\cloud-data-preprocess\HowTomcatWoks\webroot com.tomcat.chapter15.startup.Bootstrap HttpConnector Opening server socket on all host IP addresses HttpConnector[8080] Starting background thread WebappLoader[/app1]: Deploying class repositories to work directory E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\work\_\localhost\app1 WebappLoader[/app1]: Deploy class files /WEB-INF/classes to E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\webapps\app1\WEB-INF\classes ContextConfig[/app1]: Missing default web.xml, using application web.xml only StandardManager[/app1]: Seeding random number generator class java.security.SecureRandom StandardManager[/app1]: Seeding of random number generator has been completed Primitive Modern StandardHost[localhost]: MAPPING configuration error for request URI /Modern/app1/Modern StandardHost[localhost]: MAPPING configuration error for request URI /favicon.ico ModernServlet -- init StandardHost[localhost]: MAPPING configuration error for request URI /favicon.ico 3)訪問結(jié)果
public class WebRuleSet extends RuleSetBase { //org.apache.catalina.startup.WebRuleSetprotected String prefix = null; public WebRuleSet() { this("");} public WebRuleSet(String prefix) {super();this.namespaceURI = null;this.prefix = prefix;} public void addRuleInstances(Digester digester) { // highlight.digester.addRule(prefix + "web-app",new SetPublicIdRule(digester, "setPublicId"));digester.addCallMethod(prefix + "web-app/context-param","addParameter", 2);digester.addCallParam(prefix + "web-app/context-param/param-name", 0);digester.addCallParam(prefix + "web-app/context-param/param-value", 1);digester.addCallMethod(prefix + "web-app/display-name","setDisplayName", 0);digester.addRule(prefix + "web-app/distributable",new SetDistributableRule(digester));digester.addObjectCreate(prefix + "web-app/ejb-local-ref","org.apache.catalina.deploy.ContextLocalEjb");digester.addSetNext(prefix + "web-app/ejb-local-ref","addLocalEjb","org.apache.catalina.deploy.ContextLocalEjb");digester.addCallMethod(prefix + "web-app/ejb-local-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-link","setLink", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-type","setType", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/local","setLocal", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/local-home","setHome", 0);digester.addObjectCreate(prefix + "web-app/ejb-ref","org.apache.catalina.deploy.ContextEjb");digester.addSetNext(prefix + "web-app/ejb-ref","addEjb","org.apache.catalina.deploy.ContextEjb");digester.addCallMethod(prefix + "web-app/ejb-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-link","setLink", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-type","setType", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/home","setHome", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/remote","setRemote", 0);digester.addObjectCreate(prefix + "web-app/env-entry","org.apache.catalina.deploy.ContextEnvironment");digester.addSetNext(prefix + "web-app/env-entry","addEnvironment","org.apache.catalina.deploy.ContextEnvironment");digester.addCallMethod(prefix + "web-app/env-entry/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-name","setName", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-type","setType", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-value","setValue", 0);digester.addObjectCreate(prefix + "web-app/error-page","org.apache.catalina.deploy.ErrorPage");digester.addSetNext(prefix + "web-app/error-page","addErrorPage","org.apache.catalina.deploy.ErrorPage");digester.addCallMethod(prefix + "web-app/error-page/error-code","setErrorCode", 0);digester.addCallMethod(prefix + "web-app/error-page/exception-type","setExceptionType", 0);digester.addCallMethod(prefix + "web-app/error-page/location","setLocation", 0);digester.addObjectCreate(prefix + "web-app/filter","org.apache.catalina.deploy.FilterDef");digester.addSetNext(prefix + "web-app/filter","addFilterDef","org.apache.catalina.deploy.FilterDef");digester.addCallMethod(prefix + "web-app/filter/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/filter/display-name","setDisplayName", 0);digester.addCallMethod(prefix + "web-app/filter/filter-class","setFilterClass", 0);digester.addCallMethod(prefix + "web-app/filter/filter-name","setFilterName", 0);digester.addCallMethod(prefix + "web-app/filter/large-icon","setLargeIcon", 0);digester.addCallMethod(prefix + "web-app/filter/small-icon","setSmallIcon", 0);digester.addCallMethod(prefix + "web-app/filter/init-param","addInitParameter", 2);digester.addCallParam(prefix + "web-app/filter/init-param/param-name",0);digester.addCallParam(prefix + "web-app/filter/init-param/param-value",1);digester.addObjectCreate(prefix + "web-app/filter-mapping","org.apache.catalina.deploy.FilterMap");digester.addSetNext(prefix + "web-app/filter-mapping","addFilterMap","org.apache.catalina.deploy.FilterMap");digester.addCallMethod(prefix + "web-app/filter-mapping/filter-name","setFilterName", 0);digester.addCallMethod(prefix + "web-app/filter-mapping/servlet-name","setServletName", 0);digester.addCallMethod(prefix + "web-app/filter-mapping/url-pattern","setURLPattern", 0);digester.addCallMethod(prefix + "web-app/listener/listener-class","addApplicationListener", 0);digester.addObjectCreate(prefix + "web-app/login-config","org.apache.catalina.deploy.LoginConfig");digester.addSetNext(prefix + "web-app/login-config","setLoginConfig","org.apache.catalina.deploy.LoginConfig");digester.addCallMethod(prefix + "web-app/login-config/auth-method","setAuthMethod", 0);digester.addCallMethod(prefix + "web-app/login-config/realm-name","setRealmName", 0);digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-error-page","setErrorPage", 0);digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-login-page","setLoginPage", 0);digester.addCallMethod(prefix + "web-app/mime-mapping","addMimeMapping", 2);digester.addCallParam(prefix + "web-app/mime-mapping/extension", 0);digester.addCallParam(prefix + "web-app/mime-mapping/mime-type", 1);digester.addCallMethod(prefix + "web-app/resource-env-ref","addResourceEnvRef", 2);digester.addCallParam(prefix + "web-app/resource-env-ref/resource-env-ref-name", 0);digester.addCallParam(prefix + "web-app/resource-env-ref/resource-env-ref-type", 1);digester.addObjectCreate(prefix + "web-app/resource-ref","org.apache.catalina.deploy.ContextResource");digester.addSetNext(prefix + "web-app/resource-ref","addResource","org.apache.catalina.deploy.ContextResource");digester.addCallMethod(prefix + "web-app/resource-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-auth","setAuth", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-sharing-scope","setScope", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-type","setType", 0);digester.addObjectCreate(prefix + "web-app/security-constraint","org.apache.catalina.deploy.SecurityConstraint");digester.addSetNext(prefix + "web-app/security-constraint","addConstraint","org.apache.catalina.deploy.SecurityConstraint");digester.addRule(prefix + "web-app/security-constraint/auth-constraint",new SetAuthConstraintRule(digester));digester.addCallMethod(prefix + "web-app/security-constraint/auth-constraint/role-name","addAuthRole", 0);digester.addCallMethod(prefix + "web-app/security-constraint/display-name","setDisplayName", 0);digester.addCallMethod(prefix + "web-app/security-constraint/user-data-constraint/transport-guarantee","setUserConstraint", 0);digester.addObjectCreate(prefix + "web-app/security-constraint/web-resource-collection","org.apache.catalina.deploy.SecurityCollection");digester.addSetNext(prefix + "web-app/security-constraint/web-resource-collection","addCollection","org.apache.catalina.deploy.SecurityCollection");digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/http-method","addMethod", 0);digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/url-pattern","addPattern", 0);digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/web-resource-name","setName", 0);digester.addCallMethod(prefix + "web-app/security-role/role-name","addSecurityRole", 0);digester.addRule(prefix + "web-app/servlet",new WrapperCreateRule(digester));digester.addSetNext(prefix + "web-app/servlet","addChild","org.apache.catalina.Container");digester.addCallMethod(prefix + "web-app/servlet/init-param","addInitParameter", 2);digester.addCallParam(prefix + "web-app/servlet/init-param/param-name",0);digester.addCallParam(prefix + "web-app/servlet/init-param/param-value",1);digester.addCallMethod(prefix + "web-app/servlet/jsp-file","setJspFile", 0);digester.addCallMethod(prefix + "web-app/servlet/load-on-startup","setLoadOnStartupString", 0);digester.addCallMethod(prefix + "web-app/servlet/run-as/role-name","setRunAs", 0);digester.addCallMethod(prefix + "web-app/servlet/security-role-ref","addSecurityReference", 2);digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-link", 1);digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-name", 0);digester.addCallMethod(prefix + "web-app/servlet/servlet-class","setServletClass", 0);digester.addCallMethod(prefix + "web-app/servlet/servlet-name","setName", 0);digester.addCallMethod(prefix + "web-app/servlet-mapping","addServletMapping", 2);digester.addCallParam(prefix + "web-app/servlet-mapping/servlet-name", 1);digester.addCallParam(prefix + "web-app/servlet-mapping/url-pattern", 0);digester.addCallMethod(prefix + "web-app/session-config/session-timeout","setSessionTimeout", 1,new Class[] { Integer.TYPE });digester.addCallParam(prefix + "web-app/session-config/session-timeout", 0);digester.addCallMethod(prefix + "web-app/taglib","addTaglib", 2);digester.addCallParam(prefix + "web-app/taglib/taglib-location", 1);digester.addCallParam(prefix + "web-app/taglib/taglib-uri", 0);digester.addCallMethod(prefix + "web-app/welcome-file-list/welcome-file","addWelcomeFile", 0);}}// ----------------------------------------------------------- Private Classes/*** A Rule that calls the <code>setAuthConstraint(true)</code> method of* the top item on the stack, which must be of type* <code>org.apache.catalina.deploy.SecurityConstraint</code>.*/final class SetAuthConstraintRule extends Rule {public SetAuthConstraintRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {SecurityConstraint securityConstraint =(SecurityConstraint) digester.peek();securityConstraint.setAuthConstraint(true);if (digester.getDebug() > 0)digester.log("Calling SecurityConstraint.setAuthConstraint(true)");}}/*** Class that calls <code>setDistributable(true)</code> for the top object* on the stack, which must be a <code>org.apache.catalina.Context</code>.*/final class SetDistributableRule extends Rule {public SetDistributableRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {Context context = (Context) digester.peek();context.setDistributable(true);if (digester.getDebug() > 0)digester.log(context.getClass().getName() +".setDistributable( true)");}}/*** Class that calls a property setter for the top object on the stack,* passing the public ID of the entity we are currently processing.*/final class SetPublicIdRule extends Rule {public SetPublicIdRule(Digester digester, String method) {super(digester);this.method = method;}private String method = null;public void begin(Attributes attributes) throws Exception {Context context = (Context) digester.peek(digester.getCount() - 1);Object top = digester.peek();Class paramClasses[] = new Class[1];paramClasses[0] = "String".getClass();String paramValues[] = new String[1];paramValues[0] = digester.getPublicId();Method m = null;try {m = top.getClass().getMethod(method, paramClasses);} catch (NoSuchMethodException e) {digester.log("Can't find method " + method + " in " + top +" CLASS " + top.getClass());return;}m.invoke(top, paramValues);if (digester.getDebug() >= 1)digester.log("" + top.getClass().getName() + "." + method +"(" + paramValues[0] + ")");}}/*** A Rule that calls the factory method on the specified Context to* create the object that is to be added to the stack.*/final class WrapperCreateRule extends Rule {public WrapperCreateRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {Context context =(Context) digester.peek(digester.getCount() - 1);Wrapper wrapper = context.createWrapper();digester.push(wrapper);if (digester.getDebug() > 0)digester.log("new " + wrapper.getClass().getName());}public void end() throws Exception {Wrapper wrapper = (Wrapper) digester.pop();if (digester.getDebug() > 0)digester.log("pop " + wrapper.getClass().getName());} }
【1.1】Digester類 1)intro:Digester類可用于解析XML 文檔;對于XMl 文檔中的每個元素,Digester對象都會檢查它是否要做事先預(yù)定義的事件。在調(diào)用Digester.parse()方法之前,需要先定義好Digester對象執(zhí)行哪些動作; 2)如何定義在Digester對象遇到某個XMl 元素時它應(yīng)該執(zhí)行什么動作呢?—— 程序員先定義好模式,然后將每個模式與一條或多條規(guī)則相關(guān)聯(lián)。XML 文檔中根元素的模式與元素的名字相同。(干貨——引入了模式,且XML 文檔中根元素的模式與元素的名字相同) 3)看個荔枝:考慮下面的 XML文檔:(example.xml) <?xml version=1.0" encoding="ISO-8859-1"> <employee firstName="pacoson" lastName="xiao"><office><address streetName="Wellington Street" streetNumber="110" /></office> </employee> 對上述代碼的分析(Analysis): A1)該XML文檔中的根元素是 employee,employee元素有一個模式, 名為 employee; A2)office元素是 employee元素的子元素,子元素的模式是由該元素的父元素的模式再加上 “/” 符號,以及該元素名稱拼接而成的,所以office元素的模式是 ?employee/office;(干貨——我們這就了解了如何從XML 文檔中推導(dǎo)出元素的模式) 4)下面討論一下規(guī)則(rules):(干貨——規(guī)則的定義,非常重要) rule1)一條規(guī)則指明了當(dāng)Digester 對象遇到了某個特殊的模式時要執(zhí)行的一個或多個動作;規(guī)則是 org.apache.commons.digester.Rule 類;Digester類可以包含0個或多個對象; rule2)Rule類有begin()方法 和 end() 方法。在開始標簽調(diào)用start()方法,結(jié)束標簽調(diào)用 end() 方法;
5)自定義自己的規(guī)則:包括創(chuàng)建對象和設(shè)置屬性值等的規(guī)則;(干貨——自定義規(guī)則包括創(chuàng)建對象+設(shè)置屬性+調(diào)用方法+創(chuàng)建對象間的關(guān)系+驗證 XML 文檔) 5.1)創(chuàng)建對象:若想要Digester對象在遇到某個特殊字符時創(chuàng)建對象,則需要調(diào)用其 addObjectCreate()方法,該方法有4個重載版本;(干貨——引入addObjectCreate()方法) public void addObjectCreate(String pattern, String className) {addRule(pattern, new ObjectCreateRule(className)); } public void addObjectCreate(String pattern, Class clazz) {addRule(pattern, new ObjectCreateRule(clazz)); } public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName)); } public void addObjectCreate(String pattern, String attributeName, Class clazz) { addRule(pattern, new ObjectCreateRule(attributeName, clazz)); } 對上述代碼的分析(Analysis): A1)需要傳入一個模式和一個Class對象或類名來調(diào)用該方法; 看個荔枝) 如我們想讓Digester對象在遇到模式employee 時,創(chuàng)建一個 mydiy.Employee 對象,則使用下面的代碼來調(diào)用 addObjectCreate()方法:
digester.addObjectCreate("employee", "mydiy.Employee.class"); 或者 digester.addObjectCreate("employee", "mydiy.Employee"); A2)addObjecdtCreate()方法的最后兩個重載版本允許在xml 文檔中定義類的名字,而無須將其作為參數(shù)傳入,這使得類名可以在運行時決定;在上述最后兩個重載方法中,參數(shù) attributeName參數(shù)指明了 XML 元素的屬性的名字,該屬性包含了將要實例化的類的名字; 看個荔枝) step1)添加創(chuàng)建對象的一條規(guī)則:digester.addObjectCreate("employee",null,"className");(屬性名是 className); step2)傳入XML 元素中的類名:?<employee firstName="pacoson" lastName="xiao" className="mydiy.employee">;如果employee元素包含 className屬性,那么該屬性指定的值會用來作為待實例化的類的名字,如果沒有包含 className屬性,則會使用默認的類名;(干貨——顯然意思是說Emplyee類需要依賴 名為className的類對象) Attention)addObjectCreate()方法創(chuàng)建的對象會被壓入到一個內(nèi)部棧中;
5.2)設(shè)置屬性:addSetProperties()方法,該方法可以使用Digester對象為創(chuàng)建的對象設(shè)置屬性。該方法的重載版本有:(干貨——引入addSetProperties()方法) public void addSetProperties(String pattern) {addRule(pattern, new SetPropertiesRule()); }public void addSetProperties( String pattern, String attributeName, String propertyName) { addRule(pattern, new SetPropertiesRule(attributeName, propertyName)); }public void addSetProperties(String pattern, String [] attributeNames, String [] propertyNames) { addRule(pattern, new SetPropertiesRule(attributeNames, propertyNames)); } 看個荔枝)考慮下面的代碼: digester.addObjectCreate("employee", "mydiy.Employee"); digester.addSetProperties("employee"); 對以上代碼的分析(Analysis): A1)上面的Digester有兩個Rule 對象,分別用來創(chuàng)建對象和設(shè)置屬性,他們都是通過employee模式觸發(fā)的。而Rule對象按照其添加到Digester實例中的順序逐個執(zhí)行。 A2)對于下面XMl 文檔中的employee 元素(該元素匹配 employee模式):?<employee firstName="pacoson", lastName="Xiao">;依據(jù)Digester的第一條rule,會創(chuàng)建 diy.Employee類的一個實例,依據(jù)第二條Rule,調(diào)用已經(jīng)實例化的Employee.setFirstName() and Employee.setLastName(),分別傳入pacoson 和 Xiao 來設(shè)置屬性; 5.3)調(diào)用方法:Digetser類允許通過添加一條Rule,使Digester 在遇到與該規(guī)則相關(guān)聯(lián)的模式時調(diào)用內(nèi)部棧最頂端對象的 某個方法。這需要用到 addCallMethod()方法,重載版本如下:(干貨——引入addCallMethod()方法) public void addCallMethod(String pattern, String methodName) {addRule( pattern, new CallMethodRule(methodName)); }public void addCallMethod(String pattern, String methodName, int paramCount) { addRule(pattern, new CallMethodRule(methodName, paramCount)); }public void addCallMethod(String pattern, String methodName, int paramCount, String paramTypes[]) { addRule(pattern, new CallMethodRule( methodName,paramCount,paramTypes));}public void addCallMethod(String pattern, String methodName,int paramCount, Class paramTypes[]) {addRule(pattern,new CallMethodRule(methodName,paramCount,paramTypes));} 5.4)創(chuàng)建對象之間的關(guān)系(干貨——Digester實例有一個內(nèi)部棧,用于臨時存儲創(chuàng)建的對象)(干貨——引入addSetNext()方法) 5.4.1)addSetNext()方法:若棧中有兩個對象,那么該方法會調(diào)用第1個對象的指定方法并將第2個對象作為參數(shù)傳入該方法來創(chuàng)建第1個對象和第2個對象的關(guān)系; public void addSetNext(String pattern, String methodName) {addRule(pattern,new SetNextRule(methodName));}public void addSetNext(String pattern, String methodName,String paramType) {addRule(pattern,new SetNextRule(methodName, paramType));} 對以上代碼的分析(Analysis):參數(shù)pattern 指明了觸發(fā)該規(guī)則的具體模式,參數(shù)methodName 是將要調(diào)用的第1個對象的方法的名稱。模式是如下格式:firstObject/secondObject; 看個荔枝)如何創(chuàng)建對象間的關(guān)系: step1)創(chuàng)建兩個對象; digester.addObjectCreate("employee", "mydiy.Employee"); digester.addObjectCreate("employee/office", "mydiy.Office"); step2)創(chuàng)建對象間的關(guān)系:需要另外定義一條規(guī)則,使用 addSetNext()方法來建立關(guān)系(調(diào)用addOffice()方法建立關(guān)系): digetster.addSetNext("employee/office", 'addOffice'); 5.5)驗證XML文檔:Digester.validating屬性指明了是否要對 XML 文檔進行有效性驗證。默認case下,其為false;setValidating()方法可以設(shè)置其值;(干貨——引入setValidating()方法)
【1.2】Digester庫荔枝1(如何使用 Digester庫動態(tài)地創(chuàng)建對象,并設(shè)置相應(yīng)的屬性) 1)源代碼 public class Test01 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee1.xml");Digester digester = new Digester();// add rules (為模式 employee 添加3條規(guī)則)digester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addCallMethod("employee", "printName");try {Employee employee = (Employee) digester.parse(file);System.out.println("First name : " + employee.getFirstName());System.out.println("Last name : " + employee.getLastName());} catch (Exception e) {e.printStackTrace();}} } <?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Brian" lastName="May"> </employee> 2)console info Creating Employee Setting firstName : Brian Setting lastName : May My name is Brian May First name : Brian Last name : May 3)info analysis:當(dāng)調(diào)用parse()方法時,它會打開指定的xml 文檔,開始解析它;(干貨——當(dāng)調(diào)用parse()方法時,它會打開指定的xml 文檔,開始解析它,只需要parse方法就可以創(chuàng)建根元素(模式)的相應(yīng)對象和其關(guān)聯(lián)對象) step1)Digester類查看 employee 元素的開始標簽,這會觸發(fā)與 employee模式關(guān)聯(lián)的3條規(guī)則,按照其被添加到 Digester 對象中的順序逐個執(zhí)行; step2)第一條規(guī)則用于創(chuàng)建Employee對象,調(diào)用構(gòu)造函數(shù),打印Creating Employee; step3)第二條規(guī)則設(shè)置 Employee對象的屬性,在employee 元素中包含兩個屬性:分別是 firstName 和 lastName, 這會調(diào)用調(diào)用的set方法,打印 Setting firstName : Brian ?Setting lastName : May; step4)第三條規(guī)則調(diào)用 Employee.printName()方法,打印M y name is Brian May; 【1.3】Digester庫荔枝2(如何創(chuàng)建兩個對象,并建立他們的關(guān)系) 1)源代碼 public class Test02 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee2.xml");Digester digester = new Digester();// add rules,添加規(guī)則(key)digester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addObjectCreate("employee/office","com.tomcat.chapter15.digestertest.Office");digester.addSetProperties("employee/office");digester.addSetNext("employee/office", "addOffice");digester.addObjectCreate("employee/office/address","com.tomcat.chapter15.digestertest.Address");digester.addSetProperties("employee/office/address");digester.addSetNext("employee/office/address", "setAddress");try {Employee employee = (Employee) digester.parse(file);ArrayList offices = employee.getOffices();Iterator iterator = offices.iterator();System.out.println("-------------------------------------------------");while (iterator.hasNext()) {Office office = (Office) iterator.next();Address address = office.getAddress();System.out.println(office.getDescription());System.out.println("Address : " + address.getStreetNumber()+ " " + address.getStreetName());System.out.println("--------------------------------");}} catch (Exception e) {e.printStackTrace();}} } <?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Freddie" lastName="Mercury"><office description="Headquarters"><address streetName="Wellington Avenue" streetNumber="223"/></office><office description="Client site"><address streetName="Downing Street" streetNumber="10"/></office> </employee> 2)console info Creating Employee Setting firstName : Freddie Setting lastName : Mercury ..Creating Office ..Setting office description : Headquarters ....Creating Address ....Setting streetName : Wellington Avenue ....Setting streetNumber : 223 ..Setting office address : ....223 Wellington Avenue Adding Office to this employee ..Creating Office ..Setting office description : Client site ....Creating Address ....Setting streetName : Downing Street ....Setting streetNumber : 10 ..Setting office address : ....10 Downing Street Adding Office to this employee ------------------------------------------------- Headquarters Address : 223 Wellington Avenue -------------------------------- Client site Address : 10 Downing Street -------------------------------- Attention)本文不對 荔枝2的實例程序進行分析了,結(jié)合荔枝1的分析,理解這個不難;
【1.4】org.apache.commons.digester.Rule類(最重要的方法start() + end())
1)intro to begin() :當(dāng)Digester實例 遇到某個XML 元素的開始標簽時,會調(diào)用它所包含的匹配Rule 對象的begin()方法:? public void begin(Attributes attributes) throws Exception {; // The default implementation does nothing} public void begin(String namespace, String name, Attributes attributes)throws Exception {begin(attributes);} 2)intro to end():當(dāng)Digester實例 遇到某個XML 元素的結(jié)束標簽時,會調(diào)用它所包含的匹配Rule 對象的end()方法: public void end() throws Exception {; // The default implementation does nothing} public void end(String namespace, String name)throws Exception {end();} 3)Digester對象是如何完成這些工作的? 當(dāng)調(diào)用Digester.addObjectCreate()方法,addCallMethod()方法,addSetNext()方法或其他方法時,都會間接地調(diào)用 Digester.addRule()方法; public void addRule(String pattern, Rule rule) {rule.setDigester(this);getRules().add(pattern, rule);}4)再次review Digester.addObjectCreate()方法的重載version: public void addObjectCreate(String pattern, String className) {addRule(pattern, new ObjectCreateRule(className)); } public void addObjectCreate(String pattern, Class clazz) {addRule(pattern, new ObjectCreateRule(clazz)); } public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName)); } public void addObjectCreate(String pattern, String attributeName, Class clazz) { addRule(pattern, new ObjectCreateRule(attributeName, clazz)); } 對以上代碼的分析(Analysis): A1)這4個重載方法都調(diào)用了addRule()方法,ObjectCreateRule類 是 Rule 類的子類,該類的實例都作為 addRule()方法的參數(shù); A2)ObjectCreateRule.start()方法 和?ObjectCreateRule.end()方法的實現(xiàn)如下: public void begin(Attributes attributes) throws Exception { //org.apache.commons.digester.ObjectCreateRule.begin().// Identify the name of the class to instantiateString realClassName = className;if (attributeName != null) {String value = attributes.getValue(attributeName);if (value != null) {realClassName = value;}}if (digester.log.isDebugEnabled()) {digester.log.debug("[ObjectCreateRule]{" + digester.match +"}New " + realClassName);}// Instantiate the new object and push it on the context stackClass clazz = digester.getClassLoader().loadClass(realClassName);Object instance = clazz.newInstance();digester.push(instance); //highlight line.}public void end() throws Exception {Object top = digester.pop(); //highlight line.if (digester.log.isDebugEnabled()) {digester.log.debug("[ObjectCreateRule]{" + digester.match +"} Pop " + top.getClass().getName());}} 對以上代碼的分析(Analysis):begin()方法的最后三行會創(chuàng)建Digester對象的一個實例,并將其壓入到 Digester對象的內(nèi)部棧中。end()方法 會將內(nèi)部站的棧頂元素彈出; 【1.5】Digester庫荔枝3:使用RuleSet(org.apache.commons.digester.RuleSet) 1)要向Digester實例添加 Rule對象,還可以調(diào)用其 addRuleSet()方法; 2)Rule對象集合是 org.apache.commons.digester.RuleSet接口的實例,該接口定義了兩個方法,分別是 addRuleInstance()方法 和 getNamespaceURI()方法; 2.1)addRuleInstance方法:將在當(dāng)前RuleSet 中的Rule對象的集合作為該方法的參數(shù)添加到 Digester實例中; 2.2)getNamespaceURI方法:返回將要應(yīng)用在 RuleSet中所有Rule 對象的命名空間的URI; 3)RuleSetBase implements RuleSet。RuleSetBase 是一個抽象類,提供了getNamespaceURI的實現(xiàn),你只需要提供addRuleInstances()方法的實現(xiàn)就可以了; public abstract class RuleSetBase implements RuleSet { protected String namespaceURI = null; public String getNamespaceURI() {return (this.namespaceURI);} public abstract void addRuleInstances(Digester digester); }4)測試用例 public class Test03 {public static void main(String[] args) {String path = System.getProperty("user.dir") + File.separator + "src";File file = new File(path, "employee2.xml");Digester digester = new Digester();digester.addRuleSet(new EmployeeRuleSet());try {Employee employee = (Employee) digester.parse(file);ArrayList offices = employee.getOffices();Iterator iterator = offices.iterator();System.out.println("-------------------------------------------------");while (iterator.hasNext()) {Office office = (Office) iterator.next();Address address = office.getAddress();System.out.println(office.getDescription());System.out.println("Address : " + address.getStreetNumber()+ " " + address.getStreetName());System.out.println("--------------------------------");}} catch (Exception e) {e.printStackTrace();}} } public class EmployeeRuleSet extends RuleSetBase {public void addRuleInstances(Digester digester) {// add rulesdigester.addObjectCreate("employee","com.tomcat.chapter15.digestertest.Employee");digester.addSetProperties("employee");digester.addObjectCreate("employee/office","com.tomcat.chapter15.digestertest.Office");digester.addSetProperties("employee/office");digester.addSetNext("employee/office", "addOffice");digester.addObjectCreate("employee/office/address","com.tomcat.chapter15.digestertest.Address");digester.addSetProperties("employee/office/address");digester.addSetNext("employee/office/address", "setAddress");} } Attention)參考前面做的分析,應(yīng)該不難;
【2】ContextConfig類 1)在前面章節(jié)中,我們使用了 SimpleContextConfig 作為 StandardContext的監(jiān)聽器:其唯一用途是設(shè)置configure變量,這樣StandardContext.start()方法才能繼續(xù)執(zhí)行; public class SimpleContextConfig implements LifecycleListener {public void lifecycleEvent(LifecycleEvent event) {if (Lifecycle.START_EVENT.equals(event.getType())) {Context context = (Context) event.getLifecycle();context.setConfigured(true);}} } 2)而tomcat 的標準監(jiān)聽器:是 org.apache.catalina.startup.ContextConfig類的實例;(干貨——Tomcat標準監(jiān)聽器) 3)ContextConfig會執(zhí)行很多對 StandardContext實例來說必不可少的任務(wù)。如,與某個 StandardContext實例關(guān)聯(lián)的 ContextConfig 實例會安裝一個驗證器閥到 StandardContext的管道中。它還會添加一個許可閥(org.apache.catalina.valves.CertificateValve)到管道對象中; 4)更重要的是:ContextConfig類的實例還要讀取和解析默認的 web.xml 文件和應(yīng)用程序自定義的web.xml文件,并將xml 元素轉(zhuǎn)換為 java 對象;(干貨——引入了默認的和自定義的web.xml文件) 4.1)默認的web.xml:位于 CATALINA_HOME/conf 目錄中,其中定義并映射了很多默認的 servlet,配置了很多 MIME類型文件的映射,定義了默認的session超時時間,以及定義了歡迎文件的列表; 4.2)應(yīng)用程序的web.xml文件:位于 WEB-INF 目錄中; 5)以上兩個文件都不是必須的,即使這兩個文件沒有找到, ContextConfig 實例仍然會繼續(xù)執(zhí)行; 6)ContextConfig實例:會為每個 servlet元素創(chuàng)建一個 StandardWrapper類;(干貨——ContextConfig的作用) 7)在BootStrap程序中,需要實例化一個 ContextConfig類,并調(diào)用?addLifecycleListener方法; LifecycleListener listener = new ContextConfig();((Lifecycle) context).addLifecycleListener(listener); 7.1)在啟動 StandardContext實例時,會觸發(fā)以下事件(events): event1)BEFORE_START_EVENT; event2)START_EVENT; event3)AFTER_START_EVENT; 7.2)當(dāng)程序停止時,會觸發(fā)以下事件: event1)BEFORE_STOP_EVENT; event2)STOP_EVENT; event3)AFTER_STOP_EVENT; 8)ContextConfig 實例會對兩種事情做出相應(yīng):分別是START_EVENT and?STOP_EVENT。每次 StandardContext實例觸發(fā)事件時,會調(diào)用 ContextConfig.lifecycleEvent()方法; public void lifecycleEvent(LifecycleEvent event) { //org.apache.catalina.startup.ContextConfig.lifecycleEvent().// Identify the context we are associated withtry {context = (Context) event.getLifecycle();if (context instanceof StandardContext) {int contextDebug = ((StandardContext) context).getDebug();if (contextDebug > this.debug)this.debug = contextDebug;}} catch (ClassCastException e) {log(sm.getString("contextConfig.cce", event.getLifecycle()), e);return;}// Process the event that has occurredif (event.getType().equals(Lifecycle.START_EVENT))start(); //highlight line.else if (event.getType().equals(Lifecycle.STOP_EVENT))stop(); //highlight line.} 8.1)start()方法 (干貨——start方法會調(diào)用?defaultConfig() and?applicationConfig()方法,兩者分別用于讀取和解析默認的web.xml 和 應(yīng)用程序自定義的 web.xml) private synchronized void start() { //org.apache.catalina.startup.ContextConfig.start().if (debug > 0)log(sm.getString("contextConfig.start"));context.setConfigured(false);ok = true;// Set properties based on DefaultContextContainer container = context.getParent();if( !context.getOverride() ) {if( container instanceof Host ) {((Host)container).importDefaultContext(context);container = container.getParent();}if( container instanceof Engine ) {((Engine)container).importDefaultContext(context);}}// Process the default and application web.xml filesdefaultConfig(); // highlight line.applicationConfig(); // highlight line.if (ok) {validateSecurityRoles();}// Scan tag library descriptor files for additional listener classesif (ok) {try {tldScan();} catch (Exception e) {log(e.getMessage(), e);ok = false;}}// Configure a certificates exposer valve, if requiredif (ok)certificatesConfig();// Configure an authenticator if we need oneif (ok)authenticatorConfig();// Dump the contents of this pipeline if requestedif ((debug >= 1) && (context instanceof ContainerBase)) {log("Pipline Configuration:");Pipeline pipeline = ((ContainerBase) context).getPipeline();Valve valves[] = null;if (pipeline != null)valves = pipeline.getValves();if (valves != null) {for (int i = 0; i < valves.length; i++) {log(" " + valves[i].getInfo());}}log("======================");}// Make our application available if no problems were encounteredif (ok)context.setConfigured(true);else {log(sm.getString("contextConfig.unavailable"));context.setConfigured(false);}} 【2.1】 defaultConfig()方法 1)intro: 該方法負責(zé)讀取并解析位于 CATALINA_HOME/conf目錄下的默認web.xml 文件; private void defaultConfig() { //org.apache.catalina.startup.ContextConfig.defaultConfig().// Open the default web.xml file, if it existsFile file = new File(Constants.DefaultWebXml);// public static final String DefaultWebXml = "conf/web.xml";if (!file.isAbsolute())file = new File(System.getProperty("catalina.base"),Constants.DefaultWebXml);FileInputStream stream = null;try {stream = new FileInputStream(file.getCanonicalPath());stream.close();stream = null;} catch (FileNotFoundException e) {log(sm.getString("contextConfig.defaultMissing"));return;} catch (IOException e) {log(sm.getString("contextConfig.defaultMissing"), e);return;}// Process the default web.xml file (鎖定webDigester變量,并解析默認的 web.xml 文件)synchronized (webDigester) {try {InputSource is =new InputSource("file://" + file.getAbsolutePath());stream = new FileInputStream(file);is.setByteStream(stream);webDigester.setDebug(getDebug());if (context instanceof StandardContext)((StandardContext) context).setReplaceWelcomeFiles(true);webDigester.clear();webDigester.push(context);webDigester.parse(is);webDigester.push(null); // 解析結(jié)束.} catch (SAXParseException e) {log(sm.getString("contextConfig.defaultParse"), e);log(sm.getString("contextConfig.defaultPosition","" + e.getLineNumber(),"" + e.getColumnNumber()));ok = false;} catch (Exception e) {log(sm.getString("contextConfig.defaultParse"), e);ok = false;} finally {try {if (stream != null) {stream.close();}} catch (IOException e) {log(sm.getString("contextConfig.defaultClose"), e);}}}} 【2.2】applicationConfig()方法 1)intro:該方法處理的是 應(yīng)用程序自定義的web.xml,位于 WEB-INF 目錄中; private void applicationConfig() { //org.apache.catalina.startup.ContextConfig.applicationConfig().// Open the application web.xml file, if it existsInputStream stream = null;ServletContext servletContext = context.getServletContext();if (servletContext != null)stream = servletContext.getResourceAsStream(Constants.ApplicationWebXml); // ?public static final String ApplicationWebXml = "/WEB-INF/web.xml";if (stream == null) {log(sm.getString("contextConfig.applicationMissing"));return;}// Process the application web.xml filesynchronized (webDigester) {try {URL url =servletContext.getResource(Constants.ApplicationWebXml);InputSource is = new InputSource(url.toExternalForm());is.setByteStream(stream);webDigester.setDebug(getDebug());if (context instanceof StandardContext) {((StandardContext) context).setReplaceWelcomeFiles(true);}webDigester.clear();webDigester.push(context);webDigester.parse(is);webDigester.push(null);} catch (SAXParseException e) {log(sm.getString("contextConfig.applicationParse"), e);log(sm.getString("contextConfig.applicationPosition","" + e.getLineNumber(),"" + e.getColumnNumber()));ok = false;} catch (Exception e) {log(sm.getString("contextConfig.applicationParse"), e);ok = false;} finally {try {if (stream != null) {stream.close();}} catch (IOException e) {log(sm.getString("contextConfig.applicationClose"), e);}}}}
【2.3】創(chuàng)建 Web Digester 1)在ContextConfig 類中,使用變量 webDigester來引用一個 Digester類型的對象; private static Digester webDigester = createWebDigester();private static Digester createWebDigester() { //org.apache.catalina.startup.ContextConfig.createWebDigester().URL url = null;Digester webDigester = new Digester();webDigester.setValidating(true);url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_22);webDigester.register(Constants.WebDtdPublicId_22,url.toString());url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_23);webDigester.register(Constants.WebDtdPublicId_23,url.toString());webDigester.addRuleSet(new WebRuleSet()); // highlight line.return (webDigester);} 2)這個Digester對象用來解析默認的 web.xml 文件和應(yīng)用程序自定義的 web.xml 文件。在調(diào)用了?createWebDigester() 方法時會添加用來處理 web.xml 文件的規(guī)則;(干貨——在調(diào)用了?createWebDigester() 方法時會添加用來處理 web.xml 文件的規(guī)則,什么是規(guī)則,你懂的,前面已經(jīng)詳細介紹了Rule) Attention) A1)createWebDigester()方法調(diào)用了變量webDigester的 addRuleSet()方法,傳入了一個 org.apache.catalina.startup.WebRuleSet 類型的對象作為參數(shù); A2)WebRuleSet?類是 org.apache.commons.digester.RuleSetBase的子類; public class WebRuleSet extends RuleSetBase { //org.apache.catalina.startup.WebRuleSet A3)org.apache.catalina.startup.WebRuleSet的定義代碼見 文末,特別要注意其addRuleInstances()方法,其添加了很多規(guī)則集合;(干貨——addRuleInstances()方法添加了很多規(guī)則集合)
【3】應(yīng)用程序(本測試用例重在說明如何使用ContextConfig實例作為一個監(jiān)聽器來配置StandardContext對象)? Attention)通過以下實例,你會發(fā)現(xiàn),這與之前的Bootstrap 測試用例大有不同,以前是顯式地創(chuàng)建StandardWrapper(創(chuàng)建具體的servlet實例),而下面的測試用例采用Digester從 xml 中讀取 servlet的配置信息創(chuàng)建servlet實例,這就是為什么本文之前講那么多 Digester庫的原因; 1)測試用例 public final class Bootstrap {// invoke: http://localhost:8080/app1/Modern or // http://localhost:8080/app2/Primitive// note that we don't instantiate a Wrapper here,// ContextConfig reads the WEB-INF/classes dir and loads all servlets.public static void main(String[] args) {System.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/app1");context.setDocBase("app1");LifecycleListener listener = new ContextConfig();((Lifecycle) context).addLifecycleListener(listener);Host host = new StandardHost();host.addChild(context);host.setName("localhost");host.setAppBase("webapps");Loader loader = new WebappLoader();context.setLoader(loader);connector.setContainer(host);try {connector.initialize();((Lifecycle) connector).start();((Lifecycle) host).start();Container[] c = context.findChildren();int length = c.length;for (int i=0; i<length; i++) {Container child = c[i];System.out.println(child.getName());}// make the application wait until we press a key.System.in.read();((Lifecycle) host).stop();}catch (Exception e) {e.printStackTrace();}} } Supplement)本文習(xí)慣性地總結(jié)了上述測試用例的調(diào)用過程,如下:
S0)上述調(diào)用過程涉及到的變量ApplicationWebXml 和?DefaultWebXml,其值為: public static final String ApplicationWebXml = "/WEB-INF/web.xml";public static final String DefaultWebXml = "conf/web.xml"; // both of them are defined in org.apache.catalina.startup.Constant; S1)自定義的web.xml如下所示:該文件的文件路徑為 ?System.getProperty("user.dir")\webapps\app1\WEB-INF; S2)(tomcat使用了開源庫Digester來將XML 文檔中的元素轉(zhuǎn)換成 java 對象,觸發(fā)相應(yīng)規(guī)則如調(diào)用設(shè)置器來配置StandardContext的子容器(StandardWrapper包裝了servlet))(干貨——具體的規(guī)則包含在 WebRuleSet類中,見文末) <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-appPUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><servlet><servlet-name>Modern</servlet-name><servlet-class>ModernServlet</servlet-class></servlet><servlet><servlet-name>Primitive</servlet-name><servlet-class>PrimitiveServlet</servlet-class></servlet><servlet-mapping><servlet-name>Modern</servlet-name><url-pattern>/Modern</url-pattern></servlet-mapping><servlet-mapping><servlet-name>Primitive</servlet-name><url-pattern>/Primitive</url-pattern></servlet-mapping> </web-app> 2)console info E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src>java -cp .;lib/servlet.jar;lib/catalina_4_1_24.jar;lib/catalina-5.5.4.jar;lib/naming-common. jar;lib/commons-collections.jar;lib/naming-resources.jar;lib/commons-digester.jar;lib/catalina.jar;lib/commons-logging.jar;lib/commons-beanutils.jar;E :\bench-cluster\cloud-data-preprocess\HowTomcatWoks\webroot com.tomcat.chapter15.startup.Bootstrap HttpConnector Opening server socket on all host IP addresses HttpConnector[8080] Starting background thread WebappLoader[/app1]: Deploying class repositories to work directory E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\work\_\localhost\app1 WebappLoader[/app1]: Deploy class files /WEB-INF/classes to E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\webapps\app1\WEB-INF\classes ContextConfig[/app1]: Missing default web.xml, using application web.xml only StandardManager[/app1]: Seeding random number generator class java.security.SecureRandom StandardManager[/app1]: Seeding of random number generator has been completed Primitive Modern StandardHost[localhost]: MAPPING configuration error for request URI /Modern/app1/Modern StandardHost[localhost]: MAPPING configuration error for request URI /favicon.ico ModernServlet -- init StandardHost[localhost]: MAPPING configuration error for request URI /favicon.ico 3)訪問結(jié)果
public class WebRuleSet extends RuleSetBase { //org.apache.catalina.startup.WebRuleSetprotected String prefix = null; public WebRuleSet() { this("");} public WebRuleSet(String prefix) {super();this.namespaceURI = null;this.prefix = prefix;} public void addRuleInstances(Digester digester) { // highlight.digester.addRule(prefix + "web-app",new SetPublicIdRule(digester, "setPublicId"));digester.addCallMethod(prefix + "web-app/context-param","addParameter", 2);digester.addCallParam(prefix + "web-app/context-param/param-name", 0);digester.addCallParam(prefix + "web-app/context-param/param-value", 1);digester.addCallMethod(prefix + "web-app/display-name","setDisplayName", 0);digester.addRule(prefix + "web-app/distributable",new SetDistributableRule(digester));digester.addObjectCreate(prefix + "web-app/ejb-local-ref","org.apache.catalina.deploy.ContextLocalEjb");digester.addSetNext(prefix + "web-app/ejb-local-ref","addLocalEjb","org.apache.catalina.deploy.ContextLocalEjb");digester.addCallMethod(prefix + "web-app/ejb-local-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-link","setLink", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-type","setType", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/local","setLocal", 0);digester.addCallMethod(prefix + "web-app/ejb-local-ref/local-home","setHome", 0);digester.addObjectCreate(prefix + "web-app/ejb-ref","org.apache.catalina.deploy.ContextEjb");digester.addSetNext(prefix + "web-app/ejb-ref","addEjb","org.apache.catalina.deploy.ContextEjb");digester.addCallMethod(prefix + "web-app/ejb-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-link","setLink", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-type","setType", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/home","setHome", 0);digester.addCallMethod(prefix + "web-app/ejb-ref/remote","setRemote", 0);digester.addObjectCreate(prefix + "web-app/env-entry","org.apache.catalina.deploy.ContextEnvironment");digester.addSetNext(prefix + "web-app/env-entry","addEnvironment","org.apache.catalina.deploy.ContextEnvironment");digester.addCallMethod(prefix + "web-app/env-entry/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-name","setName", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-type","setType", 0);digester.addCallMethod(prefix + "web-app/env-entry/env-entry-value","setValue", 0);digester.addObjectCreate(prefix + "web-app/error-page","org.apache.catalina.deploy.ErrorPage");digester.addSetNext(prefix + "web-app/error-page","addErrorPage","org.apache.catalina.deploy.ErrorPage");digester.addCallMethod(prefix + "web-app/error-page/error-code","setErrorCode", 0);digester.addCallMethod(prefix + "web-app/error-page/exception-type","setExceptionType", 0);digester.addCallMethod(prefix + "web-app/error-page/location","setLocation", 0);digester.addObjectCreate(prefix + "web-app/filter","org.apache.catalina.deploy.FilterDef");digester.addSetNext(prefix + "web-app/filter","addFilterDef","org.apache.catalina.deploy.FilterDef");digester.addCallMethod(prefix + "web-app/filter/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/filter/display-name","setDisplayName", 0);digester.addCallMethod(prefix + "web-app/filter/filter-class","setFilterClass", 0);digester.addCallMethod(prefix + "web-app/filter/filter-name","setFilterName", 0);digester.addCallMethod(prefix + "web-app/filter/large-icon","setLargeIcon", 0);digester.addCallMethod(prefix + "web-app/filter/small-icon","setSmallIcon", 0);digester.addCallMethod(prefix + "web-app/filter/init-param","addInitParameter", 2);digester.addCallParam(prefix + "web-app/filter/init-param/param-name",0);digester.addCallParam(prefix + "web-app/filter/init-param/param-value",1);digester.addObjectCreate(prefix + "web-app/filter-mapping","org.apache.catalina.deploy.FilterMap");digester.addSetNext(prefix + "web-app/filter-mapping","addFilterMap","org.apache.catalina.deploy.FilterMap");digester.addCallMethod(prefix + "web-app/filter-mapping/filter-name","setFilterName", 0);digester.addCallMethod(prefix + "web-app/filter-mapping/servlet-name","setServletName", 0);digester.addCallMethod(prefix + "web-app/filter-mapping/url-pattern","setURLPattern", 0);digester.addCallMethod(prefix + "web-app/listener/listener-class","addApplicationListener", 0);digester.addObjectCreate(prefix + "web-app/login-config","org.apache.catalina.deploy.LoginConfig");digester.addSetNext(prefix + "web-app/login-config","setLoginConfig","org.apache.catalina.deploy.LoginConfig");digester.addCallMethod(prefix + "web-app/login-config/auth-method","setAuthMethod", 0);digester.addCallMethod(prefix + "web-app/login-config/realm-name","setRealmName", 0);digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-error-page","setErrorPage", 0);digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-login-page","setLoginPage", 0);digester.addCallMethod(prefix + "web-app/mime-mapping","addMimeMapping", 2);digester.addCallParam(prefix + "web-app/mime-mapping/extension", 0);digester.addCallParam(prefix + "web-app/mime-mapping/mime-type", 1);digester.addCallMethod(prefix + "web-app/resource-env-ref","addResourceEnvRef", 2);digester.addCallParam(prefix + "web-app/resource-env-ref/resource-env-ref-name", 0);digester.addCallParam(prefix + "web-app/resource-env-ref/resource-env-ref-type", 1);digester.addObjectCreate(prefix + "web-app/resource-ref","org.apache.catalina.deploy.ContextResource");digester.addSetNext(prefix + "web-app/resource-ref","addResource","org.apache.catalina.deploy.ContextResource");digester.addCallMethod(prefix + "web-app/resource-ref/description","setDescription", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-auth","setAuth", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-ref-name","setName", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-sharing-scope","setScope", 0);digester.addCallMethod(prefix + "web-app/resource-ref/res-type","setType", 0);digester.addObjectCreate(prefix + "web-app/security-constraint","org.apache.catalina.deploy.SecurityConstraint");digester.addSetNext(prefix + "web-app/security-constraint","addConstraint","org.apache.catalina.deploy.SecurityConstraint");digester.addRule(prefix + "web-app/security-constraint/auth-constraint",new SetAuthConstraintRule(digester));digester.addCallMethod(prefix + "web-app/security-constraint/auth-constraint/role-name","addAuthRole", 0);digester.addCallMethod(prefix + "web-app/security-constraint/display-name","setDisplayName", 0);digester.addCallMethod(prefix + "web-app/security-constraint/user-data-constraint/transport-guarantee","setUserConstraint", 0);digester.addObjectCreate(prefix + "web-app/security-constraint/web-resource-collection","org.apache.catalina.deploy.SecurityCollection");digester.addSetNext(prefix + "web-app/security-constraint/web-resource-collection","addCollection","org.apache.catalina.deploy.SecurityCollection");digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/http-method","addMethod", 0);digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/url-pattern","addPattern", 0);digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/web-resource-name","setName", 0);digester.addCallMethod(prefix + "web-app/security-role/role-name","addSecurityRole", 0);digester.addRule(prefix + "web-app/servlet",new WrapperCreateRule(digester));digester.addSetNext(prefix + "web-app/servlet","addChild","org.apache.catalina.Container");digester.addCallMethod(prefix + "web-app/servlet/init-param","addInitParameter", 2);digester.addCallParam(prefix + "web-app/servlet/init-param/param-name",0);digester.addCallParam(prefix + "web-app/servlet/init-param/param-value",1);digester.addCallMethod(prefix + "web-app/servlet/jsp-file","setJspFile", 0);digester.addCallMethod(prefix + "web-app/servlet/load-on-startup","setLoadOnStartupString", 0);digester.addCallMethod(prefix + "web-app/servlet/run-as/role-name","setRunAs", 0);digester.addCallMethod(prefix + "web-app/servlet/security-role-ref","addSecurityReference", 2);digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-link", 1);digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-name", 0);digester.addCallMethod(prefix + "web-app/servlet/servlet-class","setServletClass", 0);digester.addCallMethod(prefix + "web-app/servlet/servlet-name","setName", 0);digester.addCallMethod(prefix + "web-app/servlet-mapping","addServletMapping", 2);digester.addCallParam(prefix + "web-app/servlet-mapping/servlet-name", 1);digester.addCallParam(prefix + "web-app/servlet-mapping/url-pattern", 0);digester.addCallMethod(prefix + "web-app/session-config/session-timeout","setSessionTimeout", 1,new Class[] { Integer.TYPE });digester.addCallParam(prefix + "web-app/session-config/session-timeout", 0);digester.addCallMethod(prefix + "web-app/taglib","addTaglib", 2);digester.addCallParam(prefix + "web-app/taglib/taglib-location", 1);digester.addCallParam(prefix + "web-app/taglib/taglib-uri", 0);digester.addCallMethod(prefix + "web-app/welcome-file-list/welcome-file","addWelcomeFile", 0);}}// ----------------------------------------------------------- Private Classes/*** A Rule that calls the <code>setAuthConstraint(true)</code> method of* the top item on the stack, which must be of type* <code>org.apache.catalina.deploy.SecurityConstraint</code>.*/final class SetAuthConstraintRule extends Rule {public SetAuthConstraintRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {SecurityConstraint securityConstraint =(SecurityConstraint) digester.peek();securityConstraint.setAuthConstraint(true);if (digester.getDebug() > 0)digester.log("Calling SecurityConstraint.setAuthConstraint(true)");}}/*** Class that calls <code>setDistributable(true)</code> for the top object* on the stack, which must be a <code>org.apache.catalina.Context</code>.*/final class SetDistributableRule extends Rule {public SetDistributableRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {Context context = (Context) digester.peek();context.setDistributable(true);if (digester.getDebug() > 0)digester.log(context.getClass().getName() +".setDistributable( true)");}}/*** Class that calls a property setter for the top object on the stack,* passing the public ID of the entity we are currently processing.*/final class SetPublicIdRule extends Rule {public SetPublicIdRule(Digester digester, String method) {super(digester);this.method = method;}private String method = null;public void begin(Attributes attributes) throws Exception {Context context = (Context) digester.peek(digester.getCount() - 1);Object top = digester.peek();Class paramClasses[] = new Class[1];paramClasses[0] = "String".getClass();String paramValues[] = new String[1];paramValues[0] = digester.getPublicId();Method m = null;try {m = top.getClass().getMethod(method, paramClasses);} catch (NoSuchMethodException e) {digester.log("Can't find method " + method + " in " + top +" CLASS " + top.getClass());return;}m.invoke(top, paramValues);if (digester.getDebug() >= 1)digester.log("" + top.getClass().getName() + "." + method +"(" + paramValues[0] + ")");}}/*** A Rule that calls the factory method on the specified Context to* create the object that is to be added to the stack.*/final class WrapperCreateRule extends Rule {public WrapperCreateRule(Digester digester) {super(digester);}public void begin(Attributes attributes) throws Exception {Context context =(Context) digester.peek(digester.getCount() - 1);Wrapper wrapper = context.createWrapper();digester.push(wrapper);if (digester.getDebug() > 0)digester.log("new " + wrapper.getClass().getName());}public void end() throws Exception {Wrapper wrapper = (Wrapper) digester.pop();if (digester.getDebug() > 0)digester.log("pop " + wrapper.getClass().getName());} }
總結(jié)
以上是生活随笔為你收集整理的tomcat(15)Digester库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工业平板电脑哪些场景要带电池?那些场景不
- 下一篇: 戴尔上架新款游匣 G15 游戏本:i5-