001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.exceptionprocessor.util;
021:
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.text.MessageFormat;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: /**
030: * @author jh
031: *
032: * To change the template for this generated type comment go to Window -
033: * Preferences - Java - Code Generation - Code and Comments
034: */
035: //TODO: move me into a cool package
036: public class TextCreatorVisitor implements ExceptionDataValueVisitor {
037: private String text;
038: private String subject;
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see de.schlund.jmsexceptionhandler.rmiobj.ExceptionDataValueVisitor#visit(de.schlund.jmsexceptionhandler.rmiobj.ExceptionDataValue)
044: */
045: public void visit(ExceptionDataValue data) {
046: String sessionid_info = MessageFormat.format(
047: "[SessionId: {0}]",
048: new Object[] { data.getSessionid() });
049:
050: String url_info = MessageFormat.format("{0}://{1}:{2}{3}"
051: + (data.getQuery() != null ? "?{4}" : ""),
052: new Object[] { data.getScheme(), data.getServername(),
053: "" + data.getPort(), data.getUri(),
054: data.getQuery() });
055:
056: StringBuffer requestparam_info = new StringBuffer();
057: HashMap<String, String> map = data.getRequestParams();
058: if (map == null) {
059: requestparam_info.append("No parameters");
060: } else {
061: for (Iterator<String> iter = map.keySet().iterator(); iter
062: .hasNext();) {
063: String key = iter.next();
064: String value = map.get(key);
065: requestparam_info.append(key + " = " + value + "\n");
066: }
067: }
068:
069: StringBuffer laststep_info = new StringBuffer();
070: List<String> steps = data.getLastSteps();
071: if (steps == null) {
072: laststep_info.append("No last step info");
073: } else {
074: for (Iterator<String> iter = steps.iterator(); iter
075: .hasNext();) {
076: String value = iter.next();
077: laststep_info.append(value + "\n");
078: }
079: }
080:
081: StringBuffer sessiondata_info = new StringBuffer();
082: HashMap<String, String> sessmap = data
083: .getSessionKeysAndValues();
084: if (sessmap == null) {
085: sessiondata_info.append("No session keys and values");
086: } else {
087: for (Iterator<String> iter = sessmap.keySet().iterator(); iter
088: .hasNext();) {
089: String key = iter.next();
090: String value = sessmap.get(key);
091: sessiondata_info.append("Key = " + key + "\n");
092: sessiondata_info.append("Value = " + value + "\n");
093: sessiondata_info
094: .append("------------------------------------------------------\n");
095: }
096: }
097:
098: StringWriter strwriter = new StringWriter();
099: PrintWriter p = new PrintWriter(strwriter);
100: data.getThrowable().printStackTrace(p);
101: p.flush();
102:
103: text = sessionid_info
104: + "\n\n\n"
105: + url_info
106: + "\n\n\n"
107: + "Parameter: \n"
108: + requestparam_info.toString()
109: + "\n\n\n"
110: + "==== Last steps before error occured: ================\n"
111: + laststep_info.toString()
112: + "\n\n\n"
113: + "==== Session keys and values: ========================\n"
114: + sessiondata_info.toString()
115: + "\n\n\n"
116: + "==== Stacktrace: =====================================\n"
117: + strwriter.getBuffer().toString();
118:
119: String msg = data.getThrowable().getMessage();
120: if (msg == null) {
121: StackTraceElement[] strace = data.getThrowable()
122: .getStackTrace();
123: if (strace.length > 0) {
124: msg = strace[0].toString().trim();
125: } else {
126: msg = "No stacktrace available";
127: }
128: }
129:
130: Object[] args = new Object[] {
131: data.getServername(),
132: data.getServlet().startsWith("/") ? data.getServlet()
133: .substring(1, data.getServlet().length())
134: : data.getServlet(), data.getPage(),
135: data.getThrowable().getClass().getName(), msg };
136:
137: subject = MessageFormat.format("{0}|{1}|{2}|{3}:{4}", args);
138:
139: data.setTextSubjectRepresentation(subject);
140: data.setTextBodyRepresentation(text);
141: }
142:
143: /* (non-Javadoc)
144: * @see de.schlund.jmsexceptionhandler.tokenbucket.ReportDataValueVisitor#visit(de.schlund.jmsexceptionhandler.tokenbucket.ReportDataValue)
145: */
146: /*public void visit(ReportDataValue rdata) {
147: subject = "JBoss Report: "+rdata.getThrowableType()+"("+rdata.getDismissedCount()+")";
148: StringBuffer sb = new StringBuffer();
149: sb.append("Stacktrace: \n");
150: StackTraceElement[] strace = rdata.getStackTrace();
151: if(strace == null) {
152: sb.append("Not available.");
153: } else {
154: for(int i=0; i<strace.length; i++) {
155: sb.append(strace[i].toString()+"\n");
156: }
157: }
158: text = rdata.getThrowableType()+"repeated "+rdata.getDismissedCount()+" times" +"\n" + sb.toString();
159: }*/
160:
161: }
|