Struts學習(第三篇)——StrutsPrepareAndExecuteFilter攔截器源碼

本文來自:曹勝歡博客專欄。轉載請註明出處:http://blog.csdn.net/csh624366188

       

 

       前面博客我們介紹了開發struts2應用程序的基本流程(細談struts2之開發第一個struts2的實例),通過前面我們知道了struts2實現請求轉發和配置文件加載都是攔截器進行的操作,這也就是爲什麼我們要在web.xml配置struts2的攔截器的原因了。我們知道,在開發struts2應用開發的時候我們要在web.xml進行配置攔截器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter(在一些老版的一般配置org.apache.struts2.dispatcher.FilterDispatcher),不知道大家剛開始學的時候有沒有這個疑問,爲什麼通過這個攔截器我們就可以攔截到我們提交的請求,並且一些配置文件就可以得到加載呢?不管你有沒有,反正我是有。我想這個問題的答案,我們是非常有必要去看一下這個攔截器的源碼去找。

打開StrutsPrepareAndExecuteFilter攔截器源碼我們可以看出以下類的信息

屬性摘要:

Protected        List<Pattern>              excludedPatterns 

protected      ExecuteOperations      execute 
protected      
PrepareOperations       prepare

         我們可以看出StrutsPrepareAndExecuteFilter與普通的Filter並無區別,方法除繼承自Filter外,僅有一個回調方法,第三部分我們將按照Filter方法調用順序,init>doFilter>destroy順序地分析源碼。

提供的方法:

destroy()    

  繼承自Filter,用於資源釋放

doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

   繼承自Filter,執行方法

init(FilterConfig filterConfig)  

繼承自Filter,初始化參數

 postInit(Dispatcher dispatcher, FilterConfig filterConfig) 

  Callback for post initialization(一個空的方法,用於方法回調初始化)

下面我們一一對這些方法看一下:

1.init方法:我們先整體看一下這個方法:

 

  1. public void init(FilterConfig filterConfig) throws ServletException {  
  2.   
  3. InitOperations init = new InitOperations();  
  4.   
  5. try {  
  6.   
  7. //封裝filterConfig,其中有個主要方法getInitParameterNames將參數名字以String格式存儲在List中  
  8.   
  9. FilterHostConfig config = new FilterHostConfig(filterConfig);  
  10.   
  11. // 初始化struts內部日誌  
  12.   
  13. init.initLogging(config);  
  14.   
  15. //創建dispatcher ,並初始化,這部分下面我們重點分析,初始化時加載那些資源  
  16.   
  17. Dispatcher dispatcher = init.initDispatcher(config);  
  18.   
  19. init.initStaticContentLoader(config, dispatcher);  
  20.   
  21. //初始化類屬性:prepare 、execute  
  22.   
  23. prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);  
  24.   
  25. execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);  
  26.   
  27. this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);  
  28.   
  29. //回調空的postInit方法  
  30.   
  31. postInit(dispatcher, filterConfig);  
  32.   
  33. finally {  
  34.   
  35. init.cleanup();  
  36.   
  37. }  
  38.   
  39. }  


      首先開一下FilterHostConfig 這個封裝configfilter的類:  這個類總共不超過二三十行代碼getInitParameterNames是這個類的核心,將Filter初始化參數名稱有枚舉類型轉爲Iterator。此類的主要作爲是對filterConfig 封裝。具體代碼如下:

 

  1. public Iterator<String> getInitParameterNames() {  
  2.   
  3.        return   MakeIterator.convert(config.getInitParameterNames());  
  4.   
  5.    }  

