001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * @(#)MessagingException.java 1.17 07/05/04
039: */
040:
041: package javax.mail;
042:
043: import java.lang.*;
044:
045: /**
046: * The base class for all exceptions thrown by the Messaging classes
047: *
048: * @author John Mani
049: * @author Bill Shannon
050: */
051:
052: public class MessagingException extends Exception {
053:
054: /**
055: * The next exception in the chain.
056: *
057: * @serial
058: */
059: private Exception next;
060:
061: private static final long serialVersionUID = -7569192289819959253L;
062:
063: /**
064: * Constructs a MessagingException with no detail message.
065: */
066: public MessagingException() {
067: super ();
068: initCause(null); // prevent anyone else from setting it
069: }
070:
071: /**
072: * Constructs a MessagingException with the specified detail message.
073: *
074: * @param s the detail message
075: */
076: public MessagingException(String s) {
077: super (s);
078: initCause(null); // prevent anyone else from setting it
079: }
080:
081: /**
082: * Constructs a MessagingException with the specified
083: * Exception and detail message. The specified exception is chained
084: * to this exception.
085: *
086: * @param s the detail message
087: * @param e the embedded exception
088: * @see #getNextException
089: * @see #setNextException
090: * @see #getCause
091: */
092: public MessagingException(String s, Exception e) {
093: super (s);
094: next = e;
095: initCause(null); // prevent anyone else from setting it
096: }
097:
098: /**
099: * Get the next exception chained to this one. If the
100: * next exception is a MessagingException, the chain
101: * may extend further.
102: *
103: * @return next Exception, null if none.
104: */
105: public synchronized Exception getNextException() {
106: return next;
107: }
108:
109: /**
110: * Overrides the <code>getCause</code> method of <code>Throwable</code>
111: * to return the next exception in the chain of nested exceptions.
112: *
113: * @return next Exception, null if none.
114: */
115: public synchronized Throwable getCause() {
116: return next;
117: }
118:
119: /**
120: * Add an exception to the end of the chain. If the end
121: * is <strong>not</strong> a MessagingException, this
122: * exception cannot be added to the end.
123: *
124: * @param ex the new end of the Exception chain
125: * @return <code>true</code> if this Exception
126: * was added, <code>false</code> otherwise.
127: */
128: public synchronized boolean setNextException(Exception ex) {
129: Exception theEnd = this ;
130: while (theEnd instanceof MessagingException
131: && ((MessagingException) theEnd).next != null) {
132: theEnd = ((MessagingException) theEnd).next;
133: }
134: // If the end is a MessagingException, we can add this
135: // exception to the chain.
136: if (theEnd instanceof MessagingException) {
137: ((MessagingException) theEnd).next = ex;
138: return true;
139: } else
140: return false;
141: }
142:
143: /**
144: * Override toString method to provide information on
145: * nested exceptions.
146: */
147: public synchronized String toString() {
148: String s = super .toString();
149: Exception n = next;
150: if (n == null)
151: return s;
152: StringBuffer sb = new StringBuffer(s == null ? "" : s);
153: while (n != null) {
154: sb.append(";\n nested exception is:\n\t");
155: if (n instanceof MessagingException) {
156: MessagingException mex = (MessagingException) n;
157: sb.append(mex.super ToString());
158: n = mex.next;
159: } else {
160: sb.append(n.toString());
161: n = null;
162: }
163: }
164: return sb.toString();
165: }
166:
167: /**
168: * Return the "toString" information for this exception,
169: * without any information on nested exceptions.
170: */
171: private final String superToString() {
172: return super.toString();
173: }
174: }
|