001: /*
002: * @(#)MessagingException.java 1.10 02/06/13
003: */
004:
005: /*
006: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
007: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
008: *
009: * This code is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU General Public License version 2 only, as
011: * published by the Free Software Foundation. Sun designates this
012: * particular file as subject to the "Classpath" exception as provided
013: * by Sun in the LICENSE file that accompanied this code.
014: *
015: * This code is distributed in the hope that it will be useful, but WITHOUT
016: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
017: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
018: * version 2 for more details (a copy is included in the LICENSE file that
019: * accompanied this code).
020: *
021: * You should have received a copy of the GNU General Public License version
022: * 2 along with this work; if not, write to the Free Software Foundation,
023: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
024: *
025: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
026: * CA 95054 USA or visit www.sun.com if you need additional information or
027: * have any questions.
028: */
029:
030: package com.sun.xml.internal.messaging.saaj.packaging.mime;
031:
032: /**
033: * The base class for all exceptions thrown by the Messaging classes
034: *
035: * @author John Mani
036: * @author Bill Shannon
037: */
038:
039: public class MessagingException extends Exception {
040:
041: /**
042: * The next exception in the chain.
043: *
044: * @serial
045: */
046: private Exception next;
047:
048: /**
049: * Constructs a MessagingException with no detail message.
050: */
051: public MessagingException() {
052: super ();
053: }
054:
055: /**
056: * Constructs a MessagingException with the specified detail message.
057: * @param s the detail message
058: */
059: public MessagingException(String s) {
060: super (s);
061: }
062:
063: /**
064: * Constructs a MessagingException with the specified
065: * Exception and detail message. The specified exception is chained
066: * to this exception.
067: * @param s the detail message
068: * @param e the embedded exception
069: * @see #getNextException
070: * @see #setNextException
071: */
072: public MessagingException(String s, Exception e) {
073: super (s);
074: next = e;
075: }
076:
077: /**
078: * Get the next exception chained to this one. If the
079: * next exception is a MessagingException, the chain
080: * may extend further.
081: *
082: * @return next Exception, null if none.
083: */
084: public Exception getNextException() {
085: return next;
086: }
087:
088: /**
089: * Add an exception to the end of the chain. If the end
090: * is <strong>not</strong> a MessagingException, this
091: * exception cannot be added to the end.
092: *
093: * @param ex the new end of the Exception chain
094: * @return <code>true</code> if the this Exception
095: * was added, <code>false</code> otherwise.
096: */
097: public synchronized boolean setNextException(Exception ex) {
098: Exception theEnd = this ;
099: while (theEnd instanceof MessagingException
100: && ((MessagingException) theEnd).next != null) {
101: theEnd = ((MessagingException) theEnd).next;
102: }
103: // If the end is a MessagingException, we can add this
104: // exception to the chain.
105: if (theEnd instanceof MessagingException) {
106: ((MessagingException) theEnd).next = ex;
107: return true;
108: } else
109: return false;
110: }
111:
112: /**
113: * Produce the message, include the message from the nested
114: * exception if there is one.
115: */
116: public String getMessage() {
117: if (next == null)
118: return super .getMessage();
119: Exception n = next;
120: String s = super .getMessage();
121: StringBuffer sb = new StringBuffer(s == null ? "" : s);
122: while (n != null) {
123: sb.append(";\n nested exception is:\n\t");
124: if (n instanceof MessagingException) {
125: MessagingException mex = (MessagingException) n;
126: sb.append(n.getClass().toString());
127: String msg = mex.getSuperMessage();
128: if (msg != null) {
129: sb.append(": ");
130: sb.append(msg);
131: }
132: n = mex.next;
133: } else {
134: sb.append(n.toString());
135: n = null;
136: }
137: }
138: return sb.toString();
139: }
140:
141: private String getSuperMessage() {
142: return super.getMessage();
143: }
144: }
|