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.Reader; |
22 | |
import java.io.Writer; |
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 | |
public class ClobWrapper |
36 | |
{ |
37 | |
protected Object m_clob; |
38 | |
|
39 | |
|
40 | |
protected static Field durationSession; |
41 | |
protected static Field durationCall; |
42 | |
protected static Field modeReadOnly; |
43 | |
protected static Field modeReadWrite; |
44 | |
|
45 | |
|
46 | |
protected static Method createTemporary; |
47 | |
protected static Method freeTemporary; |
48 | |
protected static Method open; |
49 | |
protected static Method isOpen; |
50 | |
protected static Method getCharacterStream; |
51 | |
protected static Method getCharacterOutputStream; |
52 | |
protected static Method getBufferSize; |
53 | |
protected static Method close; |
54 | |
protected static Method trim; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
static |
60 | |
{ |
61 | |
try |
62 | |
{ |
63 | |
Class clobClass = ClassHelper.getClass("oracle.sql.CLOB", false); |
64 | |
createTemporary = clobClass.getMethod("createTemporary", new Class[]{Connection.class, Boolean.TYPE, Integer.TYPE}); |
65 | |
freeTemporary = clobClass.getMethod("freeTemporary", null); |
66 | |
open = clobClass.getMethod("open", new Class[]{Integer.TYPE}); |
67 | |
isOpen = clobClass.getMethod("isOpen", null); |
68 | |
getCharacterStream = clobClass.getMethod("getCharacterStream", null); |
69 | |
getCharacterOutputStream = clobClass.getMethod("getCharacterOutputStream", null); |
70 | |
getBufferSize = clobClass.getMethod("getBufferSize", null); |
71 | |
close = clobClass.getMethod("close", null); |
72 | |
trim = clobClass.getMethod("trim", new Class[]{Long.TYPE}); |
73 | |
|
74 | |
durationSession = ClassHelper.getField(clobClass, "DURATION_SESSION"); |
75 | |
durationCall = ClassHelper.getField(clobClass, "DURATION_CALL"); |
76 | |
modeReadOnly = ClassHelper.getField(clobClass, "MODE_READONLY"); |
77 | |
modeReadWrite = ClassHelper.getField(clobClass, "MODE_READWRITE"); |
78 | |
} |
79 | |
catch (Exception ignore) |
80 | |
{ |
81 | |
|
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public Object getClob() |
86 | |
{ |
87 | |
return m_clob; |
88 | |
} |
89 | |
|
90 | |
public void setClob(Object clob) |
91 | |
{ |
92 | |
m_clob = clob; |
93 | |
} |
94 | |
|
95 | |
protected static int staticIntFieldValue(Field field) { |
96 | |
int value = 0; |
97 | |
try { |
98 | |
value = field.getInt(null); |
99 | |
} catch (Exception ignore) { |
100 | |
value = -1; |
101 | |
} |
102 | |
return value; |
103 | |
} |
104 | |
|
105 | |
public static int getDurationSessionValue() { |
106 | |
return staticIntFieldValue(durationSession); |
107 | |
} |
108 | |
|
109 | |
public static int getDurationCallValue() { |
110 | |
return staticIntFieldValue(durationCall); |
111 | |
} |
112 | |
|
113 | |
public static int getModeReadOnlyValue() { |
114 | |
return staticIntFieldValue(modeReadOnly); |
115 | |
} |
116 | |
|
117 | |
public static int getModeReadWriteValue() { |
118 | |
return staticIntFieldValue(modeReadWrite); |
119 | |
} |
120 | |
|
121 | |
public static ClobWrapper createTemporary(Connection conn, boolean b, int i) throws Exception |
122 | |
{ |
123 | |
ClobWrapper retval = new ClobWrapper(); |
124 | |
|
125 | |
retval.setClob(createTemporary.invoke(null, new Object[]{conn, BooleanUtils.toBooleanObject(b), new Integer(i)})); |
126 | |
return retval; |
127 | |
} |
128 | |
|
129 | |
public void open(int i) throws SQLException |
130 | |
{ |
131 | |
if (m_clob == null) { |
132 | |
return; |
133 | |
} |
134 | |
try |
135 | |
{ |
136 | |
open.invoke(m_clob, new Object[]{new Integer(i)}); |
137 | |
} |
138 | |
catch (Throwable e) |
139 | |
{ |
140 | |
throw new SQLException(e.getMessage()); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
public boolean isOpen() throws SQLException |
145 | |
{ |
146 | |
if (m_clob == null) { |
147 | |
return false; |
148 | |
} |
149 | |
boolean clobOpen = false; |
150 | |
try |
151 | |
{ |
152 | |
Boolean retval = (Boolean) isOpen.invoke(m_clob, null); |
153 | |
if (retval != null) { |
154 | |
clobOpen = retval.booleanValue(); |
155 | |
} |
156 | |
} |
157 | |
catch (Throwable e) |
158 | |
{ |
159 | |
throw new SQLException(e.getMessage()); |
160 | |
} |
161 | |
return clobOpen; |
162 | |
} |
163 | |
|
164 | |
public Reader getCharacterStream() throws SQLException |
165 | |
{ |
166 | |
if (m_clob == null) { |
167 | |
return null; |
168 | |
} |
169 | |
Reader retval = null; |
170 | |
try |
171 | |
{ |
172 | |
retval = (Reader) getCharacterStream.invoke(m_clob, null); |
173 | |
} |
174 | |
catch (Throwable e) |
175 | |
{ |
176 | |
throw new SQLException(e.getMessage()); |
177 | |
} |
178 | |
return retval; |
179 | |
} |
180 | |
|
181 | |
public Writer getCharacterOutputStream() throws SQLException |
182 | |
{ |
183 | |
if (m_clob == null) { |
184 | |
return null; |
185 | |
} |
186 | |
Writer retval = null; |
187 | |
try |
188 | |
{ |
189 | |
retval = (Writer) getCharacterOutputStream.invoke(m_clob, null); |
190 | |
} |
191 | |
catch (Throwable e) |
192 | |
{ |
193 | |
throw new SQLException(e.getMessage()); |
194 | |
} |
195 | |
return retval; |
196 | |
} |
197 | |
|
198 | |
public int getBufferSize() throws SQLException |
199 | |
{ |
200 | |
if (m_clob == null) { |
201 | |
return 0; |
202 | |
} |
203 | |
Integer retval = null; |
204 | |
try |
205 | |
{ |
206 | |
retval = (Integer) getBufferSize.invoke(m_clob, null); |
207 | |
} |
208 | |
catch (Throwable e) |
209 | |
{ |
210 | |
throw new SQLException(e.getMessage()); |
211 | |
} |
212 | |
return retval.intValue(); |
213 | |
} |
214 | |
|
215 | |
public void close() throws SQLException |
216 | |
{ |
217 | |
if (m_clob == null) { |
218 | |
return; |
219 | |
} |
220 | |
try |
221 | |
{ |
222 | |
close.invoke(m_clob, null); |
223 | |
} |
224 | |
catch (Throwable e) |
225 | |
{ |
226 | |
throw new SQLException(e.getMessage()); |
227 | |
} |
228 | |
|
229 | |
} |
230 | |
|
231 | |
public void trim(long l) throws SQLException |
232 | |
{ |
233 | |
if (m_clob == null) { |
234 | |
return; |
235 | |
} |
236 | |
try |
237 | |
{ |
238 | |
trim.invoke(m_clob, new Object[]{new Long(l)}); |
239 | |
} |
240 | |
catch (Throwable e) |
241 | |
{ |
242 | |
throw new SQLException(e.getMessage()); |
243 | |
} |
244 | |
|
245 | |
} |
246 | |
|
247 | |
public void freeTemporary() throws SQLException |
248 | |
{ |
249 | |
if (m_clob == null) { |
250 | |
return; |
251 | |
} |
252 | |
try |
253 | |
{ |
254 | |
freeTemporary.invoke(m_clob, null); |
255 | |
} |
256 | |
catch (Throwable e) |
257 | |
{ |
258 | |
throw new SQLException(e.getMessage()); |
259 | |
} |
260 | |
} |
261 | |
} |