KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > actions > ExceptionHandlerAction


1 /*
2  * $Id: ExceptionHandlerAction.java,v 1.3 2005/06/07 12:32:34 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 31.05.2004
27  *
28  */

29 package org.jresearch.gossip.actions;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.log.Logger;
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42 import org.apache.struts.action.ExceptionHandler;
43 import org.apache.struts.config.ExceptionConfig;
44 import org.jresearch.gossip.IConst;
45 import org.jresearch.gossip.exception.SystemException;
46 import org.jresearch.gossip.log.avalon.JGossipLog;
47
48 /**
49  * @author Dmitry Belov
50  *
51  */

52 public class ExceptionHandlerAction extends ExceptionHandler {
53
54     private Logger log;
55
56     /**
57      * Default c'tor.
58      */

59     public ExceptionHandlerAction() {
60         super();
61         try {
62             log = JGossipLog.getInstance().getAppLogger();
63         } catch (SystemException e) { /* Ignore Exception! */
64         }
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.apache.struts.action.ExceptionHandler#execute(java.lang.Exception,
71      * org.apache.struts.config.ExceptionConfig,
72      * org.apache.struts.action.ActionMapping,
73      * org.apache.struts.action.ActionForm,
74      * javax.servlet.http.HttpServletRequest,
75      * javax.servlet.http.HttpServletResponse)
76      */

77     public ActionForward execute(Exception JavaDoc ex, ExceptionConfig econf,
78             ActionMapping mapping, ActionForm formInstance,
79             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80             throws ServletException JavaDoc {
81         ActionForward forward = super.execute(ex, econf, mapping, formInstance,
82                 request, response);
83         StringBuffer JavaDoc message = new StringBuffer JavaDoc();
84         if (ex instanceof SystemException) {
85             SystemException se = (SystemException) ex;
86             if (se.getNestedException() == null) {
87                 message.append(se.getTrace());
88             } else {
89                 Exception JavaDoc e = se.getNestedException();
90                 message.append(e.getMessage());
91                 message.append("\n");
92                 message.append(se.getTrace());
93             }
94         } else {
95             message.append(ex.getMessage());
96             ex.printStackTrace();
97         }
98         logException(ex);
99         request.setAttribute(IConst.CONTEXT.SYSTEM_EXCEPTION_MESSAGE, message
100                 .toString());
101         return forward;
102     }
103
104     protected void logException(Exception JavaDoc ex) {
105         StringWriter JavaDoc sw = new StringWriter JavaDoc();
106         ex.printStackTrace(new PrintWriter JavaDoc(sw));
107         log.error(sw.toString());
108     }
109 }
Popular Tags