src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/SOAPExceptionImpl.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/SOAPExceptionImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,196 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.messaging.saaj;
    1.30 +
    1.31 +import java.io.PrintStream;
    1.32 +import java.io.PrintWriter;
    1.33 +
    1.34 +import javax.xml.soap.SOAPException;
    1.35 +
    1.36 +/**
    1.37 + * An exception that signals that a SOAP exception has occurred. A
    1.38 + * <code>SOAPExceptionImpl</code> object may contain a <code>String</code>
    1.39 + * that gives the reason for the exception, an embedded
    1.40 + * <code>Throwable</code> object, or both. This class provides methods
    1.41 + * for retrieving reason messages and for retrieving the embedded
    1.42 + * <code>Throwable</code> object.
    1.43 + *
    1.44 + * <P> Typical reasons for throwing a <code>SOAPExceptionImpl</code>
    1.45 + * object are problems such as difficulty setting a header, not being
    1.46 + * able to send a message, and not being able to get a connection with
    1.47 + * the provider.  Reasons for embedding a <code>Throwable</code>
    1.48 + * object include problems such as input/output errors or a parsing
    1.49 + * problem, such as an error in parsing a header.
    1.50 + */
    1.51 +public class SOAPExceptionImpl extends SOAPException {
    1.52 +    private Throwable cause;
    1.53 +
    1.54 +    /**
    1.55 +     * Constructs a <code>SOAPExceptionImpl</code> object with no
    1.56 +     * reason or embedded <code>Throwable</code> object.
    1.57 +     */
    1.58 +    public SOAPExceptionImpl() {
    1.59 +        super();
    1.60 +        this.cause = null;
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * Constructs a <code>SOAPExceptionImpl</code> object with the given
    1.65 +     * <code>String</code> as the reason for the exception being thrown.
    1.66 +     *
    1.67 +     * @param reason a description of what caused the exception
    1.68 +     */
    1.69 +    public SOAPExceptionImpl(String reason) {
    1.70 +        super(reason);
    1.71 +        this.cause = null;
    1.72 +    }
    1.73 +
    1.74 +    /**
    1.75 +     * Constructs a <code>SOAPExceptionImpl</code> object with the given
    1.76 +     * <code>String</code> as the reason for the exception being thrown
    1.77 +     * and the given <code>Throwable</code> object as an embedded
    1.78 +     * exception.
    1.79 +     *
    1.80 +     * @param reason a description of what caused the exception
    1.81 +     * @param cause a <code>Throwable</code> object that is to
    1.82 +     *        be embedded in this <code>SOAPExceptionImpl</code> object
    1.83 +     */
    1.84 +    public SOAPExceptionImpl(String reason, Throwable cause) {
    1.85 +       super (reason);
    1.86 +       initCause(cause);
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * Constructs a <code>SOAPExceptionImpl</code> object initialized
    1.91 +     * with the given <code>Throwable</code> object.
    1.92 +     */
    1.93 +    public SOAPExceptionImpl(Throwable cause) {
    1.94 +        super (cause.toString());
    1.95 +        initCause(cause);
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Returns the detail message for this <code>SOAPExceptionImpl</code>
   1.100 +     * object.
   1.101 +     * <P>
   1.102 +     * If there is an embedded <code>Throwable</code> object, and if the
   1.103 +     * <code>SOAPExceptionImpl</code> object has no detail message of its
   1.104 +     * own, this method will return the detail message from the embedded
   1.105 +     * <code>Throwable</code> object.
   1.106 +     *
   1.107 +     * @return the error or warning message for this
   1.108 +     *         <code>SOAPExceptionImpl</code> or, if it has none, the
   1.109 +     *         message of the embedded <code>Throwable</code> object,
   1.110 +     *         if there is one
   1.111 +     */
   1.112 +    public String getMessage() {
   1.113 +        String message = super.getMessage ();
   1.114 +        if (message == null && cause != null) {
   1.115 +            return cause.getMessage();
   1.116 +        } else {
   1.117 +            return message;
   1.118 +        }
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Returns the <code>Throwable</code> object embedded in this
   1.123 +     * <code>SOAPExceptionImpl</code> if there is one. Otherwise, this method
   1.124 +     * returns <code>null</code>.
   1.125 +     *
   1.126 +     * @return the embedded <code>Throwable</code> object or <code>null</code>
   1.127 +     *         if there is none
   1.128 +     */
   1.129 +
   1.130 +    public Throwable getCause() {
   1.131 +        return cause;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Initializes the <code>cause</code> field of this <code>SOAPExceptionImpl</code>
   1.136 +     * object with the given <code>Throwable</code> object.
   1.137 +     * <P>
   1.138 +     * This method can be called at most once.  It is generally called from
   1.139 +     * within the constructor or immediately after the constructor has
   1.140 +     * returned a new <code>SOAPExceptionImpl</code> object.
   1.141 +     * If this <code>SOAPExceptionImpl</code> object was created with the
   1.142 +     * constructor {@link #SOAPExceptionImpl(Throwable)} or
   1.143 +     * {@link #SOAPExceptionImpl(String,Throwable)}, meaning that its
   1.144 +     * <code>cause</code> field already has a value, this method cannot be
   1.145 +     * called even once.
   1.146 +     *
   1.147 +     * @param  cause the <code>Throwable</code> object that caused this
   1.148 +     *         <code>SOAPExceptionImpl</code> object to be thrown.  The value of this
   1.149 +     *         parameter is saved for later retrieval by the
   1.150 +     *         {@link #getCause()} method.  A <tt>null</tt> value is
   1.151 +     *         permitted and indicates that the cause is nonexistent or
   1.152 +     *         unknown.
   1.153 +     * @return  a reference to this <code>SOAPExceptionImpl</code> instance
   1.154 +     * @throws IllegalArgumentException if <code>cause</code> is this
   1.155 +     *         <code>Throwable</code> object.  (A <code>Throwable</code> object
   1.156 +     *         cannot be its own cause.)
   1.157 +     * @throws IllegalStateException if this <code>SOAPExceptionImpl</code> object
   1.158 +     *         was created with {@link #SOAPExceptionImpl(Throwable)} or
   1.159 +     *         {@link #SOAPExceptionImpl(String,Throwable)}, or this
   1.160 +     *         method has already been called on this <code>SOAPExceptionImpl</code>
   1.161 +     *         object
   1.162 +     */
   1.163 +    public synchronized Throwable initCause(Throwable cause)
   1.164 +    {
   1.165 +        if(this.cause != null) {
   1.166 +            throw new IllegalStateException("Can't override cause");
   1.167 +        }
   1.168 +        if(cause == this) {
   1.169 +            throw new IllegalArgumentException("Self-causation not permitted");
   1.170 +        }
   1.171 +        this.cause = cause;
   1.172 +
   1.173 +        return this;
   1.174 +    }
   1.175 +
   1.176 +    public void printStackTrace() {
   1.177 +        super.printStackTrace();
   1.178 +        if (cause != null) {
   1.179 +            System.err.println("\nCAUSE:\n");
   1.180 +            cause.printStackTrace();
   1.181 +        }
   1.182 +    }
   1.183 +
   1.184 +    public void printStackTrace(PrintStream s) {
   1.185 +        super.printStackTrace(s);
   1.186 +        if (cause != null) {
   1.187 +            s.println("\nCAUSE:\n");
   1.188 +            cause.printStackTrace(s);
   1.189 +        }
   1.190 +    }
   1.191 +
   1.192 +    public void printStackTrace(PrintWriter s) {
   1.193 +        super.printStackTrace(s);
   1.194 +        if (cause != null) {
   1.195 +            s.println("\nCAUSE:\n");
   1.196 +            cause.printStackTrace(s);
   1.197 +        }
   1.198 +    }
   1.199 +}

mercurial