1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.messaging.saaj; 27 28 import java.io.PrintStream; 29 import java.io.PrintWriter; 30 31 import javax.xml.soap.SOAPException; 32 33 /** 34 * An exception that signals that a SOAP exception has occurred. A 35 * <code>SOAPExceptionImpl</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>SOAPExceptionImpl</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 SOAPExceptionImpl extends SOAPException { 49 private Throwable cause; 50 51 /** 52 * Constructs a <code>SOAPExceptionImpl</code> object with no 53 * reason or embedded <code>Throwable</code> object. 54 */ 55 public SOAPExceptionImpl() { 56 super(); 57 this.cause = null; 58 } 59 60 /** 61 * Constructs a <code>SOAPExceptionImpl</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 SOAPExceptionImpl(String reason) { 67 super(reason); 68 this.cause = null; 69 } 70 71 /** 72 * Constructs a <code>SOAPExceptionImpl</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>SOAPExceptionImpl</code> object 80 */ 81 public SOAPExceptionImpl(String reason, Throwable cause) { 82 super (reason); 83 initCause(cause); 84 } 85 86 /** 87 * Constructs a <code>SOAPExceptionImpl</code> object initialized 88 * with the given <code>Throwable</code> object. 89 * @param cause cause 90 */ 91 public SOAPExceptionImpl(Throwable cause) { 92 super (cause.toString()); 93 initCause(cause); 94 } 95 96 /** 97 * Returns the detail message for this <code>SOAPExceptionImpl</code> 98 * object. 99 * <P> 100 * If there is an embedded <code>Throwable</code> object, and if the 101 * <code>SOAPExceptionImpl</code> object has no detail message of its 102 * own, this method will return the detail message from the embedded 103 * <code>Throwable</code> object. 104 * 105 * @return the error or warning message for this 106 * <code>SOAPExceptionImpl</code> or, if it has none, the 107 * message of the embedded <code>Throwable</code> object, 108 * if there is one 109 */ 110 @Override 111 public String getMessage() { 112 String message = super.getMessage (); 113 if (message == null && cause != null) { 114 return cause.getMessage(); 115 } else { 116 return message; 117 } 118 } 119 120 /** 121 * Returns the <code>Throwable</code> object embedded in this 122 * <code>SOAPExceptionImpl</code> if there is one. Otherwise, this method 123 * returns <code>null</code>. 124 * 125 * @return the embedded <code>Throwable</code> object or <code>null</code> 126 * if there is none 127 */ 128 129 @Override 130 public Throwable getCause() { 131 return cause; 132 } 133 134 /** 135 * Initializes the <code>cause</code> field of this <code>SOAPExceptionImpl</code> 136 * object with the given <code>Throwable</code> object. 137 * <P> 138 * This method can be called at most once. It is generally called from 139 * within the constructor or immediately after the constructor has 140 * returned a new <code>SOAPExceptionImpl</code> object. 141 * If this <code>SOAPExceptionImpl</code> object was created with the 142 * constructor {@link #SOAPExceptionImpl(Throwable)} or 143 * {@link #SOAPExceptionImpl(String,Throwable)}, meaning that its 144 * <code>cause</code> field already has a value, this method cannot be 145 * called even once. 146 * 147 * @param cause the <code>Throwable</code> object that caused this 148 * <code>SOAPExceptionImpl</code> object to be thrown. The value of this 149 * parameter is saved for later retrieval by the 150 * {@link #getCause()} method. A {@code null} value is 151 * permitted and indicates that the cause is nonexistent or 152 * unknown. 153 * @return a reference to this <code>SOAPExceptionImpl</code> instance 154 * @throws IllegalArgumentException if <code>cause</code> is this 155 * <code>Throwable</code> object. (A <code>Throwable</code> object 156 * cannot be its own cause.) 157 * @throws IllegalStateException if this <code>SOAPExceptionImpl</code> object 158 * was created with {@link #SOAPExceptionImpl(Throwable)} or 159 * {@link #SOAPExceptionImpl(String,Throwable)}, or this 160 * method has already been called on this <code>SOAPExceptionImpl</code> 161 * object 162 */ 163 @Override 164 public synchronized Throwable initCause(Throwable cause) 165 { 166 if(this.cause != null) { 167 throw new IllegalStateException("Can't override cause"); 168 } 169 if(cause == this) { 170 throw new IllegalArgumentException("Self-causation not permitted"); 171 } 172 this.cause = cause; 173 174 return this; 175 } 176 177 @Override 178 public void printStackTrace() { 179 super.printStackTrace(); 180 if (cause != null) { 181 System.err.println("\nCAUSE:\n"); 182 cause.printStackTrace(); 183 } 184 } 185 186 @Override 187 public void printStackTrace(PrintStream s) { 188 super.printStackTrace(s); 189 if (cause != null) { 190 s.println("\nCAUSE:\n"); 191 cause.printStackTrace(s); 192 } 193 } 194 195 @Override 196 public void printStackTrace(PrintWriter s) { 197 super.printStackTrace(s); 198 if (cause != null) { 199 s.println("\nCAUSE:\n"); 200 cause.printStackTrace(s); 201 } 202 } 203 }