1 23 24 package org.objectweb.cjdbc.common.exceptions.driver.protocol; 25 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.io.PrintWriter ; 29 30 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 31 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 32 33 47 public class SerializableException 48 extends Exception 49 { 50 51 private String sqlState; 52 private int vendorCode; 53 54 private SerializableStackTraceElement[] stackTrace; 55 56 59 SerializableException(String message, SerializableException cause) 60 { 61 super(message, cause); 62 } 63 64 71 72 SerializableException(Throwable start) 73 { 74 this(start.getMessage(), null == start.getCause() 75 ? null 76 : new SerializableException(start.getCause())); 78 convertStackTrace(start); 79 } 80 81 void convertStackTrace(Throwable regularEx) 82 { 83 StackTraceElement [] regularST = regularEx.getStackTrace(); 84 stackTrace = new SerializableStackTraceElement[regularST.length]; 85 for (int i = 0; i < regularST.length; i++) 86 stackTrace[i] = new SerializableStackTraceElement(regularST[i]); 87 88 setStackTrace(null ); 91 92 } 93 94 95 96 99 SerializableException(CJDBCInputStream in) throws IOException 100 { 101 super(in.readUTF(), in.readBoolean() ? new SerializableException(in) : null); 104 105 stackTrace = new SerializableStackTraceElement[in.readInt()]; 107 for (int i = 0; i < stackTrace.length; i++) 108 stackTrace[i] = new SerializableStackTraceElement(in); 109 110 setSQLState(in.readUTF()); 112 setErrorCode(in.readInt()); 113 114 } 115 116 122 public void sendToStream(CJDBCOutputStream out) throws IOException 123 { 124 out.writeUTF(getMessage()); 126 127 if (null != getCause()) 129 { 130 out.writeBoolean(true); 131 ((SerializableException) getCause()).sendToStream(out); } 133 else 134 out.writeBoolean(false); 136 out.writeInt(stackTrace.length); 138 for (int i = 0; i < stackTrace.length; i++) 139 stackTrace[i].sendToStream(out); 140 141 out.writeUTF(getSQLState()); 143 out.writeInt(getErrorCode()); 144 145 out.flush(); 146 } 147 148 151 public void printStackTrace() 152 { 153 printStackTrace(System.err); 154 } 155 156 161 public void printStackTrace(PrintStream s) 162 { 163 synchronized (s) 164 { 165 s.println(this); 166 for (int i = 0; i < stackTrace.length; i++) 167 s.println("\tAt: " + stackTrace[i]); 168 169 SerializableException ourCause = (SerializableException) getCause(); 170 if (ourCause != null) 171 { 172 s.println("Caused by"); 173 ourCause.printStackTrace(s); 174 } 175 } 176 } 177 178 183 public void printStackTrace(PrintWriter s) 184 { 185 synchronized (s) 186 { 187 s.println(this); 188 for (int i = 0; i < stackTrace.length; i++) 189 s.println("\tAt: " + stackTrace[i]); 190 191 SerializableException ourCause = (SerializableException) getCause(); 192 if (ourCause != null) 193 { 194 s.println("Caused by"); 195 ourCause.printStackTrace(s); 196 } 197 } 198 } 199 200 201 205 public synchronized Throwable fillInStackTrace() 206 { 207 setStackTrace(null); 208 return this; 209 } 210 211 219 public StackTraceElement [] getStackTrace() 220 { 221 return new StackTraceElement [0]; 222 } 223 224 229 public SerializableStackTraceElement[] getSerializableStackTrace() 230 { 231 return (SerializableStackTraceElement[]) stackTrace.clone(); 232 } 233 234 243 public void setStackTrace(StackTraceElement [] ignored) 244 { 245 super.setStackTrace(new StackTraceElement [0]); 246 } 247 248 253 void setErrorCode(int vendorCode) 254 { 255 this.vendorCode = vendorCode; 256 } 257 258 263 public int getErrorCode() 264 { 265 return vendorCode; 266 } 267 268 273 public void setSQLState(String sQLState) 274 { 275 this.sqlState = sQLState; 276 } 277 278 283 public String getSQLState() 284 { 285 return sqlState; 286 } 287 288 293 public Throwable initCause(Throwable cause) 294 { 295 throwsIfNotSerializable(cause); 296 297 super.initCause(cause); 298 return this; 299 } 300 301 private void throwsIfNotSerializable(Throwable cause) 302 throws IllegalArgumentException 303 { 304 if (null == cause) 305 return; 306 307 if (!(cause instanceof SerializableException)) 308 throw new IllegalArgumentException ( 309 "The cause of SerializableException has to be a SerializableException"); 310 } 311 312 } 313 | Popular Tags |