KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > BaseExceptionHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.actionflow;
8
9
10 import java.io.IOException JavaDoc;
11
12 import javax.servlet.RequestDispatcher JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 import org.apache.log4j.Logger;
18
19 import com.inversoft.verge.mvc.MVCException;
20 import com.inversoft.verge.mvc.controller.GenericResult;
21 import com.inversoft.verge.mvc.controller.LongTxnSetup;
22 import com.inversoft.verge.mvc.controller.Result;
23 import com.inversoft.verge.mvc.controller.actionflow.config.BaseNamespace;
24 import com.inversoft.verge.mvc.controller.actionflow.config.Namespace;
25 import com.inversoft.verge.mvc.controller.actionflow.config.Node;
26
27
28 /**
29  * <p>
30  * This class is the base implementation of the ExceptionHandler
31  * interface. This implementation provides default functionality
32  * that handles exceptions only if certain conditions are
33  * true. The conditions are checked in the order given below.
34  * The conditions are as follows:
35  * </p>
36  *
37  * <p>
38  * <ul>
39  * <li>If the system property named
40  * <code>verge.controller.actionflow.errorPage</code> is set
41  * this forwards the request to that page and stores the
42  * exception in the request using the key REQUEST_EXCEPTION_KEY,
43  * which is a constant of this class.</li>
44  * <li>If the ActionFlowAction contains a Node that contains
45  * a valid Namespace and the Namespace is a {@link
46  * BaseNamespace BaseNamespace} and contains a valid value
47  * for the {@link BaseNamespace#getErrorPage() getErrorPage()}
48  * property, this forwards the request to that error page
49  * and stores the exception in the request using the key
50  * REQUEST_EXCEPTION_KEY, which is a constant of this class.</li>
51  * </ul>
52  * </p>
53  *
54  * @author Brian Pontarelli
55  * @since 2.0
56  * @version 2.0
57  */

58 public class BaseExceptionHandler implements ExceptionHandler {
59
60     /**
61      * This classes logger
62      */

63     private static final Logger logger = Logger.getLogger(BaseExceptionHandler.class);
64
65     /**
66      * The name of the system property that controls the error page (currently
67      * 'verge.controller.actionflow.errorPage')
68      */

69     public static final String JavaDoc ERROR_PAGE_SYSTEM_PROPERTY = "verge.controller.actionflow.errorPage";
70
71     /**
72      * The request key used to store the exception (currently the String 'exception')
73      */

74     public static final String JavaDoc REQUEST_EXCEPTION_KEY = "exception";
75
76
77     /**
78      * Constructs a new <code>BaseExceptionHandler</code>.
79      */

80     public BaseExceptionHandler() {
81     }
82
83
84     /**
85      * Handles the given exception in the manner described in the class comment.
86      *
87      * @param exception The Exception to handle
88      * @param action The ActionFlowAction used for the request and the Node
89      * @return True if this handled the exception, false if it was unable to
90      */

91     public boolean handleException(Exception JavaDoc exception, ActionFlowAction action) {
92
93         assert (action != null) : "action == null";
94
95         HttpServletRequest JavaDoc request = action.getHttpServletRequest();
96         HttpServletResponse JavaDoc response = action.getHttpServletResponse();
97         String JavaDoc url = System.getProperty(ERROR_PAGE_SYSTEM_PROPERTY);
98
99         if (url == null && action.getNode() != null) {
100             Namespace namespace = action.getNode().getNamespace();
101             if (namespace != null && namespace instanceof BaseNamespace) {
102                 url = ((BaseNamespace) namespace).getErrorPage();
103             }
104         }
105
106         RequestDispatcher JavaDoc rd = null;
107         boolean result = false;
108         if (url != null) {
109
110             // Handle the end long transcation support
111
Node node = action.getNode();
112             if (node.isLongTxnEnabled()) {
113                 result = handleLongTxn(action, url);
114             } else {
115                 rd = request.getRequestDispatcher(url);
116                 try {
117                     rd.forward(request, response);
118                     result = true;
119                 } catch (IOException JavaDoc ioe) {
120                     logger.error("Error while forwarding to error page", ioe);
121                 } catch (ServletException JavaDoc se) {
122                     logger.error("Error while forwarding to error page", se);
123                 }
124             }
125         }
126
127         return result;
128     }
129
130     /**
131      * Since it is possible that the ActionFlow system was in the middle of a
132      * long transaction when the exception occurred, this method is designed to
133      * render the rest of the long transaction HTML which should include some
134      * method of directing the user to the error page. Currently, this is handled
135      * using the {@link com.inversoft.verge.mvc.controller.LongTxnHandler
136      * LongTxnHandler} registered with the {@link ActionFlowExecutor
137      * ActionFlowExecutor} and using the {@link LongTxnSetup LongTxnSetup} for
138      * the namespace of the node last executed.
139      *
140      * @param action The action to retrieve the request, response, node,
141      * namespace, etc from.
142      * @param url The error page URL if one exists
143      * @return True if this handled the error and the long transaction ending
144      * both successfully, false otherwise
145      */

146     protected boolean handleLongTxn(ActionFlowAction action, String JavaDoc url) {
147         boolean result = false;
148         HttpServletRequest JavaDoc request = action.getHttpServletRequest();
149         HttpServletResponse JavaDoc response = action.getHttpServletResponse();
150         LongTxnSetup setup = action.getNode().getNamespace().getLongTxnSetup();
151         if (setup != null) {
152             String JavaDoc longTxnURL = setup.getLongTxnEndURL(action);
153             String JavaDoc category = setup.getLongTxnURLCategory(action);
154             Result ret = new GenericResult(url, category);
155
156             try {
157                 ActionFlowExecutor.handler.handleEndLongTxn(request,
158                     response, longTxnURL, ret);
159                 result = true;
160             } catch (MVCException mvce) {
161                 logger.error("Error while handling long transaction " +
162                     "ending inconjunction with an error page", mvce);
163             }
164         }
165
166         return result;
167     }
168 }
Popular Tags