001: package dinamica;
002:
003: import java.io.PrintWriter;
004: import java.io.StringWriter;
005:
006: /**
007: * Transaction module for the general error manager
008: *
009: * <br>
010: * Creation date: jan/13/2004<br>
011: * Last Update: march/1/2005<br>
012: * (c) 2004 Martin Cordova<br>
013: * This code is released under the LGPL license<br>
014: * @author Martin Cordova
015: * */
016: public class ErrorHandler extends GenericTransaction {
017:
018: /* (non-Javadoc)
019: * @see dinamica.GenericTransaction#service(dinamica.Recordset)
020: */
021: public int service(Recordset inputParams) throws Throwable {
022:
023: //capture referer page
024: String referer = getRequest().getHeader("Referer");
025: getRequest().setAttribute("dinamica.error.referer", referer);
026: getRequest().setAttribute("dinamica.error.context",
027: getRequest().getContextPath());
028:
029: String date = StringUtil.formatDate(new java.util.Date(),
030: "yyyy-MM-dd HH:mm:ss");
031: String key = "dinamica.error.exception";
032: Throwable err = (Throwable) getRequest().getAttribute(key);
033: String errUri = (String) getRequest().getAttribute(
034: "dinamica.error.url");
035:
036: //error triggered by servlet distinct from Controller or maybe a filter?
037: if (err == null) {
038: err = (Throwable) getRequest().getAttribute(
039: "javax.servlet.error.exception");
040: getRequest().setAttribute("dinamica.error.description",
041: err.getMessage());
042: }
043:
044: // default log to STDERR
045: if (err != null)
046: System.err.println("[Exception@" + date + "] "
047: + err.getMessage() + " context: "
048: + getRequest().getContextPath() + " uri:" + errUri
049: + " referer:" + referer);
050:
051: try {
052: int rc = super .service(inputParams);
053:
054: // capture stack trace for exceptions
055: // raised by non-Dinamica Actions
056: String trace = (String) getRequest().getAttribute(
057: "dinamica.error.stacktrace");
058: if (trace == null) {
059: //this exception was not raised by dinamica.Controller
060: //use standard J2EE request attributes
061: if (err != null) {
062: //get stack trace
063: StringWriter s = new StringWriter();
064: err.printStackTrace(new PrintWriter(s));
065: getRequest().setAttribute(
066: "dinamica.error.stacktrace", s.toString());
067: }
068:
069: }
070:
071: //get email config
072: String enabled = getConfig().getConfigValue(
073: "//mail/enabled");
074:
075: if (enabled.equals("true")) {
076: String host = getConfig().getConfigValue("//mail/host");
077: String subject = getConfig().getConfigValue(
078: "//mail/subject");
079: String from = getConfig().getConfigValue("//mail/from");
080: String fromName = getConfig().getConfigValue(
081: "//mail/from-name");
082: ;
083: String to = getConfig().getConfigValue("//mail/to");
084: String body = getResource("body.txt");
085:
086: //build message
087: TemplateEngine t = new TemplateEngine(getContext(),
088: getRequest(), body);
089: t.replaceDefaultValues();
090: t.replaceLabels();
091: t.replaceRequestAttributes();
092: body = t.toString();
093:
094: //send email
095: SimpleMail s = new SimpleMail();
096: s.send(host, from, fromName, to, subject, body);
097:
098: }
099:
100: return rc;
101:
102: } catch (Throwable e) {
103: System.err.println("[WARNING@" + date
104: + "] ErrorHandler failed: " + e.getMessage());
105: throw e;
106: }
107:
108: }
109:
110: }
|