下面咱接着一塊看Dispatcher dispatcher = init.initDispatcher(config);這是重點,創建並初始化Dispatcher  ,看一下具體代碼:

   

  1. public Dispatcher initDispatcher( HostConfig filterConfig ) {  
  2.   
  3.        Dispatcher dispatcher = createDispatcher(filterConfig);  
  4.   
  5.        dispatcher.init();  
  6.   
  7.        return dispatcher;  
  8.   
  9.    }     
  10.   
  11.   
  12. span style="FONT-SIZE: 18px"><span style="color:#000000;BACKGROUND: rgb(255,255,255)">     </span><span style="color:#000000;BACKGROUND: rgb(255,255,255)"><span style="color:#cc0000;"><strong>創建<span style="font-family:Verdana;">Dispatcher</span><span style="font-family:宋體;">,會讀取 </span><span style="font-family:Verdana;">filterConfig </span><span style="font-family:宋體;">中的配置信息,將配置信息解析出來,封裝成爲一個</span><span style="font-family:Verdana;">Map</span></strong></span><span style="font-family:宋體;">,然後</span></span><span style="color:#000000;BACKGROUND: rgb(255,255,255)">根據</span><span style="color:#000000;BACKGROUND: rgb(255,255,255)">servlet<span style="font-family:宋體;">上下文和參數</span><span style="font-family:Verdana;">Map</span><span style="font-family:宋體;">構造</span><span style="font-family:Verdana;">Dispatcher </span><span style="font-family:宋體;"></span></span></span>  
  1. private Dispatcher createDispatcher( HostConfig filterConfig ) {  
  2.   
  3. Map<String, String> params = new HashMap<String, String>();  
  4.   
  5. for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {  
  6.   
  7. String name = (String) e.next();  
  8.   
  9. String value = filterConfig.getInitParameter(name);  
  10.   
  11. params.put(name, value);  
  12.   
  13. }  
  14.   
  15. return new Dispatcher(filterConfig.getServletContext(), params);  
  16.   
  17. }  


      Dispatcher構造玩以後,開始對他進行初始化,加載struts2的相關配置文件,將按照順序逐一加載:default.propertiesstruts-default.xml,struts-plugin.xml,struts.xml……我們一起看看他是怎麼一步步的加載這些文件的 dispatcher的init()方法:

   

  1. public void init() {  
  2.   
  3.   
  4.     if (configurationManager == null) {  
  5.   
  6.     configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);  
  7.   
  8.     }  
  9.         try {  
  10.   
  11.             init_DefaultProperties(); // [1]  
  12.   
  13.             init_TraditionalXmlConfigurations(); // [2]  
  14.   
  15.             init_LegacyStrutsProperties(); // [3]  
  16.   
  17.             init_CustomConfigurationProviders(); // [5]  
  18.   
  19.             init_FilterInitParameters() ; // [6]  
  20.   
  21.             init_AliasStandardObjects() ; // [7]  
  22.   
  23.   
  24.             Container container = init_PreloadConfiguration();  
  25.   
  26.             container.inject(this);  
  27.   
  28.             init_CheckConfigurationReloading(container);  
  29.   
  30.             init_CheckWebLogicWorkaround(container);  
  31.   
  32.   
  33.             if (!dispatcherListeners.isEmpty()) {  
  34.   
  35.                 for (DispatcherListener l : dispatcherListeners) {  
  36.   
  37.                     l.dispatcherInitialized(this);  
  38.   
  39.                 }  
  40.   
  41.             }  
  42.   
  43.         } catch (Exception ex) {  
  44.   
  45.             if (LOG.isErrorEnabled())  
  46.   
  47.                 LOG.error("Dispatcher initialization failed", ex);  
  48.   
  49.             throw new StrutsException(ex);  
  50.   
  51.         }  
  52.   
  53.     }  


下面我們一起來看一下【1】,【2】,【3】,【5】,【6】的源碼,看一下什麼都一目瞭然了:

1.這個方法中是將一個DefaultPropertiesProvider對象追加到ConfigurationManager對象內部的ConfigurationProvider隊列中。 DefaultPropertiesProviderregister()方法可以載入org/apache/struts2/default.properties中定義的屬性。

  1. try {  
  2.   
  3.            defaultSettings = new PropertiesSettings("org/apache/struts2/default");  
  4.   
  5.        } catch (Exception e) {  
  6.   
  7.            throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);  
  8.   
  9.        }  


