1 package org.apache.ojb.broker.platforms;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import org.apache.commons.lang.BooleanUtils;
19 import org.apache.ojb.broker.util.ClassHelper;
20
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Field;
25 import java.sql.Connection;
26 import java.sql.SQLException;
27
28
29
30
31
32
33
34
35
36 public class BlobWrapper
37 {
38 protected Object m_blob;
39
40
41 protected static Field durationSession;
42 protected static Field durationCall;
43 protected static Field modeReadOnly;
44 protected static Field modeReadWrite;
45
46
47 protected static Method createTemporary;
48 protected static Method freeTemporary;
49 protected static Method open;
50 protected static Method isOpen;
51 protected static Method getBinaryStream;
52 protected static Method getBinaryOutputStream;
53 protected static Method getBufferSize;
54 protected static Method close;
55 protected static Method trim;
56
57
58
59
60 static
61 {
62 try
63 {
64 Class blobClass = ClassHelper.getClass("oracle.sql.BLOB", false);
65 createTemporary = blobClass.getMethod("createTemporary", new Class[]{Connection.class, Boolean.TYPE, Integer.TYPE});
66 freeTemporary = blobClass.getMethod("freeTemporary", null);
67 open = blobClass.getMethod("open", new Class[]{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[]{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 ignore)
81 {
82
83 }
84 }
85
86 public Object getBlob()
87 {
88 return m_blob;
89 }
90
91 public void setBlob(Object blob)
92 {
93 m_blob = blob;
94 }
95
96 protected static int staticIntFieldValue(Field field) {
97 int value = 0;
98 try {
99 value = field.getInt(null);
100 } catch (Exception 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 conn, boolean b, int i) throws Exception
123 {
124 BlobWrapper retval = new BlobWrapper();
125
126 retval.setBlob(createTemporary.invoke(null, new Object[]{conn, BooleanUtils.toBooleanObject(b), new Integer(i)}));
127 return retval;
128 }
129
130 public void open(int i) throws SQLException
131 {
132 if (m_blob == null) {
133 return;
134 }
135 try
136 {
137 open.invoke(m_blob, new Object[]{new Integer(i)});
138 }
139 catch (Throwable e)
140 {
141 throw new SQLException(e.getMessage());
142 }
143 }
144
145 public boolean isOpen() throws SQLException
146 {
147 if (m_blob == null) {
148 return false;
149 }
150
151 boolean blobOpen = false;
152 try
153 {
154 Boolean retval = (Boolean) isOpen.invoke(m_blob, null);
155 if (retval != null) {
156 blobOpen = retval.booleanValue();
157 }
158 }
159 catch (Throwable e)
160 {
161 throw new SQLException(e.getMessage());
162 }
163 return blobOpen;
164 }
165
166 public InputStream getBinaryStream() throws SQLException
167 {
168 if (m_blob == null) {
169 return null;
170 }
171 InputStream retval = null;
172 try
173 {
174 retval = (InputStream) getBinaryStream.invoke(m_blob, null);
175 }
176 catch (Throwable e)
177 {
178 throw new SQLException(e.getMessage());
179 }
180 return retval;
181 }
182
183 public OutputStream getBinaryOutputStream() throws SQLException
184 {
185 if (m_blob == null) {
186 return null;
187 }
188 OutputStream retval = null;
189 try
190 {
191 retval = (OutputStream) getBinaryOutputStream.invoke(m_blob, null);
192 }
193 catch (Throwable e)
194 {
195 throw new SQLException(e.getMessage());
196 }
197 return retval;
198 }
199
200 public int getBufferSize() throws SQLException
201 {
202 if (m_blob == null) {
203 return 0;
204 }
205 Integer retval = null;
206 try
207 {
208 retval = (Integer) getBufferSize.invoke(m_blob, null);
209 }
210 catch (Throwable e)
211 {
212 throw new SQLException(e.getMessage());
213 }
214 return retval.intValue();
215 }
216
217 public void close() throws SQLException
218 {
219 if (m_blob == null) {
220 return;
221 }
222 try
223 {
224 close.invoke(m_blob, null);
225 }
226 catch (Throwable e)
227 {
228 throw new SQLException(e.getMessage());
229 }
230
231 }
232
233 public void trim(long l) throws SQLException
234 {
235 if (m_blob == null) {
236 return;
237 }
238 try
239 {
240 trim.invoke(m_blob, new Object[]{new Long(l)});
241 }
242 catch (Throwable e)
243 {
244 throw new SQLException(e.getMessage());
245 }
246
247 }
248
249 public void freeTemporary() throws SQLException
250 {
251 if (m_blob == null) {
252 return;
253 }
254 try
255 {
256 freeTemporary.invoke(m_blob, null);
257 }
258 catch (Throwable e)
259 {
260 throw new SQLException(e.getMessage());
261 }
262 }
263
264 }