1 21 22 package org.continuent.sequoia.common.exceptions.driver.protocol; 23 24 import java.io.IOException ; 25 import java.io.PrintStream ; 26 import java.io.PrintWriter ; 27 28 import org.continuent.sequoia.common.stream.DriverBufferedInputStream; 29 import org.continuent.sequoia.common.stream.DriverBufferedOutputStream; 30 31 45 public class SerializableException extends Exception 46 { 47 private static final long serialVersionUID = -7676999309139924676L; 48 49 private String sqlState; 50 private int vendorCode; 51 52 private SerializableStackTraceElement[] stackTrace; 53 54 57 SerializableException(String message, SerializableException cause) 58 { 59 super(message, cause); 60 } 61 62 69 70 SerializableException(Throwable start) 71 { 72 this(start.getMessage(), null == start.getCause() 73 ? null 74 : new SerializableException(start.getCause())); 76 convertStackTrace(start); 77 } 78 79 void convertStackTrace(Throwable regularEx) 80 { 81 StackTraceElement [] regularST = regularEx.getStackTrace(); 82 stackTrace = new SerializableStackTraceElement[regularST.length]; 83 for (int i = 0; i < regularST.length; i++) 84 stackTrace[i] = new SerializableStackTraceElement(regularST[i]); 85 86 setStackTrace(null ); 89 90 } 91 92 93 94 97 SerializableException(DriverBufferedInputStream in) throws IOException 98 { 99 super(in.readLongUTF(), in.readBoolean() ? new SerializableException(in) : null); 102 103 stackTrace = new SerializableStackTraceElement[in.readInt()]; 105 for (int i = 0; i < stackTrace.length; i++) 106 stackTrace[i] = new SerializableStackTraceElement(in); 107 108 setSQLState(in.readLongUTF()); 110 setErrorCode(in.readInt()); 111 112 } 113 114 120 public void sendToStream(DriverBufferedOutputStream out) throws IOException 121 { 122 out.writeLongUTF(getMessage()); 124 125 if (null != getCause()) 127 { 128 out.writeBoolean(true); 129 ((SerializableException) getCause()).sendToStream(out); } 131 else 132 out.writeBoolean(false); 134 out.writeInt(stackTrace.length); 136 for (int i = 0; i < stackTrace.length; i++) 137 stackTrace[i].sendToStream(out); 138 139 out.writeLongUTF(getSQLState()); 141 out.writeInt(getErrorCode()); 142 143 out.flush(); 144 } 145 146 149 public void printStackTrace() 150 { 151 printStackTrace(System.err); 152 } 153 154 159 public void printStackTrace(PrintStream s) 160 { 161 synchronized (s) 162 { 163 s.println(this); 164 for (int i = 0; i < stackTrace.length; i++) 165 s.println("\tAt: " + stackTrace[i]); 166 167 SerializableException ourCause = (SerializableException) getCause(); 168 if (ourCause != null) 169 { 170 s.println("Caused by"); 171 ourCause.printStackTrace(s); 172 } 173 } 174 } 175 176 181 public void printStackTrace(PrintWriter s) 182 { 183 synchronized (s) 184 { 185 s.println(this); 186 for (int i = 0; i < stackTrace.length; i++) 187 s.println("\tat " + stackTrace[i]); 188 189 SerializableException ourCause = (SerializableException) getCause(); 190 if (ourCause != null) 191 { 192 s.println("Caused-by"); 193 ourCause.printStackTrace(s); 194 } 195 } 196 } 197 198 202 public synchronized Throwable fillInStackTrace() 203 { 204 setStackTrace(null); 205 return this; 206 } 207 208 216 public StackTraceElement [] getStackTrace() 217 { 218 return new StackTraceElement [0]; 219 } 220 221 226 public SerializableStackTraceElement[] getSerializableStackTrace() 227 { 228 return (SerializableStackTraceElement[]) stackTrace.clone(); 229 } 230 231 240 public void setStackTrace(StackTraceElement [] ignored) 241 { 242 super.setStackTrace(new StackTraceElement [0]); 243 } 244 245 250 void setErrorCode(int vendorCode) 251 { 252 this.vendorCode = vendorCode; 253 } 254 255 260 public int getErrorCode() 261 { 262 return vendorCode; 263 } 264 265 270 public void setSQLState(String sQLState) 271 { 272 this.sqlState = sQLState; 273 } 274 275 280 public String getSQLState() 281 { 282 return sqlState; 283 } 284 285 290 public Throwable initCause(Throwable cause) 291 { 292 throwsIfNotSerializable(cause); 293 294 super.initCause(cause); 295 return this; 296 } 297 298 private void throwsIfNotSerializable(Throwable cause) 299 throws IllegalArgumentException 300 { 301 if (null == cause) 302 return; 303 304 if (!(cause instanceof SerializableException)) 305 throw new IllegalArgumentException ( 306 "The cause of SerializableException has to be a SerializableException"); 307 } 308 309 } 310 | Popular Tags |