2. 調用init_TraditionalXmlConfigurations()方法,實現載入FilterDispatcher的配置中所定義的config屬性。 如果用戶沒有定義config屬性,struts默認會載入DEFAULT_CONFIGURATION_PATHS這個值所代表的xml文件。它的值爲"struts-default.xml,struts-plugin.xml,struts.xml"。也就是說框架默認會載入這三個項目xml文件。如果文件類型是XML格式,則按照xwork-x.x.dtd模板進行讀取。如果,是Struts的配置文件,則按struts-2.X.dtd模板進行讀取。

 private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

3.創建一個LegacyPropertiesConfigurationProvider類,並將它追加到ConfigurationManager對象內部的ConfigurationProvider隊列中。LegacyPropertiesConfigurationProvider類載入struts.properties中的配置,這個文件中的配置可以覆蓋default.properties中的。其子類是DefaultPropertiesProvider

5.init_CustomConfigurationProviders()此方法處理的是FilterDispatcher的配置中所定義的configProviders屬性。負責載入用戶自定義的ConfigurationProvider

  1. String configProvs = initParams.get("configProviders");  
  2.   
  3.         if (configProvs != null) {  
  4.   
  5.             String[] classes = configProvs.split("\\s*[,]\\s*");  
  6.   
  7.             for (String cname : classes) {  
  8.   
  9.                 try {  
  10.   
  11.                     Class cls = ClassLoaderUtils.loadClass(cname, this.getClass());  
  12.   
  13.                     ConfigurationProvider prov = (ConfigurationProvider)cls.newInstance();  
  14.   
  15.                     configurationManager.addConfigurationProvider(prov);  
  16.   
  17.                 } catch (InstantiationException e) {  
  18.   
  19.                     throw new ConfigurationException("Unable to instantiate provider: "+cname, e);  
  20.   
  21.                 } catch (IllegalAccessException e) {  
  22.   
  23.                     throw new ConfigurationException("Unable to access provider: "+cname, e);  
  24.   
  25.                 } catch (ClassNotFoundException e) {  
  26.   
  27.                     throw new ConfigurationException("Unable to locate provider class: "+cname, e);  
  28.   
  29.                 }  
  30.   
  31.             }  
  32.   
  33.         }  


6.init_FilterInitParameters()此方法用來處理FilterDispatcher的配置中所定義的所有屬性

7. init_AliasStandardObjects(),將一個BeanSelectionProvider類追加到ConfigurationManager對象內部的ConfigurationProvider隊列中。BeanSelectionProvider類主要實現加載org/apache/struts2/struts-messages

  1. private void init_AliasStandardObjects() {  
  2.         configurationManager.addConfigurationProvider(  
  3. new BeanSelectionProvider());  
  4. }  



相信看到這大家應該明白了,struts2的一些配置的加載順序和加載時所做的工作,其實有些地方我也不是理解的很清楚。其他具體的就不在說了,init方法佔時先介紹到這

