001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: SOAPExceptionImpl.java,v 1.2 2007/07/16 16:41:19 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:19 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj;
062:
063: import java.io.PrintStream;
064: import java.io.PrintWriter;
065:
066: import javax.xml.soap.SOAPException;
067:
068: /**
069: * An exception that signals that a SOAP exception has occurred. A
070: * <code>SOAPExceptionImpl</code> object may contain a <code>String</code>
071: * that gives the reason for the exception, an embedded
072: * <code>Throwable</code> object, or both. This class provides methods
073: * for retrieving reason messages and for retrieving the embedded
074: * <code>Throwable</code> object.
075: *
076: * <P> Typical reasons for throwing a <code>SOAPExceptionImpl</code>
077: * object are problems such as difficulty setting a header, not being
078: * able to send a message, and not being able to get a connection with
079: * the provider. Reasons for embedding a <code>Throwable</code>
080: * object include problems such as input/output errors or a parsing
081: * problem, such as an error in parsing a header.
082: */
083: public class SOAPExceptionImpl extends SOAPException {
084: private Throwable cause;
085:
086: /**
087: * Constructs a <code>SOAPExceptionImpl</code> object with no
088: * reason or embedded <code>Throwable</code> object.
089: */
090: public SOAPExceptionImpl() {
091: super ();
092: this .cause = null;
093: }
094:
095: /**
096: * Constructs a <code>SOAPExceptionImpl</code> object with the given
097: * <code>String</code> as the reason for the exception being thrown.
098: *
099: * @param reason a description of what caused the exception
100: */
101: public SOAPExceptionImpl(String reason) {
102: super (reason);
103: this .cause = null;
104: }
105:
106: /**
107: * Constructs a <code>SOAPExceptionImpl</code> object with the given
108: * <code>String</code> as the reason for the exception being thrown
109: * and the given <code>Throwable</code> object as an embedded
110: * exception.
111: *
112: * @param reason a description of what caused the exception
113: * @param cause a <code>Throwable</code> object that is to
114: * be embedded in this <code>SOAPExceptionImpl</code> object
115: */
116: public SOAPExceptionImpl(String reason, Throwable cause) {
117: super (reason);
118: initCause(cause);
119: }
120:
121: /**
122: * Constructs a <code>SOAPExceptionImpl</code> object initialized
123: * with the given <code>Throwable</code> object.
124: */
125: public SOAPExceptionImpl(Throwable cause) {
126: super (cause.toString());
127: initCause(cause);
128: }
129:
130: /**
131: * Returns the detail message for this <code>SOAPExceptionImpl</code>
132: * object.
133: * <P>
134: * If there is an embedded <code>Throwable</code> object, and if the
135: * <code>SOAPExceptionImpl</code> object has no detail message of its
136: * own, this method will return the detail message from the embedded
137: * <code>Throwable</code> object.
138: *
139: * @return the error or warning message for this
140: * <code>SOAPExceptionImpl</code> or, if it has none, the
141: * message of the embedded <code>Throwable</code> object,
142: * if there is one
143: */
144: public String getMessage() {
145: String message = super .getMessage();
146: if (message == null && cause != null) {
147: return cause.getMessage();
148: } else {
149: return message;
150: }
151: }
152:
153: /**
154: * Returns the <code>Throwable</code> object embedded in this
155: * <code>SOAPExceptionImpl</code> if there is one. Otherwise, this method
156: * returns <code>null</code>.
157: *
158: * @return the embedded <code>Throwable</code> object or <code>null</code>
159: * if there is none
160: */
161:
162: public Throwable getCause() {
163: return cause;
164: }
165:
166: /**
167: * Initializes the <code>cause</code> field of this <code>SOAPExceptionImpl</code>
168: * object with the given <code>Throwable</code> object.
169: * <P>
170: * This method can be called at most once. It is generally called from
171: * within the constructor or immediately after the constructor has
172: * returned a new <code>SOAPExceptionImpl</code> object.
173: * If this <code>SOAPExceptionImpl</code> object was created with the
174: * constructor {@link #SOAPExceptionImpl(Throwable)} or
175: * {@link #SOAPExceptionImpl(String,Throwable)}, meaning that its
176: * <code>cause</code> field already has a value, this method cannot be
177: * called even once.
178: *
179: * @param cause the <code>Throwable</code> object that caused this
180: * <code>SOAPExceptionImpl</code> object to be thrown. The value of this
181: * parameter is saved for later retrieval by the
182: * {@link #getCause()} method. A <tt>null</tt> value is
183: * permitted and indicates that the cause is nonexistent or
184: * unknown.
185: * @return a reference to this <code>SOAPExceptionImpl</code> instance
186: * @throws IllegalArgumentException if <code>cause</code> is this
187: * <code>Throwable</code> object. (A <code>Throwable</code> object
188: * cannot be its own cause.)
189: * @throws IllegalStateException if this <code>SOAPExceptionImpl</code> object
190: * was created with {@link #SOAPExceptionImpl(Throwable)} or
191: * {@link #SOAPExceptionImpl(String,Throwable)}, or this
192: * method has already been called on this <code>SOAPExceptionImpl</code>
193: * object
194: */
195: public synchronized Throwable initCause(Throwable cause) {
196: if (this .cause != null) {
197: throw new IllegalStateException("Can't override cause");
198: }
199: if (cause == this ) {
200: throw new IllegalArgumentException(
201: "Self-causation not permitted");
202: }
203: this .cause = cause;
204:
205: return this ;
206: }
207:
208: public void printStackTrace() {
209: super .printStackTrace();
210: if (cause != null) {
211: System.err.println("\nCAUSE:\n");
212: cause.printStackTrace();
213: }
214: }
215:
216: public void printStackTrace(PrintStream s) {
217: super .printStackTrace(s);
218: if (cause != null) {
219: s.println("\nCAUSE:\n");
220: cause.printStackTrace(s);
221: }
222: }
223:
224: public void printStackTrace(PrintWriter s) {
225: super .printStackTrace(s);
226: if (cause != null) {
227: s.println("\nCAUSE:\n");
228: cause.printStackTrace(s);
229: }
230: }
231: }
|