KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > ActionExceptionHandler


1 package com.blandware.atleap.webapp.action.core;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.struts.Globals;
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9 import org.apache.struts.action.ActionMessage;
10 import org.apache.struts.action.ActionMessages;
11 import org.apache.struts.action.ExceptionHandler;
12 import org.apache.struts.config.ExceptionConfig;
13
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19
20
21 /**
22  * <p>Implementation of <strong>ExceptionHandler</strong> that
23  * handles any Exceptions that are bubbled up to the Action
24  * layer. This allows us to remove generic try/catch statements
25  * from our Action Classes.
26  * </p>
27  * <p><a HREF="ActionExceptionHandler.java.htm"><i>View Source</i></a>
28  * </p>
29  *
30  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
31  * @version $Revision: 1.8 $ $Date: 2005/06/06 17:44:59 $
32  */

33 public final class ActionExceptionHandler extends ExceptionHandler {
34     //~ Instance fields ========================================================
35

36     private Log log = LogFactory.getLog(ActionExceptionHandler.class);
37
38     //~ Methods ================================================================
39

40     /**
41      * This method handles any java.lang.Exceptions that are not
42      * caught in previous classes. It will loop through and get
43      * all the causes (exception chain), create ActionErrors,
44      * add them to the request and then forward to the input.
45      *
46      * @see org.apache.struts.action.ExceptionHandler#execute
47      * (
48      * java.lang.Exception,
49      * org.apache.struts.config.ExceptionConfig,
50      * org.apache.struts.action.ActionMapping,
51      * org.apache.struts.action.ActionForm,
52      * javax.servlet.http.HttpServletRequest,
53      * javax.servlet.http.HttpServletResponse
54      * )
55      */

56     public ActionForward execute(Exception JavaDoc ex, ExceptionConfig ae,
57                                  ActionMapping mapping,
58                                  ActionForm formInstance,
59                                  HttpServletRequest JavaDoc request,
60                                  HttpServletResponse JavaDoc response)
61             throws ServletException JavaDoc {
62
63         // exception will be logged in MessagesTag on error.jsp, so we don't need to log it here
64

65         request.setAttribute(Globals.EXCEPTION_KEY, ex);
66
67         try {
68             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
69         } catch ( Exception JavaDoc e ) {
70             throw new ServletException JavaDoc(e);
71         }
72
73         return null;
74     }
75
76     /**
77      * This method overrides the the ExceptionHandler's storeException
78      * method in order to create more than one error message.
79      *
80      * @param request - The request we are handling
81      * @param property - The property name to use for this error
82      * @param error - The error generated from the exception mapping
83      * @param forward - The forward generated from the input path
84      * (from the form or exception mapping)
85      */

86     protected void storeException(HttpServletRequest JavaDoc request, String JavaDoc property,
87                                   ActionMessage error, ActionForward forward) {
88         ActionMessages errors =
89                 (ActionMessages) request.getAttribute(Globals.ERROR_KEY);
90
91         if ( errors == null ) {
92             errors = new ActionMessages();
93         }
94
95         errors.add(property, error);
96
97         request.setAttribute(Globals.ERROR_KEY, errors);
98     }
99
100     /**
101      * Overrides logException method in ExceptionHandler to print the stackTrace
102      *
103      * @see org.apache.struts.action.ExceptionHandler#logException(java.lang.Exception)
104      */

105     protected void logException(Exception JavaDoc ex) {
106         StringWriter JavaDoc sw = new StringWriter JavaDoc();
107         ex.printStackTrace(new PrintWriter JavaDoc(sw));
108         log.error(sw.toString());
109     }
110 }
111
Popular Tags