2、doFilter方法

     doFilter是過濾器的執行方法,它攔截提交的HttpServletRequest請求,HttpServletResponse響應,作爲strtus2的核心攔截器,在doFilter裏面到底做了哪些工作,我們將逐行解讀其源碼,大體源碼如下:

  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
  2.   
  3. //父類向子類轉:強轉爲http請求、響應  
  4.   
  5. HttpServletRequest request = (HttpServletRequest) req;  
  6.   
  7. HttpServletResponse response = (HttpServletResponse) res;  
  8.   
  9. try {  
  10.   
  11. //設置編碼和國際化  
  12.   
  13. prepare.setEncodingAndLocale(request, response);  
  14.   
  15. //創建Action上下文(重點)  
  16.   
  17. prepare.createActionContext(request, response);  
  18.   
  19. prepare.assignDispatcherToThread();  
  20.   
  21. if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
  22.   
  23. chain.doFilter(request, response);  
  24.   
  25. } else {  
  26.   
  27. request = prepare.wrapRequest(request);  
  28.   
  29. ActionMapping mapping = prepare.findActionMapping(request, response, true);  
  30.   
  31. if (mapping == null) {  
  32.   
  33. boolean handled = execute.executeStaticResourceRequest(request, response);  
  34.   
  35. if (!handled) {  
  36.   
  37. chain.doFilter(request, response);  
  38.   
  39. }  
  40.   
  41. } else {  
  42.   
  43. execute.executeAction(request, response, mapping);  
  44.   
  45. }  
  46.   
  47. }  
  48.   
  49. } finally {  
  50.   
  51. prepare.cleanupRequest(request);  
  52.   
  53. }  
  54.   
  55. }  


下面我們就逐句的來的看一下:設置字符編碼和國際化很簡單prepare調用了setEncodingAndLocale,然後調用了dispatcher方法的prepare方法:

  1. /**  
  2.   
  3. * Sets the request encoding and locale on the response  
  4.   
  5. */  
  6.   
  7. public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {  
  8.   
  9. dispatcher.prepare(request, response);  
  10.   
  11. }  


看下prepare方法,這個方法很簡單只是設置了encoding locale ,做的只是一些輔助的工作:

  1. public void prepare(HttpServletRequest request, HttpServletResponse response) {  
  2.   
  3. String encoding = null;  
  4.   
  5. if (defaultEncoding != null) {  
  6.   
  7. encoding = defaultEncoding;  
  8.   
  9. }  
  10.   
  11. Locale locale = null;  
  12.   
  13. if (defaultLocale != null) {  
  14.   
  15. locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());  
  16.   
  17. }  
  18.   
  19. if (encoding != null) {  
  20.   
  21. try {  
  22.   
  23. request.setCharacterEncoding(encoding);  
  24.   
  25. } catch (Exception e) {  
  26.   
  27. LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);  
  28.   
  29. }  
  30.   
  31. }  
  32.   
  33. if (locale != null) {  
  34.   
  35. response.setLocale(locale);  
  36.   
  37. }  
  38.   
  39. if (paramsWorkaroundEnabled) {  
  40.   
  41. request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request  
  42.   
  43. }  
  44.   
  45. }  


下面咱重點看一下創建Action上下文重點

   Action上下文創建(重點)

       ActionContext是一個容器,這個容易主要存儲requestsessionapplicationparameters等相關信息.ActionContext是一個線程的本地變量,這意味着不同的action之間不會共享ActionContext,所以也不用考慮線程安全問題。其實質是一個Mapkey是標示requestsession……的字符串,值是其對應的對象:

static ThreadLocal actionContext = new ThreadLocal();

Map<String, Object> context;

下面我們看起來下創建action上下文的源碼

  1. /**  
  2.   
  3. *創建Action上下文,初始化thread local  
  4.   
  5. */  
  6.   
  7. public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {  
  8.   
  9. ActionContext ctx;  
  10.   
  11. Integer counter = 1;  
  12.   
  13. Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);  
  14.   
  15. if (oldCounter != null) {  
  16.   
  17. counter = oldCounter + 1;  
  18.   
  19. }  
  20.   
  21.   
  22.   
  23. //注意此處是從ThreadLocal中獲取此ActionContext變量  
  24.   
  25. ActionContext oldContext = ActionContext.getContext();  
  26.   
  27. if (oldContext != null) {  
  28.   
  29. // detected existing context, so we are probably in a forward  
  30.   
  31. ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));  
  32.   
  33. } else {  
  34.   
  35. ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();  
  36.   
  37. stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));  
  38.   
  39. //stack.getContext()返回的是一個Map<String,Object>,根據此Map構造一個ActionContext  
  40.   
  41. ctx = new ActionContext(stack.getContext());  
  42.   
  43. }  
  44.   
  45. request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);  
  46.   
  47. //將ActionContext存如ThreadLocal  
  48.   
  49. ActionContext.setContext(ctx);  
  50.   
  51. return ctx;  
  52.   
  53. }  


