001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * Description: Utility for OpenUSS
004: * Copyright: Copyright (c) B. Lofi Dewanto
005: * Company: University of Muenster
006: * @author B. Lofi Dewanto
007: * @version 1.0
008: */package org.openuss.utility;
009:
010: import java.io.PrintWriter;
011:
012: /**
013: * Root exception for all exceptions available.
014: * This code was taken from the article at the Java World
015: * Java World Tip 91 by Terren Suydam (c)
016: * "Use Nested Exception in a Multitiered Environment"
017: *
018: * @author B. Lofi Dewanto
019: * @version 1.0
020: */
021: import java.io.StringWriter;
022:
023: public class NestingException extends Exception {
024: // Nested exception
025: private Throwable nestedException;
026:
027: // String representation of stack trace - not transient!
028: private String stackTraceString;
029:
030: /**
031: * java.lang.Exception constructors.
032: */
033: public NestingException() {
034: }
035:
036: /**
037: * java.lang.Exception constructors.
038: */
039: public NestingException(String msg) {
040: super (msg);
041: }
042:
043: /**
044: * Additional c'tors - nest the exceptions, storing the stack trace.
045: */
046: public NestingException(Throwable nestedException) {
047: this .nestedException = nestedException;
048: stackTraceString = generateStackTraceString(nestedException);
049: }
050:
051: /**
052: * Additional c'tors - nest the exceptions, storing the stack trace.
053: */
054: public NestingException(String msg, Throwable nestedException) {
055: this (msg);
056: this .nestedException = nestedException;
057: stackTraceString = generateStackTraceString(nestedException);
058: }
059:
060: /**
061: * Convert a stack trace to a String so it can be serialized.
062: */
063: static public String generateStackTraceString(Throwable t) {
064: StringWriter s = new StringWriter();
065: t.printStackTrace(new PrintWriter(s));
066:
067: return s.toString();
068: }
069:
070: /**
071: * Methods.
072: */
073: public Throwable getNestedException() {
074: return nestedException;
075: }
076:
077: /**
078: * Descend through linked-list of nesting exceptions, & output trace.
079: * Note that this displays the 'deepest' trace first.
080: */
081: public String getStackTraceString() {
082: // If there's no nested exception, there's no stackTrace
083: if (nestedException == null) {
084: return null;
085: }
086:
087: StringBuffer traceBuffer = new StringBuffer();
088:
089: if (nestedException instanceof NestingException) {
090: traceBuffer.append(((NestingException) nestedException)
091: .getStackTraceString());
092: traceBuffer.append("-------- nested by:\n");
093: }
094:
095: traceBuffer.append(stackTraceString);
096:
097: return traceBuffer.toString();
098: }
099:
100: /**
101: * Overrides Exception.getMessage().
102: */
103: public String getMessage() {
104: // superMsg will contain whatever String was passed into the
105: // constructor, and null otherwise.
106: String super Msg = super .getMessage();
107:
108: // if there's no nested exception, do like we would always do
109: if (getNestedException() == null) {
110: return super Msg;
111: }
112:
113: StringBuffer theMsg = new StringBuffer();
114:
115: // get the nested exception's message
116: String nestedMsg = getNestedException().getMessage();
117:
118: if (super Msg != null) {
119: theMsg.append(super Msg).append(": ").append(nestedMsg);
120: } else {
121: theMsg.append(nestedMsg);
122: }
123:
124: return theMsg.toString();
125: }
126:
127: /**
128: * Overrides Exception.toString().
129: */
130: public String toString() {
131: StringBuffer theMsg = new StringBuffer(super .toString());
132:
133: if (getNestedException() != null) {
134: theMsg.append("; \n\t---> nested ").append(
135: getNestedException());
136: }
137:
138: return theMsg.toString();
139: }
140: }
|