KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > webman > mainint > WebmanExceptionHandler


1 package com.teamkonzept.webman.mainint;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.*;
5 import java.sql.SQLException JavaDoc;
6 import com.teamkonzept.lib.*;
7 import org.apache.log4j.Category;
8 import com.teamkonzept.db.*;
9 import com.teamkonzept.web.*;
10 import com.teamkonzept.webman.mainint.events.*;
11 import com.teamkonzept.international.*;
12
13 /**
14     Webman specific exception handler
15     provides logging and output to the user
16     * @author $Author: alexandergrosse $
17     * @version $Revision: 1.20.6.2 $
18     
19 */

20 public class WebmanExceptionHandler
21     extends DefaultExceptionHandler
22     implements TKEventHandler, ErrorCodes
23 {
24     /** Logging Category */
25     private static Category cat = Category.getInstance(WebmanExceptionHandler.class);
26     
27     /** Logging Views */
28     /*
29     private final static String ALL = "AllErrors";
30
31     private final static String HIGH = "High";
32     private final static String NORMAL = "Normal";
33     private final static String USER = "Usr";
34     private final static String TEMPORARY = "Temp";
35     */

36     private static final String JavaDoc CONTEXT = "ErrorCodes";
37
38     private static WebmanExceptionHandler instance = new WebmanExceptionHandler();
39
40     private WebmanExceptionHandler()
41     {
42         LanguageManager.registerContext("/" + CONTEXT, this);
43     }
44
45     public static WebmanExceptionHandler getInstance()
46     {
47         return instance;
48     }
49
50     /** Developer Mode means more detailled information on the web client */
51     private static boolean developerMode = false;
52
53     /**
54         @return true, if the connections must be closed
55     */

56     public static boolean handleException(TKException e, TKEvent evt)
57     {
58         if (e != null)
59         {
60             int code = e.getErrorCode();
61             try
62             {
63                 int sev = e.getSeverity();
64                 Throwable JavaDoc t = e.getOriginalException();
65                 
66                 if (sev == HIGH_SEVERITY)
67                     cat.fatal("caught HIGH_SEVERITY", t==null ? e:t);
68                 else if (sev == NORMAL_SEVERITY)
69                     cat.error("caught NORMAL", t==null ? e:t);
70                 else if (sev == USER_SEVERITY)
71                     cat.warn("caught USER", t==null ? e:t);
72                 else if (sev == TEMPORARY_SEVERITY)
73                     cat.warn("caught TEMPORARY", t==null ? e:t);
74                 TKHTMLTemplate template = evt.getPrepHTMLTemplate( "error_message.tmpl" );
75                 String JavaDoc title;
76                 if (e instanceof TKDatabaseException)
77                 {
78                     title = LanguageManager.getText(CONTEXT, "DatabaseError", null);
79
80                 }
81                 else if (e instanceof TKUserException)
82                 {
83                     title = LanguageManager.getText(CONTEXT, "UserError", null);
84                 }
85                 else if (e instanceof TKConfigurationException)
86                 {
87                     title = LanguageManager.getText(CONTEXT, "ConfigurationError", null);
88                 }
89                 else
90                 {
91                     title = LanguageManager.getText(CONTEXT, "InternalError", null);
92                 }
93                 template.set( "ERROR_TITLE", title);
94                 template.set("ERROR_USER_TEXT", getErrorText(String.valueOf(code), e.getArguments()));
95                 if (developerMode || e.isPublic())
96                 {
97                     String JavaDoc text = /*"<pre>" + e.toString() + "</pre>" +*/ e.getFullStackTrace();
98                     template.set( "ERROR_TEXT", text );
99                 }
100                 else
101                 {
102                     // eigentlich gar nichts machen
103
template.set( "ERROR_TEXT", ""/*e.getMessage()*/ );
104                 }
105                 evt.finishTemplate(template);
106             } catch ( Throwable JavaDoc th )
107             {
108                 // was jetzt ? Hilfe !!!
109
cat.error("Fehler im EventHandler: " , th);
110                 throw new Error JavaDoc( th.getMessage() );
111             }
112             return (code & DATABASE) > 0;
113         }
114         else
115         {
116             cat.error("Null Exception im ExceptionHandler !!!");
117             return true;
118         }
119     }
120
121     /** handles the event */
122     public void handleEvent(TKEvent evt) throws TKException
123     {
124         String JavaDoc event = evt.getName();
125         if (event.equalsIgnoreCase("TR_DEVELOPER"))
126             developerMode = true;
127         else if (event.equalsIgnoreCase("TR_USER"))
128             developerMode = false;
129         /*else if (event.equalsIgnoreCase("TR_CHANGELOG"))
130             ;//changeLogMode(evt); */

131         else
132             throw new TKException("Invalid Event: " + event, INVALID_EVENT, NORMAL_SEVERITY, false, null);
133     }
134
135     /** returns true, if it wants to handle this event */
136     public boolean isHandler(TKEvent evt)
137     {
138         return evt.getName().startsWith("TR");
139     }
140
141     /**
142         no further event handling
143     */

144     public void addEventHandler(TKEventHandler handler)
145     {
146     }
147
148     /**
149         no further event handling
150     */

151     public void removeEventHandler(TKEventHandler handler)
152     {
153     }
154
155     public static String JavaDoc getErrorText (String JavaDoc code, Object JavaDoc[] arguments)
156     {
157         return LanguageManager.getText(CONTEXT, code, arguments);
158     }
159
160     /** setzt die zu behandelnden Events*/
161     public void setHandleEvents(TKEvent[] events)
162     {}
163
164
165     /** liefert die zu behandelnden Events zurueck */
166     public TKEvent[] getHandleEvents()
167     {
168         return null;
169     }
170
171     /**
172      * Creates an event object.
173      *
174      * @param http the responsible HTTP interface.
175      * @return an event object.
176      */

177     public TKEvent createEvent (TKHttpInterface http)
178     {
179         return null;
180     }
181
182 }
183
Popular Tags