1 package dinamica; 2 3 import java.io.PrintWriter ; 4 import java.io.StringWriter ; 5 6 16 public class ErrorHandler extends GenericTransaction 17 { 18 19 22 public int service(Recordset inputParams) throws Throwable  23 { 24 25 String referer = getRequest().getHeader("Referer"); 27 getRequest().setAttribute("dinamica.error.referer", referer); 28 getRequest().setAttribute("dinamica.error.context", getRequest().getContextPath()); 29 30 String date = StringUtil.formatDate(new java.util.Date (), "yyyy-MM-dd HH:mm:ss"); 31 String key = "javax.servlet.error.exception"; 32 Throwable err = (Throwable )getRequest().getAttribute(key); 33 String errUri = (String )getRequest().getAttribute("javax.servlet.error.request_uri"); 34 35 if (err!=null) 37 System.err.println("[Exception@" + date + "] " + err.getMessage() 38 + " context: " + getRequest().getContextPath() 39 + " uri:" + errUri 40 + " referer:" + referer); 41 42 try 43 { 44 int rc = super.service(inputParams); 45 46 String trace = (String )getRequest().getAttribute("dinamica.error.stacktrace"); 49 if (trace==null) 50 { 51 if (err!=null) 54 { 55 StringWriter s = new StringWriter (); 57 PrintWriter errMsg = new PrintWriter (s); 58 err.printStackTrace(errMsg); 59 trace = s.toString(); 60 getRequest().setAttribute("dinamica.error.stacktrace", trace); 61 } 62 63 } 64 65 66 String enabled = getConfig().getConfigValue("//mail/enabled"); 68 69 if (enabled.equals("true")) 70 { 71 String host = getConfig().getConfigValue("//mail/host"); 72 String subject = getConfig().getConfigValue("//mail/subject"); 73 String from = getConfig().getConfigValue("//mail/from"); 74 String fromName = getConfig().getConfigValue("//mail/from-name");; 75 String to = getConfig().getConfigValue("//mail/to"); 76 String body = getResource("body.txt"); 77 78 TemplateEngine t = new TemplateEngine(getContext(),getRequest(), body); 80 t.replaceDefaultValues(); 81 t.replaceLabels(); 82 t.replaceRequestAttributes(); 83 body = t.toString(); 84 85 SimpleMail s = new SimpleMail(); 87 s.send(host,from,fromName,to,subject,body); 88 89 } 90 91 return rc; 92 93 } 94 catch (Throwable e) 95 { 96 System.err.println("[WARNING@" + date + "] ErrorHandler failed: " + e.getMessage()); 97 throw e; 98 } 99 100 } 101 102 } 103
| Popular Tags
|