1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import org.jboss.logging.Logger; 25 26 import javax.ejb.Handle ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.Clob ; 30 import java.sql.Blob ; 31 import java.rmi.MarshalledObject ; 32 import java.rmi.RemoteException ; 33 import java.io.IOException ; 34 import java.io.Reader ; 35 import java.io.InputStream ; 36 37 43 public interface JDBCResultSetReader 44 { 45 54 Object get(ResultSet rs, int index, Class destination, Logger log) throws SQLException ; 55 56 public static final JDBCResultSetReader CLOB_READER = new AbstractResultSetReader() 57 { 58 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 59 { 60 Clob clob = rs.getClob(index); 61 62 String content; 63 if(clob == null) 64 { 65 content = null; 66 } 67 else 68 { 69 final Reader reader = clob.getCharacterStream(); 70 if(reader != null) 71 { 72 int intLength = (int)clob.length(); 73 74 char[] chars; 75 try 76 { 77 if(intLength <= 8192) 78 { 79 chars = new char[intLength]; 80 reader.read(chars); 81 content = String.valueOf(chars); 82 } 83 else 84 { 85 StringBuffer buf = new StringBuffer (intLength); 86 chars = new char[8192]; 87 int i = reader.read(chars); 88 while(i > 0) 89 { 90 buf.append(chars, 0, i); 91 i = reader.read(chars); 92 } 93 content = buf.toString(); 94 } 95 } 96 catch(IOException e) 97 { 98 throw new SQLException ("Failed to read CLOB character stream: " + e.getMessage()); 99 } 100 finally 101 { 102 JDBCUtil.safeClose(reader); 103 } 104 } 105 else 106 { 107 content = null; 108 } 109 } 110 111 return content; 112 } 113 }; 114 115 public static final JDBCResultSetReader LONGVARCHAR_READER = new AbstractResultSetReader() 116 { 117 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 118 { 119 return JDBCUtil.getLongString(rs, index); 120 } 121 }; 122 123 public static final JDBCResultSetReader BINARY_READER = new AbstractResultSetReader() 124 { 125 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 126 { 127 Object value = null; 128 byte[] bytes = rs.getBytes(index); 129 if(!rs.wasNull()) 130 { 131 if(destination == byte[].class) 132 value = bytes; 133 else 134 value = JDBCUtil.convertToObject(bytes); 135 } 136 return value; 137 } 138 }; 139 140 public static final JDBCResultSetReader VARBINARY_READER = new AbstractResultSetReader() 141 { 142 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 143 { 144 Object value = null; 145 byte[] bytes = rs.getBytes(index); 146 if(!rs.wasNull()) 147 { 148 if(destination == byte[].class) 149 value = bytes; 150 else 151 value = JDBCUtil.convertToObject(bytes); 152 } 153 return value; 154 } 155 }; 156 157 public static final JDBCResultSetReader BLOB_READER = new AbstractResultSetReader() 158 { 159 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 160 { 161 Blob blob = rs.getBlob(index); 162 163 Object value; 164 if(blob == null) 165 { 166 value = null; 167 } 168 else 169 { 170 InputStream binaryData = blob.getBinaryStream(); 171 if(binaryData != null) 172 { 173 if(destination == byte[].class) 174 value = JDBCUtil.getByteArray(binaryData); 175 else 176 value = JDBCUtil.convertToObject(binaryData); 177 } 178 else 179 { 180 value = null; 181 } 182 } 183 184 return value; 185 } 186 }; 187 188 public static final JDBCResultSetReader LONGVARBINARY_READER = new AbstractResultSetReader() 189 { 190 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 191 { 192 Object value = null; 193 InputStream binaryData = rs.getBinaryStream(index); 194 if(binaryData != null && !rs.wasNull()) 195 { 196 if(destination == byte[].class) 197 value = JDBCUtil.getByteArray(binaryData); 198 else 199 value = JDBCUtil.convertToObject(binaryData); 200 } 201 return value; 202 } 203 }; 204 205 public static final JDBCResultSetReader JAVA_OBJECT_READER = new AbstractResultSetReader() 206 { 207 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 208 { 209 return rs.getObject(index); 210 } 211 }; 212 213 public static final JDBCResultSetReader STRUCT_READER = new AbstractResultSetReader() 214 { 215 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 216 { 217 return rs.getObject(index); 218 } 219 }; 220 221 public static final JDBCResultSetReader ARRAY_READER = new AbstractResultSetReader() 222 { 223 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 224 { 225 return rs.getObject(index); 226 } 227 }; 228 229 public static final JDBCResultSetReader OTHER_READER = new AbstractResultSetReader() 230 { 231 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 232 { 233 return rs.getObject(index); 234 } 235 }; 236 237 public static final JDBCResultSetReader JAVA_UTIL_DATE_READER = new AbstractResultSetReader() 238 { 239 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 240 { 241 return rs.getTimestamp(index); 242 } 243 244 protected Object coerceToJavaType(Object value, Class destination) 245 { 246 java.util.Date result; 248 if(value instanceof java.sql.Timestamp ) 250 { 251 java.sql.Timestamp ts = (java.sql.Timestamp )value; 252 long temp = ts.getTime(); 256 if(temp % 1000 == 0) 257 temp += ts.getNanos() / 1000000; 258 result = new java.util.Date (temp); 259 } 260 else 261 { 262 result = new java.util.Date (((java.util.Date )value).getTime()); 263 } 264 return result; 265 } 266 }; 267 268 public static final JDBCResultSetReader JAVA_SQL_DATE_READER = new AbstractResultSetReader() 269 { 270 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 271 { 272 return rs.getDate(index); 273 } 274 275 protected Object coerceToJavaType(Object value, Class destination) 276 { 277 return new java.sql.Date (((java.sql.Date )value).getTime()); 279 } 280 }; 281 282 public static final JDBCResultSetReader JAVA_SQL_TIME_READER = new AbstractResultSetReader() 283 { 284 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 285 { 286 return rs.getTime(index); 287 } 288 289 protected Object coerceToJavaType(Object value, Class destination) 290 { 291 return new java.sql.Time (((java.sql.Time )value).getTime()); 293 } 294 }; 295 296 public static final JDBCResultSetReader JAVA_SQL_TIMESTAMP_READER = new AbstractResultSetReader() 297 { 298 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 299 { 300 return rs.getTimestamp(index); 301 } 302 303 protected Object coerceToJavaType(Object value, Class destination) 304 { 305 java.sql.Timestamp orignal = (java.sql.Timestamp )value; 307 java.sql.Timestamp copy = new java.sql.Timestamp (orignal.getTime()); 308 copy.setNanos(orignal.getNanos()); 309 return copy; 310 } 311 }; 312 313 public static final JDBCResultSetReader BIGDECIMAL_READER = new AbstractResultSetReader() 314 { 315 protected Object readResult(ResultSet rs, int index, Class destination) 316 throws SQLException 317 { 318 return rs.getBigDecimal(index); 319 } 320 }; 321 322 public static final JDBCResultSetReader REF_READER = new AbstractResultSetReader() 323 { 324 protected Object readResult(ResultSet rs, int index, Class destination) 325 throws SQLException 326 { 327 return rs.getRef(index); 328 } 329 }; 330 331 public static final JDBCResultSetReader BYTE_ARRAY_READER = new AbstractResultSetReader() 332 { 333 protected Object readResult(ResultSet rs, int index, Class destination) 334 throws SQLException 335 { 336 return rs.getBytes(index); 337 } 338 }; 339 340 public static final JDBCResultSetReader OBJECT_READER = new AbstractResultSetReader() 341 { 342 protected Object readResult(ResultSet rs, int index, Class destination) throws SQLException 343 { 344 return rs.getObject(index); 345 } 346 }; 347 348 public static final JDBCResultSetReader STRING_READER = new JDBCResultSetReader() 349 { 350 public Object get(ResultSet rs, int index, Class destination, Logger log) throws SQLException 351 { 352 final String result = rs.getString(index); 353 354 if(log.isTraceEnabled()) 355 { 356 log.trace("result: i=" + index + ", type=" + destination.getName() + ", value=" + result); 357 } 358 359 return result; 360 } 361 }; 362 363 abstract class AbstractPrimitiveReader 364 extends AbstractResultSetReader 365 { 366 368 public Object get(ResultSet rs, int index, Class destination, Logger log) 369 throws SQLException 370 { 371 Object result = readResult(rs, index, destination); 372 if(rs.wasNull()) 373 result = null; 374 else 375 result = coerceToJavaType(result, destination); 376 377 if(log.isTraceEnabled()) 378 { 379 log.trace("result: i=" + index + ", type=" + destination.getName() + ", value=" + result); 380 } 381 382 return result; 383 } 384 385 387 protected Object coerceToJavaType(Object value, Class destination) 388 throws SQLException 389 { 390 return value; 391 } 392 } 393 394 public static final JDBCResultSetReader BOOLEAN_READER = new AbstractPrimitiveReader() 395 { 396 protected Object readResult(ResultSet rs, int index, Class destination) 397 throws SQLException 398 { 399 return (rs.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE); 400 } 401 }; 402 403 public static final JDBCResultSetReader BYTE_READER = new AbstractPrimitiveReader() 404 { 405 protected Object readResult(ResultSet rs, int index, Class destination) 406 throws SQLException 407 { 408 return new Byte (rs.getByte(index)); 409 } 410 }; 411 412 public static final JDBCResultSetReader CHARACTER_READER = new AbstractPrimitiveReader() 413 { 414 protected Object readResult(ResultSet rs, int index, Class destination) 415 throws SQLException 416 { 417 return rs.getString(index); 418 } 419 420 protected Object coerceToJavaType(Object value, Class destination) 421 { 422 if(value instanceof String && (destination == Character .class || destination == Character.TYPE)) 427 { 428 return new Character (((String )value).charAt(0)); 429 } 430 else 431 { 432 return value; 433 } 434 } 435 }; 436 437 public static final JDBCResultSetReader SHORT_READER = new AbstractPrimitiveReader() 438 { 439 protected Object readResult(ResultSet rs, int index, Class destination) 440 throws SQLException 441 { 442 return new Short (rs.getShort(index)); 443 } 444 }; 445 446 public static final JDBCResultSetReader INT_READER = new AbstractPrimitiveReader() 447 { 448 protected Object readResult(ResultSet rs, int index, Class destination) 449 throws SQLException 450 { 451 return new Integer (rs.getInt(index)); 452 } 453 }; 454 455 public static final JDBCResultSetReader LONG_READER = new AbstractPrimitiveReader() 456 { 457 protected Object readResult(ResultSet rs, int index, Class destination) 458 throws SQLException 459 { 460 return new Long (rs.getLong(index)); 461 } 462 }; 463 464 public static final JDBCResultSetReader FLOAT_READER = new AbstractPrimitiveReader() 465 { 466 protected Object readResult(ResultSet rs, int index, Class destination) 467 throws SQLException 468 { 469 return new Float (rs.getFloat(index)); 470 } 471 }; 472 473 public static final JDBCResultSetReader DOUBLE_READER = new AbstractPrimitiveReader() 474 { 475 protected Object readResult(ResultSet rs, int index, Class destination) 476 throws SQLException 477 { 478 return new Double (rs.getDouble(index)); 479 } 480 }; 481 482 abstract class AbstractResultSetReader implements JDBCResultSetReader 483 { 484 public Object get(ResultSet rs, int index, Class destination, Logger log) throws SQLException 485 { 486 Object result = readResult(rs, index, destination); 487 if(result != null) 488 result = coerceToJavaType(result, destination); 489 490 if(log.isTraceEnabled()) 491 { 492 log.trace("result: i=" + index + ", type=" + destination.getName() + ", value=" + result); 493 } 494 495 return result; 496 } 497 498 protected abstract Object readResult(ResultSet rs, int index, Class destination) 499 throws SQLException ; 500 501 protected Object coerceToJavaType(Object value, Class destination) 502 throws SQLException 503 { 504 try 505 { 506 if(value instanceof MarshalledObject && !destination.equals(MarshalledObject .class)) 511 { 512 value = ((MarshalledObject )value).get(); 513 } 514 515 if(value instanceof Handle ) 520 { 521 value = ((Handle )value).getEJBObject(); 522 } 523 524 if(destination.isAssignableFrom(value.getClass())) 526 { 527 return value; 528 } 529 530 if(destination == java.math.BigInteger .class && value.getClass() == java.math.BigDecimal .class) 531 { 532 return ((java.math.BigDecimal )value).toBigInteger(); 533 } 534 535 throw new SQLException ("Got a " + value.getClass().getName() + "[cl=" + 537 System.identityHashCode(value.getClass().getClassLoader()) + 538 ", value=" + value + "] while looking for a " + 539 destination.getName() + "[cl=" + 540 System.identityHashCode(destination) + "]"); 541 } 542 catch(RemoteException e) 543 { 544 throw new SQLException ("Unable to load EJBObject back from Handle: " + e); 545 } 546 catch(IOException e) 547 { 548 throw new SQLException ("Unable to load to deserialize result: " + e); 549 } 550 catch(ClassNotFoundException e) 551 { 552 throw new SQLException ("Unable to load to deserialize result: " + e); 553 } 554 } 555 } 556 } 557 | Popular Tags |