一句句來看:

ValueStackstack= dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();  

dispatcher.getContainer().getInstance(ValueStackFactory.class)根據字面估計一下就是創建ValueStackFactory的實例。這個地方我也只是根據字面來理解的。ValueStackFactory是接口,其默認實現是OgnlValueStackFactory,調用OgnlValueStackFactorycreateValueStack()

下面看一下OgnlValueStack的構造方法  

  1. protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticAccess) {     
  2.   
  3.       //new一個CompoundRoot出來    
  4.   
  5.     setRoot(xworkConverter, accessor, new CompoundRoot(), allowStaticAccess);     
  6.   
  7.     push(prov);     
  8.   
  9. }    


接下來看一下setRoot方法:

  1. protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,     
  2.   
  3.                        boolean allowStaticMethodAccess) {     
  4.   
  5.     //OgnlValueStack.root = compoundRoot;                    
  6.   
  7.     this.root = compoundRoot;     
  8.   
  9. 1    //方法/屬性訪問策略。    
  10.   
  11.     this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);     
  12.   
  13.     //創建context了,創建context使用的是ongl的默認方式。    
  14.   
  15.     //Ognl.createDefaultContext返回一個OgnlContext類型的實例    
  16.   
  17.     //這個OgnlContext裏面,root是OgnlValueStack中的compoundRoot,map是OgnlContext自己創建的private Map _values = new HashMap(23);    
  18.   
  19.     this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);       
  20.   
  21.          
  22.   
  23.      //不是太理解,猜測如下:    
  24.   
  25.     //context是剛剛創建的OgnlContext,其中的HashMap類型_values加入如下k-v:    
  26.   
  27.    //key:com.opensymphony.xwork2.util.ValueStack.ValueStack    
  28.   
  29.     //value:this,這個應該是當前的OgnlValueStack實例。    
  30.   
  31.     //剛剛用斷點跟了一下,_values裏面是:    
  32.   
  33.     //com.opensymphony.xwork2.ActionContext.container=com.opensymphony.xwork2.inject.ContainerImpl@96231e    
  34.   
  35.     //com.opensymphony.xwork2.util.ValueStack.ValueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@4d912    
  36.   
  37.     context.put(VALUE_STACK, this);     
  38.   
  39.   //此時:OgnlValueStack中的compoundRoot是空的;    
  40.   
  41.    //context是一個OgnlContext,其中的_root指向OgnlValueStack中的root,_values裏面的東西,如剛纔所述。    
  42.   
  43.     //OgnlContext中的額外設置。    
  44.   
  45.     Ognl.setClassResolver(context, accessor);     
  46.   
  47.     ((OgnlContext) context).setTraceEvaluations(false);     
  48.   
  49.     ((OgnlContext) context).setKeepLastEvaluation(false);     
  50.   
  51. }             
  52.        

  上面代碼中dispatcher.createContextMap,如何封裝相關參數:,我們以RequestMap爲例,其他的原理都一樣:主要方法實現:

  1. //map的get實現  
  2.   
  3. public Object get(Object key) {  
  4.   
  5. return request.getAttribute(key.toString());  
  6.   
  7. }  
  8.   
  9. //map的put實現  
  10.   
  11. public Object put(Object key, Object value) {  
  12.   
  13. Object oldValue = get(key);  
  14.   
  15. entries = null;  
  16.   
  17. request.setAttribute(key.toString(), value);  
  18.   
  19. return oldValue;  
  20.   
  21. }  


        到此,幾乎StrutsPrepareAndExecuteFilter大部分的源碼都涉及到了。自己感覺都好亂,所以還請大家見諒,能力有限,希望大家可以共同學習

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章