KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > platforms > BlobWrapper


1 package org.apache.ojb.broker.platforms;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.commons.lang.BooleanUtils;
19 import org.apache.ojb.broker.util.ClassHelper;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 /**
29  * Wraps the Oracle BLOB type and makes it accessible via reflection
30  * without having to import the Oracle Classes.
31  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
32  * @author <a HREF="mailto:erik@cj.com">Erik Forkalsrud</a>
33  * @author <a HREF="martin.kalen@curalia.se">Martin Kal&eacute;n</a>
34  * @version CVS $Id: BlobWrapper.java,v 1.7.2.1 2005/12/21 22:26:40 tomdz Exp $
35  */

36 public class BlobWrapper
37 {
38     protected Object JavaDoc m_blob;
39
40     // Fields - values must be looked up via reflection not be compile-time Oracle-version dependent
41
protected static Field JavaDoc durationSession;
42     protected static Field JavaDoc durationCall;
43     protected static Field JavaDoc modeReadOnly;
44     protected static Field JavaDoc modeReadWrite;
45
46     // Methods
47
protected static Method JavaDoc createTemporary;
48     protected static Method JavaDoc freeTemporary;
49     protected static Method JavaDoc open;
50     protected static Method JavaDoc isOpen;
51     protected static Method JavaDoc getBinaryStream;
52     protected static Method JavaDoc getBinaryOutputStream;
53     protected static Method JavaDoc getBufferSize;
54     protected static Method JavaDoc close;
55     protected static Method JavaDoc trim;
56
57     /**
58      * Initialize all methods and fields via reflection.
59      */

60     static
61     {
62         try
63         {
64             Class JavaDoc blobClass = ClassHelper.getClass("oracle.sql.BLOB", false);
65             createTemporary = blobClass.getMethod("createTemporary", new Class JavaDoc[]{Connection JavaDoc.class, Boolean.TYPE, Integer.TYPE});
66             freeTemporary = blobClass.getMethod("freeTemporary", null);
67             open = blobClass.getMethod("open", new Class JavaDoc[]{Integer.TYPE});
68             isOpen = blobClass.getMethod("isOpen", null);
69             getBinaryStream = blobClass.getMethod("getBinaryStream", null);
70             getBinaryOutputStream = blobClass.getMethod("getBinaryOutputStream", null);
71             getBufferSize = blobClass.getMethod("getBufferSize", null);
72             close = blobClass.getMethod("close", null);
73             trim = blobClass.getMethod("trim", new Class JavaDoc[]{Long.TYPE});
74
75             durationSession = ClassHelper.getField(blobClass, "DURATION_SESSION");
76             durationCall = ClassHelper.getField(blobClass, "DURATION_CALL");
77             modeReadOnly = ClassHelper.getField(blobClass, "MODE_READONLY");
78             modeReadWrite = ClassHelper.getField(blobClass, "MODE_READWRITE");
79         }
80         catch (Exception JavaDoc ignore)
81         {
82             // ignore it
83
}
84     }
85
86     public Object JavaDoc getBlob()
87     {
88         return m_blob;
89     }
90
91     public void setBlob(Object JavaDoc blob)
92     {
93         m_blob = blob;
94     }
95
96     protected static int staticIntFieldValue(Field JavaDoc field) {
97         int value = 0;
98         try {
99             value = field.getInt(null);
100         } catch (Exception JavaDoc ignore) {
101             value = -1;
102         }
103         return value;
104     }
105
106     public static int getDurationSessionValue() {
107         return staticIntFieldValue(durationSession);
108     }
109
110     public static int getDurationCallValue() {
111         return staticIntFieldValue(durationCall);
112     }
113
114     public static int getModeReadOnlyValue() {
115         return staticIntFieldValue(modeReadOnly);
116     }
117
118     public static int getModeReadWriteValue() {
119         return staticIntFieldValue(modeReadWrite);
120     }
121
122     public static BlobWrapper createTemporary(Connection JavaDoc conn, boolean b, int i) throws Exception JavaDoc
123     {
124         BlobWrapper retval = new BlobWrapper();
125         // Passing null to invoke static method
126
retval.setBlob(createTemporary.invoke(null, new Object JavaDoc[]{conn, BooleanUtils.toBooleanObject(b), new Integer JavaDoc(i)}));
127         return retval;
128     }
129
130     public void open(int i) throws SQLException JavaDoc
131     {
132         if (m_blob == null) {
133             return;
134         }
135         try
136         {
137             open.invoke(m_blob, new Object JavaDoc[]{new Integer JavaDoc(i)});
138         }
139         catch (Throwable JavaDoc e)
140         {
141             throw new SQLException JavaDoc(e.getMessage());
142         }
143     }
144
145     public boolean isOpen() throws SQLException JavaDoc
146     {
147         if (m_blob == null) {
148             return false;
149         }
150         
151         boolean blobOpen = false;
152         try
153         {
154             Boolean JavaDoc retval = (Boolean JavaDoc) isOpen.invoke(m_blob, null);
155             if (retval != null) {
156                 blobOpen = retval.booleanValue();
157             }
158         }
159         catch (Throwable JavaDoc e)
160         {
161             throw new SQLException JavaDoc(e.getMessage());
162         }
163         return blobOpen;
164     }
165
166     public InputStream JavaDoc getBinaryStream() throws SQLException JavaDoc
167     {
168         if (m_blob == null) {
169             return null;
170         }
171         InputStream JavaDoc retval = null;
172         try
173         {
174             retval = (InputStream JavaDoc) getBinaryStream.invoke(m_blob, null);
175         }
176         catch (Throwable JavaDoc e)
177         {
178             throw new SQLException JavaDoc(e.getMessage());
179         }
180         return retval;
181     }
182
183     public OutputStream JavaDoc getBinaryOutputStream() throws SQLException JavaDoc
184     {
185         if (m_blob == null) {
186             return null;
187         }
188         OutputStream JavaDoc retval = null;
189         try
190         {
191             retval = (OutputStream JavaDoc) getBinaryOutputStream.invoke(m_blob, null);
192         }
193         catch (Throwable JavaDoc e)
194         {
195             throw new SQLException JavaDoc(e.getMessage());
196         }
197         return retval;
198     }
199
200     public int getBufferSize() throws SQLException JavaDoc
201     {
202         if (m_blob == null) {
203             return 0;
204         }
205         Integer JavaDoc retval = null;
206         try
207         {
208             retval = (Integer JavaDoc) getBufferSize.invoke(m_blob, null);
209         }
210         catch (Throwable JavaDoc e)
211         {
212             throw new SQLException JavaDoc(e.getMessage());
213         }
214         return retval.intValue();
215     }
216
217     public void close() throws SQLException JavaDoc
218     {
219         if (m_blob == null) {
220             return;
221         }
222         try
223         {
224             close.invoke(m_blob, null);
225         }
226         catch (Throwable JavaDoc e)
227         {
228             throw new SQLException JavaDoc(e.getMessage());
229         }
230
231     }
232
233     public void trim(long l) throws SQLException JavaDoc
234     {
235         if (m_blob == null) {
236             return;
237         }
238         try
239         {
240             trim.invoke(m_blob, new Object JavaDoc[]{new Long JavaDoc(l)});
241         }
242         catch (Throwable JavaDoc e)
243         {
244             throw new SQLException JavaDoc(e.getMessage());
245         }
246
247     }
248
249     public void freeTemporary() throws SQLException JavaDoc
250     {
251         if (m_blob == null) {
252             return;
253         }
254         try
255         {
256             freeTemporary.invoke(m_blob, null);
257         }
258         catch (Throwable JavaDoc e)
259         {
260             throw new SQLException JavaDoc(e.getMessage());
261         }
262     }
263
264 }
265
Popular Tags