posts - 64,comments - 22,trackbacks - 0
               摘要: Servlet 3.0作為Java EE6規(guī)范體系中一員,隨著Java EE6規(guī)范一起發(fā)布。該版本在前一版本(Servlet 2.5)的基礎(chǔ)上提供了若干新特性用于簡化Web應(yīng)用的開發(fā)和部署。  閱讀全文
          posted @ 2013-01-16 11:08 hellxoul 閱讀(52) | 評論 (0)編輯 收藏
               摘要: n 可以用來改變根據(jù)自己需要改變加載的優(yōu)先級!  閱讀全文
          posted @ 2013-01-16 10:50 hellxoul 閱讀(63) | 評論 (0)編輯 收藏
              只有注冊用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2013-01-16 10:46 hellxoul 閱讀(70) | 評論 (0)編輯 收藏
              只有注冊用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2013-01-16 10:44 hellxoul 閱讀(61) | 評論 (0)編輯 收藏
              只有注冊用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2012-12-21 11:03 hellxoul 閱讀(79) | 評論 (0)編輯 收藏
          接下來,我們將重點(diǎn)討論一下Struts2中的攔截器的內(nèi)部結(jié)構(gòu)和執(zhí)行順序,并結(jié)合源碼進(jìn)行分析。

          Interceptor結(jié)構(gòu) Top

          讓我們再來回顧一下之前我們曾經(jīng)用過的一張Action LifeCycle的圖:



          圖中,我們可以發(fā)現(xiàn),Struts2的Interceptor一層一層,把Action包裹在最里面。這樣的結(jié)構(gòu),大概有以下一些特點(diǎn):

          1. 整個結(jié)構(gòu)就如同一個堆棧,除了Action以外,堆棧中的其他元素是Interceptor

          2. Action位于堆棧的底部。由于堆棧"先進(jìn)后出"的特性,如果我們試圖把Action拿出來執(zhí)行,我們必須首先把位于Action上端的Interceptor拿出來執(zhí)行。這樣,整個執(zhí)行就形成了一個遞歸調(diào)用

          3. 每個位于堆棧中的Interceptor,除了需要完成它自身的邏輯,還需要完成一個特殊的執(zhí)行職責(zé)。這個執(zhí)行職責(zé)有3種選擇:

          1) 中止整個執(zhí)行,直接返回一個字符串作為resultCode

          2) 通過遞歸調(diào)用負(fù)責(zé)調(diào)用堆棧中下一個Interceptor的執(zhí)行

          3) 如果在堆棧內(nèi)已經(jīng)不存在任何的Interceptor,調(diào)用Action


          Struts2的攔截器結(jié)構(gòu)的設(shè)計(jì),實(shí)際上是一個典型的責(zé)任鏈模式的應(yīng)用。首先將整個執(zhí)行劃分成若干相同類型的元素,每個元素具備不同的邏輯責(zé)任,并將他們納入到一個鏈?zhǔn)降臄?shù)據(jù)結(jié)構(gòu)中(我們可以把堆棧結(jié)構(gòu)也看作是一個遞歸的鏈?zhǔn)浇Y(jié)構(gòu)),而每個元素又有責(zé)任負(fù)責(zé)鏈?zhǔn)浇Y(jié)構(gòu)中下一個元素的執(zhí)行調(diào)用。

          這樣的設(shè)計(jì),從代碼重構(gòu)的角度來看,實(shí)際上是將一個復(fù)雜的系統(tǒng),分而治之,從而使得每個部分的邏輯能夠高度重用并具備高度可擴(kuò)展性。所以,Interceptor結(jié)構(gòu)實(shí)在是Struts2/Xwork設(shè)計(jì)中的精華之筆。

          Interceptor執(zhí)行分析 Top

          Interceptor的定義

          我們來看一下Interceptor的接口的定義:

          Java代碼  收藏代碼
          1. public interface Interceptor extends Serializable {  
          2.   
          3.     /** 
          4.      * Called to let an interceptor clean up any resources it has allocated. 
          5.      */  
          6.     void destroy();  
          7.   
          8.     /** 
          9.      * Called after an interceptor is created, but before any requests are processed using 
          10.      * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving 
          11.      * the Interceptor a chance to initialize any needed resources. 
          12.      */  
          13.     void init();  
          14.   
          15.     /** 
          16.      * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the 
          17.      * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code. 
          18.      * 
          19.      * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself. 
          20.      * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}. 
          21.      */  
          22.     String intercept(ActionInvocation invocation) throws Exception;  
          23. }  


          Interceptor的接口定義沒有什么特別的地方,除了init和destory方法以外,intercept方法是實(shí)現(xiàn)整個攔截器機(jī)制的核心方法。而它所依賴的參數(shù)ActionInvocation則是我們之前章節(jié)中曾經(jīng)提到過的著名的Action調(diào)度者

          我們再來看看一個典型的Interceptor的抽象實(shí)現(xiàn)類:

          Java代碼  收藏代碼
          1. public abstract class AroundInterceptor extends AbstractInterceptor {  
          2.       
          3.     /* (non-Javadoc) 
          4.      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) 
          5.      */  
          6.     @Override  
          7.     public String intercept(ActionInvocation invocation) throws Exception {  
          8.         String result = null;  
          9.   
          10.         before(invocation);  
          11.         // 調(diào)用下一個攔截器,如果攔截器不存在,則執(zhí)行Action  
          12.         result = invocation.invoke();  
          13.         after(invocation, result);  
          14.   
          15.         return result;  
          16.     }  
          17.       
          18.     public abstract void before(ActionInvocation invocation) throws Exception;  
          19.   
          20.     public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;  
          21.   
          22. }  


          在這個實(shí)現(xiàn)類中,實(shí)際上已經(jīng)實(shí)現(xiàn)了最簡單的攔截器的雛形。或許大家對這樣的代碼還比較陌生,這沒有關(guān)系。我在這里需要指出的是一個很重要的方法 invocation.invoke()。這是ActionInvocation中的方法,而ActionInvocation是Action調(diào)度者,所 以這個方法具備以下2層含義:

          1. 如果攔截器堆棧中還有其他的Interceptor,那么invocation.invoke()將調(diào)用堆棧中下一個Interceptor的執(zhí)行。

          2. 如果攔截器堆棧中只有Action了,那么invocation.invoke()將調(diào)用Action執(zhí)行。

          所以,我們可以發(fā)現(xiàn),invocation.invoke()這個方法其實(shí)是整個攔截器框架的實(shí)現(xiàn)核心。基于這樣的實(shí)現(xiàn)機(jī)制,我們還可以得到下面2個非常重要的推論:

          1. 如果在攔截器中,我們不使用invocation.invoke()來完成堆棧中下一個元素的調(diào)用,而是直接返回一個字符串作為執(zhí)行結(jié)果,那么整個執(zhí)行將被中止。

          2. 我們可以以invocation.invoke()為界,將攔截器中的代碼分成2個部分,在invocation.invoke()之前的代碼,將會在 Action之前被依次執(zhí)行,而在invocation.invoke()之后的代碼,將會在Action之后被逆序執(zhí)行。

          由此,我們就可以通過invocation.invoke()作為Action代碼真正的攔截點(diǎn),從而實(shí)現(xiàn)AOP。

          Interceptor攔截類型

          從上面的分析,我們知道,整個攔截器的核心部分是invocation.invoke()這個函數(shù)的調(diào)用位置。事實(shí)上,我們也正式根據(jù)這句代碼的調(diào)用位置,來進(jìn)行攔截類型的區(qū)分的。在Struts2中,Interceptor的攔截類型,分成以下三類:

          1. before

          before攔截,是指在攔截器中定義的代碼,它們存在于invocation.invoke()代碼執(zhí)行之前。這些代碼,將依照攔截器定義的順序,順序執(zhí)行

          2. after

          after攔截,是指在攔截器中定義的代碼,它們存在于invocation.invoke()代碼執(zhí)行之后。這些代碼,將一招攔截器定義的順序,逆序執(zhí)行

          3. PreResultListener

          有的時候,before攔截和after攔截對我們來說是不夠的,因?yàn)槲覀冃枰贏ction執(zhí)行完之后,但是還沒有回到視圖層之前,做一些事 情。Struts2同樣支持這樣的攔截,這種攔截方式,是通過在攔截器中注冊一個PreResultListener的接口來實(shí)現(xiàn)的。

          Java代碼  收藏代碼
          1. public interface PreResultListener {  
          2.   
          3.     /** 
          4.      * This callback method will be called after the Action execution and before the Result execution. 
          5.      * 
          6.      * @param invocation 
          7.      * @param resultCode 
          8.      */  
          9.     void beforeResult(ActionInvocation invocation, String resultCode);  
          10. }  


          在這里,我們看到,Struts2能夠支持如此多的攔截類型,與其本身的數(shù)據(jù)結(jié)構(gòu)和整體設(shè)計(jì)有很大的關(guān)系。正如我在之前的文章中所提到的:

          downpour 寫道
          因?yàn)锳ction是一個普通的Java類,而不是一個Servlet類,完全脫離于Web容器,所以我們就能夠更加方便地對Control層進(jìn)行合理的層次設(shè)計(jì),從而抽象出許多公共的邏輯,并將這些邏輯脫離出Action對象本身。


          我們可以看到,Struts2對于整個執(zhí)行的劃分,從Interceptor到Action一直到Result,每一層都職責(zé)明確。不僅如此,Struts2還為每一個層次之前都設(shè)立了恰如其分的插入點(diǎn)。使得整個Action層的擴(kuò)展性得到了史無前例的提升。

          Interceptor執(zhí)行順序

          Interceptor的執(zhí)行順序或許是我們在整個過程中最最關(guān)心的部分。根據(jù)上面所提到的概念,我們實(shí)際上已經(jīng)能夠大致明白了Interceptor的執(zhí)行機(jī)理。我們來看看Struts2的Reference對Interceptor執(zhí)行順序的一個形象的例子。

          如果我們有一個interceptor-stack的定義如下:

          Xml代碼  收藏代碼
          1. <interceptor-stack name="xaStack">  
          2.   <interceptor-ref name="thisWillRunFirstInterceptor"/>  
          3.   <interceptor-ref name="thisWillRunNextInterceptor"/>  
          4.   <interceptor-ref name="followedByThisInterceptor"/>  
          5.   <interceptor-ref name="thisWillRunLastInterceptor"/>  
          6. </interceptor-stack>  


          那么,整個執(zhí)行的順序大概像這樣:



          在這里,我稍微改了一下Struts2的Reference中的執(zhí)行順序示例,使得整個執(zhí)行順序更加能夠被理解。我們可以看到,遞歸調(diào)用保證了各種各樣的攔截類型的執(zhí)行能夠井井有條。

          請注意在這里,每個攔截器中的代碼的執(zhí)行順序,在Action之前,攔截器的執(zhí)行順序與堆棧中定義的一致;而在Action和Result之后,攔截器的執(zhí)行順序與堆棧中定義的順序相反。

          源碼解析 Top

          接下來我們就來看看源碼,看看Struts2是如何保證攔截器、Action與Result三者之間的執(zhí)行順序的。

          之前我曾經(jīng)提到,ActionInvocation是Struts2中的調(diào)度器,所以事實(shí)上,這些代碼的調(diào)度執(zhí)行,是在 ActionInvocation的實(shí)現(xiàn)類中完成的,這里,我抽取了DefaultActionInvocation中的invoke()方法,它將向我 們展示一切。

          Java代碼  收藏代碼
          1. /** 
          2.  * @throws ConfigurationException If no result can be found with the returned code 
          3.  */  
          4. public String invoke() throws Exception {  
          5.     String profileKey = "invoke: ";  
          6.     try {  
          7.         UtilTimerStack.push(profileKey);  
          8.               
          9.         if (executed) {  
          10.             throw new IllegalStateException("Action has already executed");  
          11.         }  
          12.         // 依次調(diào)用攔截器堆棧中的攔截器代碼執(zhí)行  
          13.         if (interceptors.hasNext()) {  
          14.             final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();  
          15.             UtilTimerStack.profile("interceptor: "+interceptor.getName(),   
          16.                     new UtilTimerStack.ProfilingBlock<String>() {  
          17.                         public String doProfiling() throws Exception {  
          18.                          // 將ActionInvocation作為參數(shù),調(diào)用interceptor中的intercept方法執(zhí)行  
          19.                             resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  
          20.                             return null;  
          21.                         }  
          22.             });  
          23.         } else {  
          24.             resultCode = invokeActionOnly();  
          25.         }  
          26.   
          27.         // this is needed because the result will be executed, then control will return to the Interceptor, which will  
          28.         // return above and flow through again  
          29.         if (!executed) {  
          30.             // 執(zhí)行PreResultListener  
          31.             if (preResultListeners != null) {  
          32.                 for (Iterator iterator = preResultListeners.iterator();  
          33.                     iterator.hasNext();) {  
          34.                     PreResultListener listener = (PreResultListener) iterator.next();  
          35.                           
          36.                     String _profileKey="preResultListener: ";  
          37.                     try {  
          38.                             UtilTimerStack.push(_profileKey);  
          39.                             listener.beforeResult(this, resultCode);  
          40.                     }  
          41.                     finally {  
          42.                             UtilTimerStack.pop(_profileKey);  
          43.                     }  
          44.                 }  
          45.             }  
          46.   
          47.             // now execute the result, if we're supposed to  
          48.             // action與interceptor執(zhí)行完畢,執(zhí)行Result  
          49.             if (proxy.getExecuteResult()) {  
          50.                 executeResult();  
          51.             }  
          52.   
          53.             executed = true;  
          54.         }  
          55.   
          56.         return resultCode;  
          57.     }  
          58.     finally {  
          59.         UtilTimerStack.pop(profileKey);  
          60.     }  
          61. }  


          從源碼中,我們可以看到,我們之前提到的Struts2的Action層的4個不同的層次,在這個方法中都有體現(xiàn),他們分別是:攔截器 (Interceptor)、Action、PreResultListener和Result。在這個方法中,保證了這些層次的有序調(diào)用和執(zhí)行。由此我 們也可以看出Struts2在Action層次設(shè)計(jì)上的眾多考慮,每個層次都具備了高度的擴(kuò)展性和插入點(diǎn),使得程序員可以在任何喜歡的層次加入自己的實(shí)現(xiàn)機(jī)制改變Action的行為。

          在這里,需要特別強(qiáng)調(diào)的,是其中攔截器部分的執(zhí)行調(diào)用:

          Java代碼  收藏代碼
          1. resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  


          表面上,它只是執(zhí)行了攔截器中的intercept方法,如果我們結(jié)合攔截器來看,就能看出點(diǎn)端倪來:

          Java代碼  收藏代碼
          1. public String intercept(ActionInvocation invocation) throws Exception {  
          2.     String result = null;  
          3.   
          4.         before(invocation);  
          5.         // 調(diào)用invocation的invoke()方法,在這里形成了遞歸調(diào)用  
          6.         result = invocation.invoke();  
          7.         after(invocation, result);  
          8.   
          9.         return result;  
          10. }  


          原來在intercept()方法又對ActionInvocation的invoke()方法進(jìn)行遞歸調(diào)用,ActionInvocation 循環(huán)嵌套在intercept()中,一直到語句result = invocation.invoke()執(zhí)行結(jié)束。這樣,Interceptor又會按照剛開始執(zhí)行的逆向順序依次執(zhí)行結(jié)束。
          posted @ 2012-12-14 10:41 hellxoul 閱讀(268) | 評論 (0)編輯 收藏

          自己制作ssl證書:自己簽發(fā)免費(fèi)ssl證書,為nginx生成自簽名ssl證書

              這里說下Linux 系統(tǒng)怎么通過openssl命令生成 證書。

            首先執(zhí)行如下命令生成一個key
          openssl genrsa -des3 -out ssl.key 1024
              然后他會要求你輸入這個key文件的密碼。不推薦輸入。因?yàn)橐院笠onginx使用。每次reload nginx配置時候都要你驗(yàn)證這個PAM密碼的。
              由于生成時候必須輸入密碼。你可以輸入后 再刪掉。

          mv ssl.key xxx.key
          openssl rsa -in xxx.key -out ssl.key
          rm xxx.key
              然后根據(jù)這個key文件生成證書請求文件
          openssl req -new -key ssl.key -out ssl.csr
              以上命令生成時候要填很多東西 一個個看著寫吧(可以隨便,畢竟這是自己生成的證書)

              最后根據(jù)這2個文件生成crt證書文件
          openssl x509 -req -days 365 -in ssl.csr -signkey ssl.key -out ssl.crt
              這里365是證書有效期 推薦3650哈哈。這個大家隨意。最后使用到的文件是key和crt文件。

              如果需要用pfx 可以用以下命令生成
          openssl pkcs12 -export -inkey ssl.key -in ssl.crt -out ssl.pfx

              在需要使用證書的nginx配置文件的server節(jié)點(diǎn)里加入以下配置就可以了。
          ssl on;
          ssl_certificate /home/ssl.crt;
          ssl_certificate_key /home/ssl.key;
          ssl_session_timeout 5m;
          ssl_protocols SSLv2 SSLv3 TLSv1;
          ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
          ssl_prefer_server_ciphers on;
              然后重啟nginx就大功告成了

          posted @ 2012-11-29 16:50 hellxoul 閱讀(2067) | 評論 (0)編輯 收藏

          Nginx + https + 免費(fèi)SSL證書配置指南

          時間:2010-02-24 20:15:42   類別:技術(shù)   訪問:11,529 views   RSS 2.0   評論  

          請參考 Nginx Wiki http://wiki.nginx.org/NginxHttpSslModule

          生成證書

          $ cd /usr/local/nginx/conf
          $ openssl genrsa -des3 -out server.key 1024
          $ openssl req -new -key server.key -out server.csr
          $ cp server.key server.key.org
          $ openssl rsa -in server.key.org -out server.key
          $ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

          編輯 nginx.conf

          server {
              server_name YOUR_DOMAINNAME_HERE;
              listen 443;
              ssl on;
              ssl_certificate /usr/local/nginx/conf/server.crt;
              ssl_certificate_key /usr/local/nginx/conf/server.key;
          }

          OK, 完成了。但這樣證書是不被信任的,自己玩玩還行,要被信任請看下面。

          以下內(nèi)容轉(zhuǎn)載自
          http://goo.gl/YOb5
          http://goo.gl/Gftj

          HTTPS(全稱:Hypertext Transfer Protocol over Secure Socket Layer),是以安全為目標(biāo)的HTTP通道,簡單講是HTTP的安全版。即HTTP下加入SSL層,HTTPS的安全基礎(chǔ)是SSL,因此加密的詳細(xì)內(nèi)容 請看SSL。

          它是一個URI scheme(抽象標(biāo)識符體系),句法類同http:體系。用于安全的HTTP數(shù)據(jù)傳輸。https:URL表明它使用了HTTP,但HTTPS存在不同 于HTTP的默認(rèn)端口及一個加密/身份驗(yàn)證層(在HTTP與TCP之間)。這個系統(tǒng)的最初研發(fā)由網(wǎng)景公司進(jìn)行,提供了身份驗(yàn)證與加密通訊方法,現(xiàn)在它被廣 泛用于萬維網(wǎng)上安全敏感的通訊,例如交易支付方面。

          1、自行頒發(fā)不受瀏覽器信任的SSL證書:
          HTTPS的SSL證書可以自行頒發(fā),Linux下的頒發(fā)步驟如下:

          openssl genrsa -des3 -out api.bz.key 1024
          openssl  req -new -key api.bz.key -out api.bz.csr
          openssl rsa -in api.bz.key  -out api.bz_nopass.key

          Nginx + https + 免費(fèi)SSL證書配置指南 7f13bbb85e123811 thumb

          nginx.conf 的SSL證書配置,使用 api.bz_nopass.key,在啟動Nginx是無需輸入SSL證書密碼,而使用 api.bz.key 則需要輸入密碼:

          server {
          server_name sms.api.bz;
          listen  443;
          index index.html index.htm index.php;
          root  /data0/htdocs/api.bz;
          ssl on;
          ssl_certificate api.bz.crt;
          ssl_certificate_key api.bz_nopass.key;
          ......
          }

          自行頒發(fā)的SSL證書雖然能夠?qū)崿F(xiàn)加密傳輸功能,但得不到瀏覽器的信任,會出現(xiàn)以下提示:
          Nginx + https + 免費(fèi)SSL證書配置指南 6c3f2b38523ed259

          posted @ 2012-11-29 14:44 hellxoul 閱讀(393) | 評論 (0)編輯 收藏
              只有注冊用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2012-11-08 12:52 hellxoul 閱讀(39) | 評論 (0)編輯 收藏
              只有注冊用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2012-10-29 13:01 hellxoul 閱讀(78) | 評論 (0)編輯 收藏
          僅列出標(biāo)題
          共7頁: 上一頁 1 2 3 4 5 6 7 下一頁 
          主站蜘蛛池模板: 嵊泗县| 延庆县| 乌兰浩特市| 柞水县| 万载县| 朝阳市| 伽师县| 莫力| 龙口市| 措勤县| 鄂托克旗| 盱眙县| 正阳县| 武定县| 云南省| 阿克陶县| 海兴县| 巴彦县| 内黄县| 三门县| 临猗县| 高阳县| 商南县| 沂南县| 新干县| 滨海县| 曲松县| 广水市| 金寨县| 定西市| 沈阳市| 砚山县| 仙居县| 石泉县| 金寨县| 乐平市| 从化市| 永寿县| 股票| 定襄县| 连云港市|