KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > action > ExceptionHandler


1 /*
2  * $Id: ExceptionHandler.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18  
19 package org.apache.struts.action;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.Globals;
28 import org.apache.struts.config.ExceptionConfig;
29 import org.apache.struts.util.MessageResources;
30 import org.apache.struts.util.ModuleException;
31
32 /**
33  * <p>An <strong>ExceptionHandler</strong> is configured in the Struts
34  * configuration file to handle a specific type of exception thrown
35  * by an <code>Action.execute</code> method.</p>
36  *
37  * @since Struts 1.1
38  */

39 public class ExceptionHandler {
40     
41
42     /**
43      * <p>Commons logging instance.</p>
44      */

45     private static final Log log = LogFactory.getLog(ExceptionHandler.class);
46     
47
48     /**
49      * <p>The message resources for this package.</p>
50      */

51     private static MessageResources messages =
52         MessageResources.getMessageResources(
53             "org.apache.struts.action.LocalStrings");
54     
55
56     /**
57      * <p>Handle the <code>Exception</code>.
58      * Return the <code>ActionForward</code> instance (if any) returned by
59      * the called <code>ExceptionHandler</code>.
60      *
61      * @param ex The exception to handle
62      * @param ae The ExceptionConfig corresponding to the exception
63      * @param mapping The ActionMapping we are processing
64      * @param formInstance The ActionForm we are processing
65      * @param request The servlet request we are processing
66      * @param response The servlet response we are creating
67      *
68      * @exception ServletException if a servlet exception occurs
69      *
70      * @since Struts 1.1
71      */

72     public ActionForward execute(
73         Exception JavaDoc ex,
74         ExceptionConfig ae,
75         ActionMapping mapping,
76         ActionForm formInstance,
77         HttpServletRequest JavaDoc request,
78         HttpServletResponse JavaDoc response)
79         throws ServletException JavaDoc {
80
81         ActionForward forward = null;
82         ActionMessage error = null;
83         String JavaDoc property = null;
84
85         // Build the forward from the exception mapping if it exists
86
// or from the form input
87
if (ae.getPath() != null) {
88             forward = new ActionForward(ae.getPath());
89         } else {
90             forward = mapping.getInputForward();
91         }
92
93         // Figure out the error
94
if (ex instanceof ModuleException) {
95             error = ((ModuleException) ex).getActionMessage();
96             property = ((ModuleException) ex).getProperty();
97         } else {
98             error = new ActionMessage(ae.getKey(), ex.getMessage());
99             property = error.getKey();
100         }
101
102         this.logException(ex);
103
104         // Store the exception
105
request.setAttribute(Globals.EXCEPTION_KEY, ex);
106         this.storeException(request, property, error, forward, ae.getScope());
107
108         return forward;
109
110     }
111
112
113     /**
114      * <p>Logs the <code>Exception</code> using commons-logging.</p>
115      * @param e The Exception to log.
116      * @since Struts 1.2
117      */

118     protected void logException(Exception JavaDoc e){
119
120         log.debug(messages.getMessage("exception.log"), e);
121
122     }
123
124
125     /**
126      * <p>Default implementation for handling an <code>ActionError</code> generated
127      * from an <code>Exception</code> during <code>Action</code> delegation. The default
128      * implementation is to set an attribute of the request or session, as
129      * defined by the scope provided (the scope from the exception mapping). An
130      * <code>ActionErrors</code> instance is created, the error is added to the collection
131      * and the collection is set under the <code>Globals.ERROR_KEY</code>.</p>
132      *
133      * @param request The request we are handling
134      * @param property The property name to use for this error
135      * @param error The error generated from the exception mapping
136      * @param forward The forward generated from the input path (from the form or exception mapping)
137      * @param scope The scope of the exception mapping.
138      * @deprecated Use storeException(HttpServletRequest, String, ActionMessage, ActionForward, String)
139      * instead. This will be removed after Struts 1.2.
140      */

141     protected void storeException(
142         HttpServletRequest JavaDoc request,
143         String JavaDoc property,
144         ActionError error,
145         ActionForward forward,
146         String JavaDoc scope) {
147
148         this.storeException(request, property, (ActionMessage) error, forward, scope);
149         // :TODO: Remove after Struts 1.2
150

151     }
152
153
154     /**
155      * <p>Default implementation for handling an <code>ActionMessage</code> generated
156      * from an <code>Exception</code> during <code>Action</code> delegation. The default
157      * implementation is to set an attribute of the request or session, as
158      * defined by the scope provided (the scope from the exception mapping). An
159      * <code>ActionMessages</code> instance is created, the error is added to the
160      * collection and the collection is set under the <code>Globals.ERROR_KEY</code>.</p>
161      *
162      * @param request The request we are handling
163      * @param property The property name to use for this error
164      * @param error The error generated from the exception mapping
165      * @param forward The forward generated from the input path (from the form or exception mapping)
166      * @param scope The scope of the exception mapping.
167      * @since Struts 1.2
168      */

169     protected void storeException(
170         HttpServletRequest JavaDoc request,
171         String JavaDoc property,
172         ActionMessage error,
173         ActionForward forward,
174         String JavaDoc scope) {
175
176         ActionMessages errors = new ActionMessages();
177         errors.add(property, error);
178
179         if ("request".equals(scope)) {
180             request.setAttribute(Globals.ERROR_KEY, errors);
181         } else {
182             request.getSession().setAttribute(Globals.ERROR_KEY, errors);
183         }
184     }
185
186 }
187
188
Popular Tags