001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: ChainedError.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.util;
025:
026: import java.io.PrintStream;
027: import java.io.PrintWriter;
028:
029: /**
030: * Error used as a base for creating an error that has a chain of
031: * exceptions that lead to the derived error. Very useful for interfaces
032: * where the implementation exception is not known.
033: */
034: public class ChainedError extends Error implements ChainedThrowable {
035: private Throwable cause;
036:
037: /**
038: * Construct an error without a specified cause.
039: *
040: * @param msg The message associated with the exception.
041: */
042: public ChainedError(String msg) {
043: super (msg);
044: cause = null;
045: }
046:
047: /**
048: * Construct an exception with an associated causing exception.
049: *
050: * @param msg The message associated with the exception.
051: * @param cause The error or exception that cause this
052: * exception.
053: */
054: public ChainedError(String msg, Throwable cause) {
055: super (msg);
056: this .cause = cause;
057: }
058:
059: /**
060: * Construct an exception from a causing exception.
061: *
062: * @param cause The error or exception that cause this
063: * exception. The message will be take be this object's
064: * messasge.
065: */
066: public ChainedError(Throwable cause) {
067: super (ChainedThrowableUtil.makeMessage(cause));
068: this .cause = cause;
069: }
070:
071: /**
072: * Return the message associated with this exception. If causes
073: * are included, they will be appended to the message.
074: */
075: public String getMessage() {
076: return ChainedThrowableUtil
077: .getMessage(this , super .getMessage());
078: }
079:
080: /**
081: * Get the causing exception associated with this exception.
082: * @return The causing exception or null if no cause is specified.
083: */
084: public Throwable getCause() {
085: return cause;
086: }
087:
088: /**
089: * Prints this ChainedError and its backtrace, and the causes
090: * and their stack traces to the standard error stream.
091: */
092: public void printStackTrace() {
093: super .printStackTrace();
094: ChainedThrowableUtil.printCauseTrace(this );
095: }
096:
097: /**
098: * Prints this ChainedError and its backtrace, and the causes
099: * and their stack traces to the e specified print stream.
100: */
101: public void printStackTrace(PrintStream s) {
102: super .printStackTrace(s);
103: ChainedThrowableUtil.printCauseTrace(this , s);
104: }
105:
106: /**
107: * Prints this ChainedError and its backtrace, and the causes
108: * and their stack traces to the e specified print writer.
109: */
110: public void printStackTrace(PrintWriter s) {
111: super.printStackTrace(s);
112: ChainedThrowableUtil.printCauseTrace(this, s);
113: }
114: }
|