001: /*
002: * $Id: SOAPExceptionImpl.java,v 1.3 2006/01/27 12:49:17 vj135062 Exp $
003: * $Revision: 1.3 $
004: * $Date: 2006/01/27 12:49:17 $
005: */
006:
007: /*
008: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
009: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010: *
011: * This code is free software; you can redistribute it and/or modify it
012: * under the terms of the GNU General Public License version 2 only, as
013: * published by the Free Software Foundation. Sun designates this
014: * particular file as subject to the "Classpath" exception as provided
015: * by Sun in the LICENSE file that accompanied this code.
016: *
017: * This code is distributed in the hope that it will be useful, but WITHOUT
018: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020: * version 2 for more details (a copy is included in the LICENSE file that
021: * accompanied this code).
022: *
023: * You should have received a copy of the GNU General Public License version
024: * 2 along with this work; if not, write to the Free Software Foundation,
025: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026: *
027: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028: * CA 95054 USA or visit www.sun.com if you need additional information or
029: * have any questions.
030: */
031: package com.sun.xml.internal.messaging.saaj;
032:
033: import java.io.PrintStream;
034: import java.io.PrintWriter;
035:
036: import javax.xml.soap.SOAPException;
037:
038: /**
039: * An exception that signals that a SOAP exception has occurred. A
040: * <code>SOAPExceptionImpl</code> object may contain a <code>String</code>
041: * that gives the reason for the exception, an embedded
042: * <code>Throwable</code> object, or both. This class provides methods
043: * for retrieving reason messages and for retrieving the embedded
044: * <code>Throwable</code> object.
045: *
046: * <P> Typical reasons for throwing a <code>SOAPExceptionImpl</code>
047: * object are problems such as difficulty setting a header, not being
048: * able to send a message, and not being able to get a connection with
049: * the provider. Reasons for embedding a <code>Throwable</code>
050: * object include problems such as input/output errors or a parsing
051: * problem, such as an error in parsing a header.
052: */
053: public class SOAPExceptionImpl extends SOAPException {
054: private Throwable cause;
055:
056: /**
057: * Constructs a <code>SOAPExceptionImpl</code> object with no
058: * reason or embedded <code>Throwable</code> object.
059: */
060: public SOAPExceptionImpl() {
061: super ();
062: this .cause = null;
063: }
064:
065: /**
066: * Constructs a <code>SOAPExceptionImpl</code> object with the given
067: * <code>String</code> as the reason for the exception being thrown.
068: *
069: * @param reason a description of what caused the exception
070: */
071: public SOAPExceptionImpl(String reason) {
072: super (reason);
073: this .cause = null;
074: }
075:
076: /**
077: * Constructs a <code>SOAPExceptionImpl</code> object with the given
078: * <code>String</code> as the reason for the exception being thrown
079: * and the given <code>Throwable</code> object as an embedded
080: * exception.
081: *
082: * @param reason a description of what caused the exception
083: * @param cause a <code>Throwable</code> object that is to
084: * be embedded in this <code>SOAPExceptionImpl</code> object
085: */
086: public SOAPExceptionImpl(String reason, Throwable cause) {
087: super (reason);
088: initCause(cause);
089: }
090:
091: /**
092: * Constructs a <code>SOAPExceptionImpl</code> object initialized
093: * with the given <code>Throwable</code> object.
094: */
095: public SOAPExceptionImpl(Throwable cause) {
096: super (cause.toString());
097: initCause(cause);
098: }
099:
100: /**
101: * Returns the detail message for this <code>SOAPExceptionImpl</code>
102: * object.
103: * <P>
104: * If there is an embedded <code>Throwable</code> object, and if the
105: * <code>SOAPExceptionImpl</code> object has no detail message of its
106: * own, this method will return the detail message from the embedded
107: * <code>Throwable</code> object.
108: *
109: * @return the error or warning message for this
110: * <code>SOAPExceptionImpl</code> or, if it has none, the
111: * message of the embedded <code>Throwable</code> object,
112: * if there is one
113: */
114: public String getMessage() {
115: String message = super .getMessage();
116: if (message == null && cause != null) {
117: return cause.getMessage();
118: } else {
119: return message;
120: }
121: }
122:
123: /**
124: * Returns the <code>Throwable</code> object embedded in this
125: * <code>SOAPExceptionImpl</code> if there is one. Otherwise, this method
126: * returns <code>null</code>.
127: *
128: * @return the embedded <code>Throwable</code> object or <code>null</code>
129: * if there is none
130: */
131:
132: public Throwable getCause() {
133: return cause;
134: }
135:
136: /**
137: * Initializes the <code>cause</code> field of this <code>SOAPExceptionImpl</code>
138: * object with the given <code>Throwable</code> object.
139: * <P>
140: * This method can be called at most once. It is generally called from
141: * within the constructor or immediately after the constructor has
142: * returned a new <code>SOAPExceptionImpl</code> object.
143: * If this <code>SOAPExceptionImpl</code> object was created with the
144: * constructor {@link #SOAPExceptionImpl(Throwable)} or
145: * {@link #SOAPExceptionImpl(String,Throwable)}, meaning that its
146: * <code>cause</code> field already has a value, this method cannot be
147: * called even once.
148: *
149: * @param cause the <code>Throwable</code> object that caused this
150: * <code>SOAPExceptionImpl</code> object to be thrown. The value of this
151: * parameter is saved for later retrieval by the
152: * {@link #getCause()} method. A <tt>null</tt> value is
153: * permitted and indicates that the cause is nonexistent or
154: * unknown.
155: * @return a reference to this <code>SOAPExceptionImpl</code> instance
156: * @throws IllegalArgumentException if <code>cause</code> is this
157: * <code>Throwable</code> object. (A <code>Throwable</code> object
158: * cannot be its own cause.)
159: * @throws IllegalStateException if this <code>SOAPExceptionImpl</code> object
160: * was created with {@link #SOAPExceptionImpl(Throwable)} or
161: * {@link #SOAPExceptionImpl(String,Throwable)}, or this
162: * method has already been called on this <code>SOAPExceptionImpl</code>
163: * object
164: */
165: public synchronized Throwable initCause(Throwable cause) {
166: if (this .cause != null) {
167: throw new IllegalStateException("Can't override cause");
168: }
169: if (cause == this ) {
170: throw new IllegalArgumentException(
171: "Self-causation not permitted");
172: }
173: this .cause = cause;
174:
175: return this ;
176: }
177:
178: public void printStackTrace() {
179: super .printStackTrace();
180: if (cause != null) {
181: System.err.println("\nCAUSE:\n");
182: cause.printStackTrace();
183: }
184: }
185:
186: public void printStackTrace(PrintStream s) {
187: super .printStackTrace(s);
188: if (cause != null) {
189: s.println("\nCAUSE:\n");
190: cause.printStackTrace(s);
191: }
192: }
193:
194: public void printStackTrace(PrintWriter s) {
195: super .printStackTrace(s);
196: if (cause != null) {
197: s.println("\nCAUSE:\n");
198: cause.printStackTrace(s);
199: }
200: }
201: }
|