1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)MessagingException.java 1.17 07/05/04
39 */
40
41 package javax.mail;
42
43 import java.lang;
44
45 /**
46 * The base class for all exceptions thrown by the Messaging classes
47 *
48 * @author John Mani
49 * @author Bill Shannon
50 */
51
52 public class MessagingException extends Exception {
53
54 /**
55 * The next exception in the chain.
56 *
57 * @serial
58 */
59 private Exception next;
60
61 private static final long serialVersionUID = -7569192289819959253L;
62
63 /**
64 * Constructs a MessagingException with no detail message.
65 */
66 public MessagingException() {
67 super();
68 initCause(null); // prevent anyone else from setting it
69 }
70
71 /**
72 * Constructs a MessagingException with the specified detail message.
73 *
74 * @param s the detail message
75 */
76 public MessagingException(String s) {
77 super(s);
78 initCause(null); // prevent anyone else from setting it
79 }
80
81 /**
82 * Constructs a MessagingException with the specified
83 * Exception and detail message. The specified exception is chained
84 * to this exception.
85 *
86 * @param s the detail message
87 * @param e the embedded exception
88 * @see #getNextException
89 * @see #setNextException
90 * @see #getCause
91 */
92 public MessagingException(String s, Exception e) {
93 super(s);
94 next = e;
95 initCause(null); // prevent anyone else from setting it
96 }
97
98 /**
99 * 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.superToString());
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 }