ohair@286: /*
ohair@286: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package javax.xml.soap;
ohair@286:
ohair@286: /**
ohair@286: * An exception that signals that a SOAP exception has occurred. A
ohair@286: * SOAPException
object may contain a String
ohair@286: * that gives the reason for the exception, an embedded
ohair@286: * Throwable
object, or both. This class provides methods
ohair@286: * for retrieving reason messages and for retrieving the embedded
ohair@286: * Throwable
object.
ohair@286: *
ohair@286: *
Typical reasons for throwing a SOAPException
ohair@286: * object are problems such as difficulty setting a header, not being
ohair@286: * able to send a message, and not being able to get a connection with
ohair@286: * the provider. Reasons for embedding a Throwable
ohair@286: * object include problems such as input/output errors or a parsing
ohair@286: * problem, such as an error in parsing a header.
ohair@286: */
ohair@286: public class SOAPException extends Exception {
ohair@286: private Throwable cause;
ohair@286:
ohair@286: /**
ohair@286: * Constructs a SOAPException
object with no
ohair@286: * reason or embedded Throwable
object.
ohair@286: */
ohair@286: public SOAPException() {
ohair@286: super();
ohair@286: this.cause = null;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Constructs a SOAPException
object with the given
ohair@286: * String
as the reason for the exception being thrown.
ohair@286: *
ohair@286: * @param reason a description of what caused the exception
ohair@286: */
ohair@286: public SOAPException(String reason) {
ohair@286: super(reason);
ohair@286: this.cause = null;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Constructs a SOAPException
object with the given
ohair@286: * String
as the reason for the exception being thrown
ohair@286: * and the given Throwable
object as an embedded
ohair@286: * exception.
ohair@286: *
ohair@286: * @param reason a description of what caused the exception
ohair@286: * @param cause a Throwable
object that is to
ohair@286: * be embedded in this SOAPException
object
ohair@286: */
ohair@286: public SOAPException(String reason, Throwable cause) {
ohair@286: super(reason);
ohair@286: initCause(cause);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Constructs a SOAPException
object initialized
ohair@286: * with the given Throwable
object.
ohair@286: */
ohair@286: public SOAPException(Throwable cause) {
ohair@286: super(cause.toString());
ohair@286: initCause(cause);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Returns the detail message for this SOAPException
ohair@286: * object.
ohair@286: *
ohair@286: * If there is an embedded Throwable
object, and if the
ohair@286: * SOAPException
object has no detail message of its
ohair@286: * own, this method will return the detail message from the embedded
ohair@286: * Throwable
object.
ohair@286: *
ohair@286: * @return the error or warning message for this
ohair@286: * SOAPException
or, if it has none, the
ohair@286: * message of the embedded Throwable
object,
ohair@286: * if there is one
ohair@286: */
ohair@286: public String getMessage() {
ohair@286: String message = super.getMessage();
ohair@286: if (message == null && cause != null) {
ohair@286: return cause.getMessage();
ohair@286: } else {
ohair@286: return message;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Returns the Throwable
object embedded in this
ohair@286: * SOAPException
if there is one. Otherwise, this method
ohair@286: * returns null
.
ohair@286: *
ohair@286: * @return the embedded Throwable
object or null
ohair@286: * if there is none
ohair@286: */
ohair@286:
ohair@286: public Throwable getCause() {
ohair@286: return cause;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Initializes the cause
field of this SOAPException
ohair@286: * object with the given Throwable
object.
ohair@286: *
ohair@286: * This method can be called at most once. It is generally called from
ohair@286: * within the constructor or immediately after the constructor has
ohair@286: * returned a new SOAPException
object.
ohair@286: * If this SOAPException
object was created with the
ohair@286: * constructor {@link #SOAPException(Throwable)} or
ohair@286: * {@link #SOAPException(String,Throwable)}, meaning that its
ohair@286: * cause
field already has a value, this method cannot be
ohair@286: * called even once.
ohair@286: *
ohair@286: * @param cause the Throwable
object that caused this
ohair@286: * SOAPException
object to be thrown. The value of this
ohair@286: * parameter is saved for later retrieval by the
ohair@286: * {@link #getCause()} method. A null value is
ohair@286: * permitted and indicates that the cause is nonexistent or
ohair@286: * unknown.
ohair@286: * @return a reference to this SOAPException
instance
ohair@286: * @throws IllegalArgumentException if cause
is this
ohair@286: * Throwable
object. (A Throwable
object
ohair@286: * cannot be its own cause.)
ohair@286: * @throws IllegalStateException if the cause for this SOAPException
object
ohair@286: * has already been initialized
ohair@286: */
ohair@286: public synchronized Throwable initCause(Throwable cause) {
ohair@286: if (this.cause != null) {
ohair@286: throw new IllegalStateException("Can't override cause");
ohair@286: }
ohair@286: if (cause == this) {
ohair@286: throw new IllegalArgumentException("Self-causation not permitted");
ohair@286: }
ohair@286: this.cause = cause;
ohair@286:
ohair@286: return this;
ohair@286: }
ohair@286: }