View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 import java.io.PrintStream; 21 import java.io.PrintWriter; 22 import java.io.StringWriter; 23 24 /*** 25 * The BaseException class is used across all core services. 26 * 27 * It allows you to set a message with an exception, much like the 28 * normal java.lang.Exception class but in addition you can also carry 29 * along an original exception if you want. This 30 * is incorportated in the output from getMessage and toString(). 31 * 32 * The previous version of this class used to check if the string 33 * passed in was part of the MessageCatalog and if so, then use 34 * the associated key in creating the exception message. Various constructors 35 * were also provided to do string replacement on the exception message. 36 * This functionality is now removed. It is the responsibility of the 37 * creator of the exception to lookup message text directly from the 38 * MessageCatalog and pass it to the constructor of the exception. 39 * 40 * Implementation inspired from various placed, most recently 41 * Javaworld, Javatips #91. Java 1.4 now has this functionality. 42 * 43 * @author Mark Pollack 44 * @version @VSS_FLAG_VERSION@ 45 */ 46 public class RBaseException extends Exception { 47 48 /*** 49 * Do nothing constructor. 50 * 51 */ 52 public RBaseException() {} 53 54 /*** 55 * Creates a new <tt>RBaseException</tt> instance with a given 56 * message string. 57 * 58 * @param message a <tt>String</tt> value 59 */ 60 public RBaseException(String message) { 61 super(message); 62 } 63 64 /*** 65 * Create an exception nesting the rootCause of this exception. 66 * 67 * @param rootCause a value of type 'Throwable'. 68 */ 69 public RBaseException(Throwable rootCause) { 70 super(); 71 if (rootCause != null) { 72 this.previousThrowable = rootCause; 73 stackTraceString = generateStackTraceString(rootCause); 74 } else { 75 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of RBaseException"); 76 stackTraceString = generateStackTraceString(this.previousThrowable); 77 } 78 } 79 80 81 /*** 82 * Create a new <tt>RBaseException</tt> instance. Allows for 83 * an association of an originating exception. 84 * 85 * @param message Message to describe the exception 86 * @param rootCause The exceptio that caused this exception to be raised. 87 */ 88 public RBaseException(String message, Throwable rootCause) { 89 super(message); 90 if (rootCause != null) { 91 this.previousThrowable = rootCause; 92 stackTraceString = generateStackTraceString(rootCause); 93 } else { 94 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of RBaseException"); 95 stackTraceString = generateStackTraceString(this.previousThrowable); 96 } 97 } 98 99 /*** 100 * Create a new <tt>RBaseException</tt> instance. Allows for 101 * an association of an originating exception. 102 * 103 * @param message Message to describe the exception 104 * @param inserts an <tt>Object[]</tt> value 105 * @param rootCause The exceptio that caused this exception to be raised. 106 */ 107 public RBaseException(String message, Object[] inserts, Throwable rootCause) { 108 super(message); 109 if (rootCause != null) { 110 this.previousThrowable = rootCause; 111 stackTraceString = generateStackTraceString(rootCause); 112 } else { 113 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of RBaseException"); 114 stackTraceString = generateStackTraceString(this.previousThrowable); 115 } 116 } 117 118 /*** 119 * Create a new <tt>RBaseException</tt> instance. Allows for 120 * an association of an originating exception. 121 * 122 * @param message Message to describe the exception 123 * @param insert an <tt>Object</tt> value 124 * @param rootCause The exceptio that caused this exception to be raised. 125 */ 126 public RBaseException(String message, Object insert, Throwable rootCause) { 127 super(message); 128 if (rootCause != null) { 129 this.previousThrowable = rootCause; 130 stackTraceString = generateStackTraceString(rootCause); 131 } else { 132 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of RBaseException"); 133 stackTraceString = generateStackTraceString(this.previousThrowable); 134 } 135 } 136 137 /*** 138 * Get the exception that caused this exception to be raised. 139 * 140 * @return the root exception. 141 */ 142 public Throwable getRootCause() { 143 return previousThrowable; 144 } 145 146 147 148 /*** 149 * Return the stack trace including root cause exceptions. 150 * 151 * @return The stack trace of the exception. 152 */ 153 public String getStackTraceString() { 154 // if there's no nested exception, there's no stackTrace 155 if (previousThrowable == null) 156 return null; 157 158 StringBuffer traceBuffer = new StringBuffer(); 159 160 if (previousThrowable instanceof RBaseException) { 161 162 traceBuffer.append(((RBaseException)previousThrowable).getStackTraceString()); 163 traceBuffer.append("-------- nested by:\n"); 164 } 165 166 traceBuffer.append(stackTraceString); 167 return traceBuffer.toString(); 168 } 169 170 // overrides Exception.getMessage() 171 /*** 172 * Override Exceptin.getMessage to include information of the root 173 * causeo of the exception. 174 * 175 * @return The exception message, including root cause messages. 176 */ 177 public String getMessage() { 178 // superMsg will contain whatever String was passed into the 179 // constructor, and null otherwise. 180 String superMsg = super.getMessage(); 181 182 // if there's no nested exception, do like we would always do 183 if (getRootCause() == null) 184 return superMsg; 185 186 StringBuffer theMsg = new StringBuffer(); 187 188 // get the nested exception's message 189 String nestedMsg = getRootCause().getMessage(); 190 191 if (superMsg != null) 192 theMsg.append(superMsg).append(": ").append(nestedMsg); 193 else 194 theMsg.append(nestedMsg); 195 196 return theMsg.toString(); 197 } 198 199 // overrides Exception.toString() 200 public String toString() { 201 StringBuffer theMsg = new StringBuffer(super.toString()); 202 203 if (getRootCause() != null) 204 theMsg.append("; \n\t---> nested ").append(getRootCause()); 205 206 return theMsg.toString(); 207 } 208 209 210 211 //TODO: hmmmm what about these..... 212 213 public void printStackTrace() 214 { 215 super.printStackTrace(); 216 if (this.previousThrowable != null) 217 { 218 this.previousThrowable.printStackTrace(); 219 } 220 } 221 222 public void printStackTrace(PrintStream inPrintStream) 223 { 224 super.printStackTrace(inPrintStream); 225 if (this.previousThrowable != null) 226 { 227 this.previousThrowable.printStackTrace(inPrintStream); 228 } 229 } 230 231 232 public void printStackTrace(PrintWriter inPrintWriter) 233 { 234 super.printStackTrace(inPrintWriter); 235 if (this.previousThrowable != null) 236 { 237 this.previousThrowable.printStackTrace(inPrintWriter); 238 } 239 } 240 241 242 static public String generateStackTraceString(Throwable t) { 243 StringWriter s = new StringWriter(); 244 t.printStackTrace(new PrintWriter(s)); 245 return s.toString(); 246 247 } 248 249 /* 250 * By giving <tt>RBaseException</tt> a reference to a Throwable object, 251 * exception chaining can be enforced easily. 252 */ 253 private Throwable previousThrowable = null; 254 private String stackTraceString; 255 } 256

This page was automatically generated by Maven