KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ext > oracle > OracleBlobDataType


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 /**
36  * @author Manuel Laflamme
37  * @version $Revision: 1.2 $
38  * @since Feb 2, 2004
39  */

40 public class OracleBlobDataType extends BlobDataType
41 {
42     private static final Integer JavaDoc DURATION_SESSION = new Integer JavaDoc(1);
43 // private static final Integer DURATION_CALL = new Integer(2);
44
// private static final Integer MODE_READONLY = new Integer(0);
45
private static final Integer JavaDoc MODE_READWRITE = new Integer JavaDoc(1);
46
47     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
48             throws SQLException JavaDoc, TypeCastException
49     {
50         return typeCast(resultSet.getBlob(column));
51     }
52
53     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
54             throws SQLException JavaDoc, TypeCastException
55     {
56         statement.setObject(column, getBlob(value, statement.getConnection()));
57     }
58
59     private Object JavaDoc getBlob(Object JavaDoc value, Connection JavaDoc connection)
60             throws TypeCastException
61     {
62         Object JavaDoc tempBlob = null;
63         try
64         {
65             Class JavaDoc aBlobClass = Class.forName("oracle.sql.BLOB");
66
67             // Create new temporary Blob
68
Method JavaDoc createTemporaryMethod = aBlobClass.getMethod("createTemporary",
69                     new Class JavaDoc[]{Connection JavaDoc.class, Boolean.TYPE, Integer.TYPE});
70             tempBlob = createTemporaryMethod.invoke(null,
71                     new Object JavaDoc[]{connection, Boolean.TRUE, DURATION_SESSION});
72
73             // Open the temporary Blob in readwrite mode to enable writing
74
Method JavaDoc openMethod = aBlobClass.getMethod("open", new Class JavaDoc[]{Integer.TYPE});
75             openMethod.invoke(tempBlob, new Object JavaDoc[]{MODE_READWRITE});
76
77             // Get the output stream to write
78
Method JavaDoc getOutputStreamMethod = tempBlob.getClass().getMethod(
79                     "getBinaryOutputStream", new Class JavaDoc[0]);
80             OutputStream JavaDoc tempBlobOutputStream = (OutputStream JavaDoc)getOutputStreamMethod.invoke(
81                     tempBlob, new Object JavaDoc[0]);
82
83             // Write the data into the temporary BLOB
84
tempBlobOutputStream.write((byte[])typeCast(value));
85
86             // Flush and close the stream
87
tempBlobOutputStream.flush();
88             tempBlobOutputStream.close();
89
90             // Close the temporary Blob
91
Method JavaDoc closeMethod = tempBlob.getClass().getMethod(
92                     "close", new Class JavaDoc[0]);
93             closeMethod.invoke(tempBlob, new Object JavaDoc[0]);
94         }
95         catch (IllegalAccessException JavaDoc e)
96         {
97             freeTemporaryBlob(tempBlob);
98             throw new TypeCastException(value, this, e);
99         }
100         catch (NoSuchMethodException JavaDoc e)
101         {
102             freeTemporaryBlob(tempBlob);
103             throw new TypeCastException(value, this, e);
104         }
105         catch (IOException JavaDoc e)
106         {
107             freeTemporaryBlob(tempBlob);
108             throw new TypeCastException(value, this, e);
109         }
110         catch (InvocationTargetException JavaDoc e)
111         {
112             freeTemporaryBlob(tempBlob);
113             throw new TypeCastException(value, this, e);
114         }
115         catch (ClassNotFoundException JavaDoc 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 JavaDoc tempBlob) throws TypeCastException
126     {
127         if (tempBlob == null)
128         {
129             return;
130         }
131
132         try
133         {
134             Method JavaDoc freeTemporaryMethod = tempBlob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]);
135             freeTemporaryMethod.invoke(tempBlob, new Object JavaDoc[0]);
136         }
137         catch (NoSuchMethodException JavaDoc e)
138         {
139             throw new TypeCastException("Error freeing Oracle BLOB", e);
140         }
141         catch (IllegalAccessException JavaDoc e)
142         {
143             throw new TypeCastException("Error freeing Oracle BLOB", e);
144         }
145         catch (InvocationTargetException JavaDoc e)
146         {
147             throw new TypeCastException("Error freeing Oracle BLOB", e.getTargetException());
148         }
149     }
150
151 }
152
Popular Tags