001: package com.rimfaxe.thread;
002:
003: import java.io.StringWriter;
004: import java.io.PrintWriter;
005: import java.util.HashMap;
006: import java.io.OutputStream;
007: import java.io.PrintStream;
008:
009: /** Default base class for all exceptions.
010: */
011:
012: public class ReportingException extends Exception {
013: /** The nested exception */
014: private ReportingException mNestedException;
015:
016: /** The name value pair Hashmap */
017: private HashMap mHashMap = null; // name value pair hashmap
018:
019: /** Constructs a ReportingException with message of the value passed through
020: * the constructor.
021: * @aMsg The msg string passed parameter is the message
022: * assigned to this ReportingException.
023: */
024:
025: public ReportingException(String aMessage) {
026: super (aMessage);
027: }
028:
029: /** the put method inserts name value pairs into the given exception. These values
030: * should be passed parameters within the method and also other values of interest.
031: * This method nests ReportingExceptions and stores other Exception values as their stack trace.
032: * @param aName is a name String
033: * @param aValue is a value String
034: */
035:
036: public synchronized void put(String aName, Object aValue) {
037: if (aValue instanceof ReportingException)
038: mNestedException = (ReportingException) aValue;
039: else if (aValue instanceof Exception) {
040: mHashMap.put(aName, getStackTraceOf((Exception) aValue));
041: } else
042: mHashMap.put(aName, aValue);
043: }
044:
045: /** the printStackTrace method outputs the stack trace to System.out
046: * this overrides the method printStackTrace from Exception.
047: */
048:
049: public void printStackTrace() {
050: //printStackTrace((OutputStream)System.out);
051: }
052:
053: /** the printStackTrace method outputs the stack trace to aOutputStream
054: * this overrides the method printStackTrace from Exception.
055: * @param aKey string.
056: */
057:
058: public void printStackTrace(OutputStream aOutputStream) {
059: PrintStream lPrintStream = new PrintStream(aOutputStream);
060: lPrintStream.print(getStackTrace());
061: }
062:
063: /** the getTrace method returns a String value containing
064: * the stack trace for this exception. This does not use getStackTrace
065: * which is now implemented in Java 1.4
066: * @return String containing stack trace.
067: */
068:
069: public String getTrace() {
070: StringBuffer lBuffer = new StringBuffer();
071:
072: lBuffer.append("\n\n------------------------\n");
073: lBuffer.append(this .getMessage());
074: lBuffer.append("\n------------------------\n\n");
075: if (mNestedException == null) { // this is the end of the tree -- print the stack trace information
076: lBuffer.append(getStackTraceOf(this ));
077: } else {
078:
079: if (!mHashMap.isEmpty()) {
080: lBuffer.append("\n-- Name Value Pairs --\n");
081: lBuffer.append(mHashMap.toString());
082: } else
083: lBuffer.append("\n-------------------\n");
084: lBuffer.append(mNestedException.getStackTrace()); // recursive call to nested exception
085: }
086:
087: return lBuffer.toString();
088: }
089:
090: /** Gets the stored nested exception.
091: * This method is very useful to the ExceptionHandler which needs to know the entire stack of exceptions.
092: * @return nested exception */
093: public Throwable getNestedException() {
094: return mNestedException;
095: }
096:
097: /** Utility method to get the stack trace string of the specified throwable. */
098: private String getStackTraceOf(Throwable aThrowable) {
099: if (aThrowable == null)
100: return null;
101:
102: StringWriter s = new StringWriter();
103: aThrowable.printStackTrace(new PrintWriter(s));
104: return s.toString();
105: }
106: }
|