01: package net.sourceforge.jaxor.util;
02:
03: import java.io.*;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06: import java.util.List;
07:
08: /**
09: * This is the common superclass for all system level
10: * exceptions. This includes misconfigured servers,
11: * coding errors, etc.
12: *
13: */
14: public class SystemException extends RuntimeException {
15: private List _causes = new ArrayList();
16: private String _stackTrace;
17:
18: public SystemException() {
19: super ();
20: }
21:
22: public SystemException(String msg) {
23: super (msg);
24: }
25:
26: public SystemException(Throwable cause) {
27: this ("", cause);
28: }
29:
30: public SystemException(String msg, Throwable cause) {
31: super (msg);
32: _causes.add(cause);
33: }
34:
35: public SystemException(Throwable root, Throwable secondary) {
36: this ("", root);
37: add(secondary);
38: }
39:
40: public void add(Throwable secondary) {
41: _causes.add(secondary);
42: }
43:
44: private void getStackTraceAsString() {
45: if (_stackTrace != null) {
46: return;
47: }
48: StringWriter stringWriter = new StringWriter();
49: PrintWriter printer = new PrintWriter(stringWriter);
50: super .printStackTrace(printer);
51: for (Iterator iterator = _causes.iterator(); iterator.hasNext();) {
52: Throwable throwable = (Throwable) iterator.next();
53: printer.println("Cause: ");
54: throwable.printStackTrace(printer);
55: }
56: printer.flush();
57: printer.close();
58: _stackTrace = stringWriter.getBuffer().toString();
59: }
60:
61: public List getCauseList() {
62: return _causes;
63: }
64:
65: public void printStackTrace() {
66: printStackTrace(System.err);
67: }
68:
69: /**
70: * Prints the stack trace to the specified output stream, as well as the stack trace of
71: * any nested exceptions.
72: */
73: public void printStackTrace(PrintStream out) {
74: getStackTraceAsString();
75: try {
76: out.write(_stackTrace.getBytes());
77: } catch (IOException e) {
78: throw new SystemException(
79: "Failed to write stack trace to stream", e);
80: }
81: }
82:
83: /**
84: * Prints the stack trace to the specified output stream, as well as the stack trace of
85: * any nested exceptions.
86: */
87: public void printStackTrace(PrintWriter out) {
88: getStackTraceAsString();
89: out.write(_stackTrace);
90: }
91:
92: private void writeObject(ObjectOutputStream s) throws IOException {
93: getStackTraceAsString();
94: s.defaultWriteObject();
95: }
96: }
|