001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.ext.oracle;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.datatype.BlobDataType;
027: import org.dbunit.dataset.datatype.TypeCastException;
028:
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033: import java.sql.Connection;
034: import java.sql.PreparedStatement;
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037:
038: /**
039: * @author Manuel Laflamme
040: * @version $Revision: 554 $
041: * @since Feb 2, 2004
042: */
043: public class OracleBlobDataType extends BlobDataType {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(OracleBlobDataType.class);
050:
051: private static final Integer DURATION_SESSION = new Integer(1);
052: // private static final Integer DURATION_CALL = new Integer(2);
053: // private static final Integer MODE_READONLY = new Integer(0);
054: private static final Integer MODE_READWRITE = new Integer(1);
055:
056: public Object getSqlValue(int column, ResultSet resultSet)
057: throws SQLException, TypeCastException {
058: logger.debug("getSqlValue(column=" + column + ", resultSet="
059: + resultSet + ") - start");
060:
061: return typeCast(resultSet.getBlob(column));
062: }
063:
064: public void setSqlValue(Object value, int column,
065: PreparedStatement statement) throws SQLException,
066: TypeCastException {
067: logger.debug("setSqlValue(value=" + value + ", column="
068: + column + ", statement=" + statement + ") - start");
069:
070: statement.setObject(column, getBlob(value, statement
071: .getConnection()));
072: }
073:
074: private Object getBlob(Object value, Connection connection)
075: throws TypeCastException {
076: logger.debug("getBlob(value=" + value + ", connection="
077: + connection + ") - start");
078:
079: Object tempBlob = null;
080: try {
081: Class aBlobClass = Class.forName("oracle.sql.BLOB");
082:
083: // Create new temporary Blob
084: Method createTemporaryMethod = aBlobClass.getMethod(
085: "createTemporary", new Class[] { Connection.class,
086: Boolean.TYPE, Integer.TYPE });
087: tempBlob = createTemporaryMethod.invoke(null, new Object[] {
088: connection, Boolean.TRUE, DURATION_SESSION });
089:
090: // Open the temporary Blob in readwrite mode to enable writing
091: Method openMethod = aBlobClass.getMethod("open",
092: new Class[] { Integer.TYPE });
093: openMethod
094: .invoke(tempBlob, new Object[] { MODE_READWRITE });
095:
096: // Get the output stream to write
097: Method getOutputStreamMethod = tempBlob.getClass()
098: .getMethod("getBinaryOutputStream", new Class[0]);
099: OutputStream tempBlobOutputStream = (OutputStream) getOutputStreamMethod
100: .invoke(tempBlob, new Object[0]);
101:
102: // Write the data into the temporary BLOB
103: tempBlobOutputStream.write((byte[]) typeCast(value));
104:
105: // Flush and close the stream
106: tempBlobOutputStream.flush();
107: tempBlobOutputStream.close();
108:
109: // Close the temporary Blob
110: Method closeMethod = tempBlob.getClass().getMethod("close",
111: new Class[0]);
112: closeMethod.invoke(tempBlob, new Object[0]);
113: } catch (IllegalAccessException e) {
114: logger.error("getBlob()", e);
115:
116: freeTemporaryBlob(tempBlob);
117: throw new TypeCastException(value, this , e);
118: } catch (NoSuchMethodException e) {
119: logger.error("getBlob()", e);
120:
121: freeTemporaryBlob(tempBlob);
122: throw new TypeCastException(value, this , e);
123: } catch (IOException e) {
124: logger.error("getBlob()", e);
125:
126: freeTemporaryBlob(tempBlob);
127: throw new TypeCastException(value, this , e);
128: } catch (InvocationTargetException e) {
129: logger.error("getBlob()", e);
130:
131: freeTemporaryBlob(tempBlob);
132: throw new TypeCastException(value, this , e);
133: } catch (ClassNotFoundException e) {
134: logger.error("getBlob()", e);
135:
136: freeTemporaryBlob(tempBlob);
137: throw new TypeCastException(value, this , e);
138: }
139:
140: return tempBlob;
141: }
142:
143: private void freeTemporaryBlob(Object tempBlob)
144: throws TypeCastException {
145: logger.debug("freeTemporaryBlob(tempBlob=" + tempBlob
146: + ") - start");
147:
148: if (tempBlob == null) {
149: return;
150: }
151:
152: try {
153: Method freeTemporaryMethod = tempBlob.getClass().getMethod(
154: "freeTemporary", new Class[0]);
155: freeTemporaryMethod.invoke(tempBlob, new Object[0]);
156: } catch (NoSuchMethodException e) {
157: logger.error("freeTemporaryBlob()", e);
158:
159: throw new TypeCastException("Error freeing Oracle BLOB", e);
160: } catch (IllegalAccessException e) {
161: logger.error("freeTemporaryBlob()", e);
162:
163: throw new TypeCastException("Error freeing Oracle BLOB", e);
164: } catch (InvocationTargetException e) {
165: logger.error("freeTemporaryBlob()", e);
166:
167: throw new TypeCastException("Error freeing Oracle BLOB", e
168: .getTargetException());
169: }
170: }
171:
172: }
|