001: package com.teamkonzept.webman.mainint;
002:
003: import java.text.MessageFormat;
004: import java.util.*;
005: import java.sql.SQLException;
006: import com.teamkonzept.lib.*;
007: import org.apache.log4j.Category;
008: import com.teamkonzept.db.*;
009: import com.teamkonzept.web.*;
010: import com.teamkonzept.webman.mainint.events.*;
011: import com.teamkonzept.international.*;
012:
013: /**
014: Webman specific exception handler
015: provides logging and output to the user
016: * @author $Author: alexandergrosse $
017: * @version $Revision: 1.20.6.2 $
018:
019: */
020: public class WebmanExceptionHandler extends DefaultExceptionHandler
021: implements TKEventHandler, ErrorCodes {
022: /** Logging Category */
023: private static Category cat = Category
024: .getInstance(WebmanExceptionHandler.class);
025:
026: /** Logging Views */
027: /*
028: private final static String ALL = "AllErrors";
029:
030: private final static String HIGH = "High";
031: private final static String NORMAL = "Normal";
032: private final static String USER = "Usr";
033: private final static String TEMPORARY = "Temp";
034: */
035: private static final String CONTEXT = "ErrorCodes";
036:
037: private static WebmanExceptionHandler instance = new WebmanExceptionHandler();
038:
039: private WebmanExceptionHandler() {
040: LanguageManager.registerContext("/" + CONTEXT, this );
041: }
042:
043: public static WebmanExceptionHandler getInstance() {
044: return instance;
045: }
046:
047: /** Developer Mode means more detailled information on the web client */
048: private static boolean developerMode = false;
049:
050: /**
051: @return true, if the connections must be closed
052: */
053: public static boolean handleException(TKException e, TKEvent evt) {
054: if (e != null) {
055: int code = e.getErrorCode();
056: try {
057: int sev = e.getSeverity();
058: Throwable t = e.getOriginalException();
059:
060: if (sev == HIGH_SEVERITY)
061: cat
062: .fatal("caught HIGH_SEVERITY",
063: t == null ? e : t);
064: else if (sev == NORMAL_SEVERITY)
065: cat.error("caught NORMAL", t == null ? e : t);
066: else if (sev == USER_SEVERITY)
067: cat.warn("caught USER", t == null ? e : t);
068: else if (sev == TEMPORARY_SEVERITY)
069: cat.warn("caught TEMPORARY", t == null ? e : t);
070: TKHTMLTemplate template = evt
071: .getPrepHTMLTemplate("error_message.tmpl");
072: String title;
073: if (e instanceof TKDatabaseException) {
074: title = LanguageManager.getText(CONTEXT,
075: "DatabaseError", null);
076:
077: } else if (e instanceof TKUserException) {
078: title = LanguageManager.getText(CONTEXT,
079: "UserError", null);
080: } else if (e instanceof TKConfigurationException) {
081: title = LanguageManager.getText(CONTEXT,
082: "ConfigurationError", null);
083: } else {
084: title = LanguageManager.getText(CONTEXT,
085: "InternalError", null);
086: }
087: template.set("ERROR_TITLE", title);
088: template.set("ERROR_USER_TEXT", getErrorText(String
089: .valueOf(code), e.getArguments()));
090: if (developerMode || e.isPublic()) {
091: String text = /*"<pre>" + e.toString() + "</pre>" +*/e
092: .getFullStackTrace();
093: template.set("ERROR_TEXT", text);
094: } else {
095: // eigentlich gar nichts machen
096: template.set("ERROR_TEXT", ""/*e.getMessage()*/);
097: }
098: evt.finishTemplate(template);
099: } catch (Throwable th) {
100: // was jetzt ? Hilfe !!!
101: cat.error("Fehler im EventHandler: ", th);
102: throw new Error(th.getMessage());
103: }
104: return (code & DATABASE) > 0;
105: } else {
106: cat.error("Null Exception im ExceptionHandler !!!");
107: return true;
108: }
109: }
110:
111: /** handles the event */
112: public void handleEvent(TKEvent evt) throws TKException {
113: String event = evt.getName();
114: if (event.equalsIgnoreCase("TR_DEVELOPER"))
115: developerMode = true;
116: else if (event.equalsIgnoreCase("TR_USER"))
117: developerMode = false;
118: /*else if (event.equalsIgnoreCase("TR_CHANGELOG"))
119: ;//changeLogMode(evt); */
120: else
121: throw new TKException("Invalid Event: " + event,
122: INVALID_EVENT, NORMAL_SEVERITY, false, null);
123: }
124:
125: /** returns true, if it wants to handle this event */
126: public boolean isHandler(TKEvent evt) {
127: return evt.getName().startsWith("TR");
128: }
129:
130: /**
131: no further event handling
132: */
133: public void addEventHandler(TKEventHandler handler) {
134: }
135:
136: /**
137: no further event handling
138: */
139: public void removeEventHandler(TKEventHandler handler) {
140: }
141:
142: public static String getErrorText(String code, Object[] arguments) {
143: return LanguageManager.getText(CONTEXT, code, arguments);
144: }
145:
146: /** setzt die zu behandelnden Events*/
147: public void setHandleEvents(TKEvent[] events) {
148: }
149:
150: /** liefert die zu behandelnden Events zurueck */
151: public TKEvent[] getHandleEvents() {
152: return null;
153: }
154:
155: /**
156: * Creates an event object.
157: *
158: * @param http the responsible HTTP interface.
159: * @return an event object.
160: */
161: public TKEvent createEvent(TKHttpInterface http) {
162: return null;
163: }
164:
165: }
|