01: /*
02: * ExceptionBuffer.java March 2004
03: *
04: * Copyright (C) 2004, Niall Gallagher <niallg@users.sf.net>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General
16: * Public License along with this library; if not, write to the
17: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18: * Boston, MA 02111-1307 USA
19: */
20:
21: package simple.http.serve;
22:
23: import java.io.StringWriter;
24: import java.io.PrintWriter;
25:
26: /**
27: * The <code>ExceptionBuffer</code> captures a stack trace from
28: * a <code>Throwable</code>. This enables the cause of an error
29: * to be generated by a <code>Report</code> implementation. The
30: * <code>ErrorReport</code> implementation is currently using
31: * this to describe the cause of a service error.
32: *
33: * @author Niall Gallagher
34: */
35: class ExceptionBuffer extends StringWriter {
36:
37: /**
38: * This provides an adapter to capture the stack trace.
39: */
40: private PrintWriter out;
41:
42: /**
43: * This is the exception to acquire the stack trace from.
44: */
45: private Throwable cause;
46:
47: /**
48: * Constructor for the <code>ExceptionBuffer</code> object. The
49: * exception provided is used to generate a stack trace, which
50: * can be used to generate an error report for a service.
51: *
52: * @param cause the exception to acquire a stack trace from
53: */
54: public ExceptionBuffer(Throwable cause) {
55: this .out = new PrintWriter(this );
56: this .cause = cause;
57: }
58:
59: /**
60: * Generates the stace trace of the exception as a text string.
61: * If the stack trace cannot be generated of if there is a
62: * problem generating it then this will return an empty string.
63: *
64: * @return this returns the stack trace of the exception
65: */
66: public String getCause() {
67: if (getBuffer().length() > 0) {
68: return toString();
69: }
70: try {
71: cause.printStackTrace(out);
72: } catch (Exception e) {
73: return "";
74: }
75: return toString();
76: }
77: }
|