001 /*
002 * $Id: SOAPException.java,v 1.5 2004/04/02 01:24:18 ofung Exp $
003 * $Revision: 1.5 $
004 * $Date: 2004/04/02 01:24:18 $
005 */
006
007 /*
008 * Copyright 2005-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 javax.xml.soap;
032
033 /**
034 * An exception that signals that a SOAP exception has occurred. A
035 * <code>SOAPException</code> object may contain a <code>String</code>
036 * that gives the reason for the exception, an embedded
037 * <code>Throwable</code> object, or both. This class provides methods
038 * for retrieving reason messages and for retrieving the embedded
039 * <code>Throwable</code> object.
040 *
041 * <P> Typical reasons for throwing a <code>SOAPException</code>
042 * object are problems such as difficulty setting a header, not being
043 * able to send a message, and not being able to get a connection with
044 * the provider. Reasons for embedding a <code>Throwable</code>
045 * object include problems such as input/output errors or a parsing
046 * problem, such as an error in parsing a header.
047 */
048 public class SOAPException extends Exception {
049 private Throwable cause;
050
051 /**
052 * Constructs a <code>SOAPException</code> object with no
053 * reason or embedded <code>Throwable</code> object.
054 */
055 public SOAPException() {
056 super ();
057 this .cause = null;
058 }
059
060 /**
061 * Constructs a <code>SOAPException</code> object with the given
062 * <code>String</code> as the reason for the exception being thrown.
063 *
064 * @param reason a description of what caused the exception
065 */
066 public SOAPException(String reason) {
067 super (reason);
068 this .cause = null;
069 }
070
071 /**
072 * Constructs a <code>SOAPException</code> object with the given
073 * <code>String</code> as the reason for the exception being thrown
074 * and the given <code>Throwable</code> object as an embedded
075 * exception.
076 *
077 * @param reason a description of what caused the exception
078 * @param cause a <code>Throwable</code> object that is to
079 * be embedded in this <code>SOAPException</code> object
080 */
081 public SOAPException(String reason, Throwable cause) {
082 super (reason);
083 initCause(cause);
084 }
085
086 /**
087 * Constructs a <code>SOAPException</code> object initialized
088 * with the given <code>Throwable</code> object.
089 */
090 public SOAPException(Throwable cause) {
091 super (cause.toString());
092 initCause(cause);
093 }
094
095 /**
096 * Returns the detail message for this <code>SOAPException</code>
097 * object.
098 * <P>
099 * If there is an embedded <code>Throwable</code> object, and if the
100 * <code>SOAPException</code> object has no detail message of its
101 * own, this method will return the detail message from the embedded
102 * <code>Throwable</code> object.
103 *
104 * @return the error or warning message for this
105 * <code>SOAPException</code> or, if it has none, the
106 * message of the embedded <code>Throwable</code> object,
107 * if there is one
108 */
109 public String getMessage() {
110 String message = super .getMessage();
111 if (message == null && cause != null) {
112 return cause.getMessage();
113 } else {
114 return message;
115 }
116 }
117
118 /**
119 * Returns the <code>Throwable</code> object embedded in this
120 * <code>SOAPException</code> if there is one. Otherwise, this method
121 * returns <code>null</code>.
122 *
123 * @return the embedded <code>Throwable</code> object or <code>null</code>
124 * if there is none
125 */
126
127 public Throwable getCause() {
128 return cause;
129 }
130
131 /**
132 * Initializes the <code>cause</code> field of this <code>SOAPException</code>
133 * object with the given <code>Throwable</code> object.
134 * <P>
135 * This method can be called at most once. It is generally called from
136 * within the constructor or immediately after the constructor has
137 * returned a new <code>SOAPException</code> object.
138 * If this <code>SOAPException</code> object was created with the
139 * constructor {@link #SOAPException(Throwable)} or
140 * {@link #SOAPException(String,Throwable)}, meaning that its
141 * <code>cause</code> field already has a value, this method cannot be
142 * called even once.
143 *
144 * @param cause the <code>Throwable</code> object that caused this
145 * <code>SOAPException</code> object to be thrown. The value of this
146 * parameter is saved for later retrieval by the
147 * {@link #getCause()} method. A <tt>null</tt> value is
148 * permitted and indicates that the cause is nonexistent or
149 * unknown.
150 * @return a reference to this <code>SOAPException</code> instance
151 * @throws IllegalArgumentException if <code>cause</code> is this
152 * <code>Throwable</code> object. (A <code>Throwable</code> object
153 * cannot be its own cause.)
154 * @throws IllegalStateException if the cause for this <code>SOAPException</code> object
155 * has already been initialized
156 */
157 public synchronized Throwable initCause(Throwable cause) {
158 if (this .cause != null) {
159 throw new IllegalStateException("Can't override cause");
160 }
161 if (cause == this ) {
162 throw new IllegalArgumentException(
163 "Self-causation not permitted");
164 }
165 this.cause = cause;
166
167 return this;
168 }
169 }
|