001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.mail;
018:
019: import java.io.PrintStream;
020: import java.io.PrintWriter;
021:
022: /**
023: * Exception thrown when a checked error occurs in commons-email.
024: * <p>
025: * Supports nesting, emulating JDK 1.4 behavior if necessary.
026: * <p>
027: * Adapted from {@link org.apache.commons.collections.FunctorException}.
028: *
029: * @author jakarta-commons
030: * @since 1.0
031: * @version $Id: EmailException.java 279313 2005-09-07 12:41:58Z henning $
032: */
033: public class EmailException extends Exception {
034: /** Serializable version identifier */
035: static final long serialVersionUID = 5550674499282474616L;
036:
037: /**
038: * Does JDK support nested exceptions?
039: */
040: private static final boolean JDK_SUPPORTS_NESTED;
041:
042: static {
043: boolean flag = false;
044:
045: try {
046: Throwable.class.getDeclaredMethod("getCause", new Class[0]);
047: flag = true;
048: } catch (NoSuchMethodException ex) {
049: flag = false;
050: }
051:
052: JDK_SUPPORTS_NESTED = flag;
053: }
054:
055: /**
056: * Root cause of the exception
057: */
058: private final Throwable rootCause;
059:
060: /**
061: * Constructs a new <code>EmailException</code> with no
062: * detail message.
063: */
064: public EmailException() {
065: super ();
066: this .rootCause = null;
067: }
068:
069: /**
070: * Constructs a new <code>EmailException</code> with specified
071: * detail message.
072: *
073: * @param msg the error message.
074: */
075: public EmailException(String msg) {
076: super (msg);
077: this .rootCause = null;
078: }
079:
080: /**
081: * Constructs a new <code>EmailException</code> with specified
082: * nested <code>Throwable</code> root cause.
083: *
084: * @param rootCause the exception or error that caused this exception
085: * to be thrown.
086: */
087: public EmailException(Throwable rootCause) {
088: super (((rootCause == null) ? null : rootCause.getMessage()));
089: this .rootCause = rootCause;
090: }
091:
092: /**
093: * Constructs a new <code>EmailException</code> with specified
094: * detail message and nested <code>Throwable</code> root cause.
095: *
096: * @param msg the error message.
097: * @param rootCause the exception or error that caused this exception
098: * to be thrown.
099: */
100: public EmailException(String msg, Throwable rootCause) {
101: super (msg);
102: this .rootCause = rootCause;
103: }
104:
105: /**
106: * Gets the cause of this throwable.
107: *
108: * @return the cause of this throwable, or <code>null</code>
109: */
110: public Throwable getCause() {
111: return rootCause;
112: }
113:
114: /**
115: * Prints the stack trace of this exception to the standard error stream.
116: */
117: public void printStackTrace() {
118: printStackTrace(System.err);
119: }
120:
121: /**
122: * Prints the stack trace of this exception to the specified stream.
123: *
124: * @param out the <code>PrintStream</code> to use for output
125: */
126: public void printStackTrace(PrintStream out) {
127: synchronized (out) {
128: PrintWriter pw = new PrintWriter(out, false);
129: printStackTrace(pw);
130:
131: // Flush the PrintWriter before it's GC'ed.
132: pw.flush();
133: }
134: }
135:
136: /**
137: * Prints the stack trace of this exception to the specified writer.
138: *
139: * @param out the <code>PrintWriter</code> to use for output
140: */
141: public void printStackTrace(PrintWriter out) {
142: synchronized (out) {
143: super .printStackTrace(out);
144:
145: if ((rootCause != null) && (JDK_SUPPORTS_NESTED == false)) {
146: out.print("Caused by: ");
147: rootCause.printStackTrace(out);
148: }
149: }
150: }
151: }
|