1 21 package org.dbunit.ext.oracle; 22 23 import org.dbunit.dataset.datatype.BlobDataType; 24 import org.dbunit.dataset.datatype.TypeCastException; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.sql.Connection ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 35 40 public class OracleBlobDataType extends BlobDataType 41 { 42 private static final Integer DURATION_SESSION = new Integer (1); 43 private static final Integer MODE_READWRITE = new Integer (1); 46 47 public Object getSqlValue(int column, ResultSet resultSet) 48 throws SQLException , TypeCastException 49 { 50 return typeCast(resultSet.getBlob(column)); 51 } 52 53 public void setSqlValue(Object value, int column, PreparedStatement statement) 54 throws SQLException , TypeCastException 55 { 56 statement.setObject(column, getBlob(value, statement.getConnection())); 57 } 58 59 private Object getBlob(Object value, Connection connection) 60 throws TypeCastException 61 { 62 Object tempBlob = null; 63 try 64 { 65 Class aBlobClass = Class.forName("oracle.sql.BLOB"); 66 67 Method createTemporaryMethod = aBlobClass.getMethod("createTemporary", 69 new Class []{Connection .class, Boolean.TYPE, Integer.TYPE}); 70 tempBlob = createTemporaryMethod.invoke(null, 71 new Object []{connection, Boolean.TRUE, DURATION_SESSION}); 72 73 Method openMethod = aBlobClass.getMethod("open", new Class []{Integer.TYPE}); 75 openMethod.invoke(tempBlob, new Object []{MODE_READWRITE}); 76 77 Method getOutputStreamMethod = tempBlob.getClass().getMethod( 79 "getBinaryOutputStream", new Class [0]); 80 OutputStream tempBlobOutputStream = (OutputStream )getOutputStreamMethod.invoke( 81 tempBlob, new Object [0]); 82 83 tempBlobOutputStream.write((byte[])typeCast(value)); 85 86 tempBlobOutputStream.flush(); 88 tempBlobOutputStream.close(); 89 90 Method closeMethod = tempBlob.getClass().getMethod( 92 "close", new Class [0]); 93 closeMethod.invoke(tempBlob, new Object [0]); 94 } 95 catch (IllegalAccessException e) 96 { 97 freeTemporaryBlob(tempBlob); 98 throw new TypeCastException(value, this, e); 99 } 100 catch (NoSuchMethodException e) 101 { 102 freeTemporaryBlob(tempBlob); 103 throw new TypeCastException(value, this, e); 104 } 105 catch (IOException e) 106 { 107 freeTemporaryBlob(tempBlob); 108 throw new TypeCastException(value, this, e); 109 } 110 catch (InvocationTargetException e) 111 { 112 freeTemporaryBlob(tempBlob); 113 throw new TypeCastException(value, this, e); 114 } 115 catch (ClassNotFoundException e) 116 { 117 freeTemporaryBlob(tempBlob); 118 throw new TypeCastException(value, this, e); 119 } 120 121 return tempBlob; 122 } 123 124 125 private void freeTemporaryBlob(Object tempBlob) throws TypeCastException 126 { 127 if (tempBlob == null) 128 { 129 return; 130 } 131 132 try 133 { 134 Method freeTemporaryMethod = tempBlob.getClass().getMethod("freeTemporary", new Class [0]); 135 freeTemporaryMethod.invoke(tempBlob, new Object [0]); 136 } 137 catch (NoSuchMethodException e) 138 { 139 throw new TypeCastException("Error freeing Oracle BLOB", e); 140 } 141 catch (IllegalAccessException e) 142 { 143 throw new TypeCastException("Error freeing Oracle BLOB", e); 144 } 145 catch (InvocationTargetException e) 146 { 147 throw new TypeCastException("Error freeing Oracle BLOB", e.getTargetException()); 148 } 149 } 150 151 } 152 | Popular Tags |