1 21 package oracle.toplink.essentials.exceptions; 23 24 import java.io.*; 25 import oracle.toplink.essentials.internal.helper.JavaPlatform; 26 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator; 27 import oracle.toplink.essentials.internal.sessions.AbstractSession; 28 29 33 public abstract class TopLinkException extends RuntimeException { 34 protected transient AbstractSession session; 35 protected Throwable internalException; 36 protected static Boolean shouldPrintInternalException = null; 37 protected String indentationString; 38 protected int errorCode; 39 protected static final String CR = System.getProperty("line.separator"); 40 protected boolean hasBeenLogged; 42 43 47 public TopLinkException() { 48 this(""); 49 } 50 51 55 public TopLinkException(String theMessage) { 56 super(theMessage); 57 this.indentationString = ""; 58 hasBeenLogged = false; 59 } 60 61 65 public TopLinkException(String message, Throwable internalException) { 66 this(message); 67 setInternalException(internalException); 68 } 69 70 74 protected static String cr() { 75 return oracle.toplink.essentials.internal.helper.Helper.cr(); 76 } 77 78 82 public int getErrorCode() { 83 return errorCode; 84 } 85 86 90 public String getIndentationString() { 91 return indentationString; 92 } 93 94 101 public Throwable getInternalException() { 102 return internalException; 103 } 104 105 110 public String getMessage() { 111 StringWriter writer = new StringWriter(100); 112 113 if ((getInternalException() == null) || (!super.getMessage().equals(getInternalException().toString()))) { 115 writer.write(cr()); 116 writer.write(getIndentationString()); 117 writer.write(ExceptionMessageGenerator.getHeader("DescriptionHeader")); 118 writer.write(super.getMessage()); 119 } 120 121 if (getInternalException() != null) { 122 writer.write(cr()); 123 writer.write(getIndentationString()); 124 writer.write(ExceptionMessageGenerator.getHeader("InternalExceptionHeader")); 125 writer.write(getInternalException().toString()); 126 127 if ((getInternalException() instanceof java.lang.reflect.InvocationTargetException ) && ((((java.lang.reflect.InvocationTargetException )getInternalException()).getTargetException()) != null)) { 128 writer.write(cr()); 129 writer.write(getIndentationString()); 130 writer.write(ExceptionMessageGenerator.getHeader("TargetInvocationExceptionHeader")); 131 writer.write(((java.lang.reflect.InvocationTargetException )getInternalException()).getTargetException().toString()); 132 } 133 } 134 135 return writer.toString(); 136 } 137 138 142 public AbstractSession getSession() { 143 return session; 144 } 145 146 150 public boolean hasBeenLogged() { 151 return hasBeenLogged; 152 } 153 154 158 public void printStackTrace() { 159 printStackTrace(System.err); 160 } 161 162 166 public void printStackTrace(PrintStream outStream) { 167 printStackTrace(new PrintWriter(outStream)); 168 } 169 170 174 public void printStackTrace(PrintWriter writer) { 175 writer.write(ExceptionMessageGenerator.getHeader("LocalExceptionStackHeader")); 176 writer.write(cr()); 177 super.printStackTrace(writer); 178 179 if ((getInternalException() != null) && shouldPrintInternalException()) { 180 writer.write(ExceptionMessageGenerator.getHeader("InternalExceptionStackHeader")); 181 writer.write(cr()); 182 getInternalException().printStackTrace(writer); 183 184 if ((getInternalException() instanceof java.lang.reflect.InvocationTargetException ) && ((((java.lang.reflect.InvocationTargetException )getInternalException()).getTargetException()) != null)) { 185 writer.write(ExceptionMessageGenerator.getHeader("TargetInvocationExceptionStackHeader")); 186 writer.write(cr()); 187 ((java.lang.reflect.InvocationTargetException )getInternalException()).getTargetException().printStackTrace(writer); 188 } 189 } 190 writer.flush(); 191 } 192 193 196 public void setErrorCode(int errorCode) { 197 this.errorCode = errorCode; 198 } 199 200 204 public void setHasBeenLogged(boolean logged) { 205 this.hasBeenLogged = logged; 206 } 207 208 212 public void setIndentationString(String indentationString) { 213 this.indentationString = indentationString; 214 } 215 216 220 public void setInternalException(Throwable anException) { 221 internalException = anException; 222 JavaPlatform.setExceptionCause(this, anException); 223 } 224 225 228 public void setSession(AbstractSession session) { 229 this.session = session; 230 } 231 232 240 public static void setShouldPrintInternalException(boolean printException) { 241 shouldPrintInternalException = new Boolean (printException); 242 } 243 244 250 public static boolean shouldPrintInternalException() { 251 if (shouldPrintInternalException == null) { 252 shouldPrintInternalException = new Boolean (JavaPlatform.shouldPrintInternalException()); 253 } 254 return shouldPrintInternalException.booleanValue(); 255 } 256 257 260 public String toString() { 261 return getIndentationString() + ExceptionMessageGenerator.getHeader("ExceptionHeader") + getErrorCode() + "] (" + oracle.toplink.essentials.sessions.DatabaseLogin.getVersion() + "): " + getClass().getName() + getMessage(); 262 } 263 } 264 | Popular Tags |