1 /*
2 * $Id: SOAPException.java,v 1.5 2004/04/02 01:24:18 ofung Exp $
3 * $Revision: 1.5 $
4 * $Date: 2004/04/02 01:24:18 $
5 */
6
7 /*
8 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Sun designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Sun in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
28 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * have any questions.
30 */
31 package javax.xml.soap;
32
33 /**
34 * An exception that signals that a SOAP exception has occurred. A
35 * <code>SOAPException</code> object may contain a <code>String</code>
36 * that gives the reason for the exception, an embedded
37 * <code>Throwable</code> object, or both. This class provides methods
38 * for retrieving reason messages and for retrieving the embedded
39 * <code>Throwable</code> object.
40 *
41 * <P> Typical reasons for throwing a <code>SOAPException</code>
42 * object are problems such as difficulty setting a header, not being
43 * able to send a message, and not being able to get a connection with
44 * the provider. Reasons for embedding a <code>Throwable</code>
45 * object include problems such as input/output errors or a parsing
46 * problem, such as an error in parsing a header.
47 */
48 public class SOAPException extends Exception {
49 private Throwable cause;
50
51 /**
52 * Constructs a <code>SOAPException</code> object with no
53 * reason or embedded <code>Throwable</code> object.
54 */
55 public SOAPException() {
56 super();
57 this.cause = null;
58 }
59
60 /**
61 * Constructs a <code>SOAPException</code> object with the given
62 * <code>String</code> as the reason for the exception being thrown.
63 *
64 * @param reason a description of what caused the exception
65 */
66 public SOAPException(String reason) {
67 super(reason);
68 this.cause = null;
69 }
70
71 /**
72 * Constructs a <code>SOAPException</code> object with the given
73 * <code>String</code> as the reason for the exception being thrown
74 * and the given <code>Throwable</code> object as an embedded
75 * exception.
76 *
77 * @param reason a description of what caused the exception
78 * @param cause a <code>Throwable</code> object that is to
79 * be embedded in this <code>SOAPException</code> object
80 */
81 public SOAPException(String reason, Throwable cause) {
82 super(reason);
83 initCause(cause);
84 }
85
86 /**
87 * Constructs a <code>SOAPException</code> object initialized
88 * with the given <code>Throwable</code> object.
89 */
90 public SOAPException(Throwable cause) {
91 super(cause.toString());
92 initCause(cause);
93 }
94
95 /**
96 * Returns the detail message for this <code>SOAPException</code>
97 * object.
98 * <P>
99 * 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("Self-causation not permitted");
163 }
164 this.cause = cause;
165
166 return this;
167 }
168 }