KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > interceptors > ExceptionInterceptor


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.interceptors;
21
22 import net.sf.hibernate.HibernateException;
23 import net.sf.hibernate.StaleObjectStateException;
24 import net.sf.hibernate.util.HibernateUtil;
25
26 import za.org.coefficient.core.CoefficientInterceptor;
27 import za.org.coefficient.interfaces.CoefficientContext;
28
29 import org.apache.commons.beanutils.PropertyUtils;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33
34 import java.lang.reflect.InvocationTargetException JavaDoc;
35
36 import java.util.StringTokenizer JavaDoc;
37
38 /**
39  *
40  * @version $Revision: 1.6 $ $Date: 2004/11/09 13:52:11 $
41  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
42  */

43 public class ExceptionInterceptor extends CoefficientInterceptor {
44     
45     public static final String JavaDoc LAST_UNHANDLED_EXCEPTION = "__last_unhandled_exception__";
46
47     //~ Methods ================================================================
48

49     public int invoke(CoefficientContext ctx) throws Exception JavaDoc {
50         int retVal = INVOKE_PROCESS_CHILD;
51         boolean dont = false;
52
53         try {
54             retVal = super.invoke(ctx);
55             // Always finalize the Hibernate Session
56
HibernateUtil.finalizeSession();
57         } catch (Exception JavaDoc e) {
58             e.printStackTrace();
59             Throwable JavaDoc t = null;
60             if (e instanceof InvocationTargetException JavaDoc) {
61                 t = ((InvocationTargetException JavaDoc) e).getTargetException();
62                 if (t instanceof StaleObjectStateException) {
63                     ctx.setError(
64                         "Another site user updated the data you are changing since you have last read it. Please try your manipulation again, but make sure that your change is still relevant.");
65                     dont = true;
66                 }
67             }
68
69             if (!dont) {
70                 setErrorForException(ctx, e, t);
71             }
72             if(t == null) {
73                 ctx.setSessionAttribute(LAST_UNHANDLED_EXCEPTION, e);
74             } else {
75                 ctx.setSessionAttribute(LAST_UNHANDLED_EXCEPTION, t);
76             }
77             try {
78                 //if(e instanceof HibernateException || t instanceof HibernateException) {
79
// HibernateUtil.clearAndDiscardSession();
80
//} else {
81
HibernateUtil.finalizeSession();
82                     //}
83
} catch(Exception JavaDoc ex) {
84                 setErrorForException(ctx, ex, null);
85             }
86         }
87
88         return retVal;
89     }
90
91     private void setErrorForException(CoefficientContext ctx, Exception JavaDoc e,
92                                       Throwable JavaDoc t) {
93         StringWriter JavaDoc sw = new StringWriter JavaDoc();
94         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
95         if (t == null) {
96             e.printStackTrace(pw);
97         } else {
98             t.printStackTrace(pw);
99         }
100         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sw.toString(), "\n");
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
102         
103         sb.append("<b><font color='red'>There has been a system exception</font></b><br><br>");
104         
105         while (st.hasMoreElements()) {
106             sb.append(st.nextToken())
107                 .append("<br>");
108         }
109         
110         ctx.setError(sb.toString());
111     }
112 }
113
Popular Tags