KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > RequestSynchronizer


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 /**
21  * serializes http requests. Ensures that there is max one request per session
22  */

23 public class RequestSynchronizer {
24   private static final String JavaDoc WEBKEY = "requestSynchronizer";
25   private static Logger logger = Logger.getLogger(RequestSynchronizer.class);
26
27   /**
28    * the uri that shows the content of the current request. The busy.jsp
29    * will redirect to this uri.
30    */

31   private String JavaDoc resultURI = null;
32
33   /**
34    * the thread that is currently performing a request. In BEA, if you do the a
35    * request.getRequestDispatcher().forward() or .include(), this filter will
36    * be called recursively in the same thread. We recognize this by comparing the
37    * threads and allowing the current thread to pass recursively.
38    * <p/>
39    * In Tomcat and Websphere things are different. When doing an include/forward,
40    * the filters are not applied recursively, so the recursive requests always get through.
41    */

42   private Thread JavaDoc currentThread = null;
43
44   RequestSynchronizer() {
45   }
46
47   public static synchronized RequestSynchronizer instance(HttpServletRequest JavaDoc request) {
48     HttpSession JavaDoc session = request.getSession(true);
49     RequestSynchronizer rsync = (RequestSynchronizer) session.getAttribute(WEBKEY);
50     if (rsync == null) {
51       rsync = new RequestSynchronizer();
52       session.setAttribute(WEBKEY, rsync);
53     }
54     return rsync;
55   }
56
57   public interface Handler {
58
59     /**
60      * this request that should be processed normally
61      */

62     void normalRequest() throws Exception JavaDoc;
63
64     /**
65      * this is a recursive request like forward, include etc
66      */

67     void recursiveRequest() throws Exception JavaDoc;
68
69     /**
70      * this should redirect to a page saying "your result is computed, please wait ... ".
71      * The page could then redirect to RequestSynchronizer.instance(session).getResultURI()
72      * to show the result.
73      * @param redirect true, when the current page is not the busy page. false, if
74      * the current page already is the busy page.
75      */

76     void showBusyPage(boolean redirect) throws Exception JavaDoc;
77
78     /**
79      * returns the URI of the result that the busy page should redirect to
80      */

81     String JavaDoc getResultURI();
82
83     /**
84      * true if this request is the busy page
85      */

86     boolean isBusyPage();
87   }
88
89   private synchronized boolean startNormalRequest(Handler handler) {
90     if (currentThread == null) {
91       logInfo("normal request");
92       currentThread = Thread.currentThread();
93       return true;
94     }
95     return false;
96   }
97
98   private synchronized void endNormalRequest() {
99     currentThread = null;
100   }
101
102   private synchronized boolean startRecursiveRequest(Handler handler) {
103     if (Thread.currentThread().equals(currentThread)) {
104       logInfo("recursive request");
105       return true;
106     }
107     return false;
108   }
109
110   private synchronized void endRecursiveRequest() {
111   }
112
113   public void handleRequest(Handler handler) throws Exception JavaDoc {
114     
115     // if this is the busy page, just display
116
if (handler.isBusyPage()) {
117       handler.showBusyPage(false);
118       logInfo("handle-busy-false");
119       return;
120     }
121
122     if (startNormalRequest(handler)) {
123       // no other requests active, run this request normally
124
try {
125         resultURI = handler.getResultURI();
126         logInfo("handle-normal");
127         handler.normalRequest();
128       } finally {
129         endNormalRequest();
130       }
131       resultURI = handler.getResultURI();
132       return;
133     }
134
135     if (startRecursiveRequest(handler)) {
136       // recursive request, same thread
137
try {
138         logInfo("handle-recursive");
139         handler.recursiveRequest();
140       } finally {
141         endRecursiveRequest();
142       }
143       return;
144     }
145
146     // another thread, redirect to busy page
147
logInfo("handle-busy-true");
148     handler.showBusyPage(true);
149   }
150
151   public String JavaDoc getResultURI() {
152     return resultURI;
153   }
154
155   private void logInfo(String JavaDoc id) {
156     if (logger.isInfoEnabled())
157       logger.info("Log " + id + " Thread = " + Thread.currentThread().getName() + ", resultURI = "
158           + resultURI + ", currentThread = " + currentThread);
159   }
160
161 }
Popular Tags