KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
22 import java.io.Writer 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 CLOB 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="martin.kalen@curalia.se">Martin Kal&eacute;n</a>
33  * @version CVS $Id: ClobWrapper.java,v 1.10.2.1 2005/12/21 22:26:40 tomdz Exp $
34  */

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

59     static
60     {
61         try
62         {
63             Class JavaDoc clobClass = ClassHelper.getClass("oracle.sql.CLOB", false);
64             createTemporary = clobClass.getMethod("createTemporary", new Class JavaDoc[]{Connection JavaDoc.class, Boolean.TYPE, Integer.TYPE});
65             freeTemporary = clobClass.getMethod("freeTemporary", null);
66             open = clobClass.getMethod("open", new Class JavaDoc[]{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 JavaDoc[]{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 JavaDoc ignore)
80         {
81             // ignore it
82
}
83     }
84
85     public Object JavaDoc getClob()
86     {
87         return m_clob;
88     }
89
90     public void setClob(Object JavaDoc clob)
91     {
92         m_clob = clob;
93     }
94
95     protected static int staticIntFieldValue(Field JavaDoc field) {
96         int value = 0;
97         try {
98             value = field.getInt(null);
99         } catch (Exception JavaDoc 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 JavaDoc conn, boolean b, int i) throws Exception JavaDoc
122     {
123         ClobWrapper retval = new ClobWrapper();
124         // Passing null to invoke static method
125
retval.setClob(createTemporary.invoke(null, new Object JavaDoc[]{conn, BooleanUtils.toBooleanObject(b), new Integer JavaDoc(i)}));
126         return retval;
127     }
128
129     public void open(int i) throws SQLException JavaDoc
130     {
131         if (m_clob == null) {
132             return;
133         }
134         try
135         {
136             open.invoke(m_clob, new Object JavaDoc[]{new Integer JavaDoc(i)});
137         }
138         catch (Throwable JavaDoc e)
139         {
140             throw new SQLException JavaDoc(e.getMessage());
141         }
142     }
143
144     public boolean isOpen() throws SQLException JavaDoc
145     {
146         if (m_clob == null) {
147             return false;
148         }
149         boolean clobOpen = false;
150         try
151         {
152             Boolean JavaDoc retval = (Boolean JavaDoc) isOpen.invoke(m_clob, null);
153             if (retval != null) {
154                 clobOpen = retval.booleanValue();
155             }
156         }
157         catch (Throwable JavaDoc e)
158         {
159             throw new SQLException JavaDoc(e.getMessage());
160         }
161         return clobOpen;
162     }
163
164     public Reader JavaDoc getCharacterStream() throws SQLException JavaDoc
165     {
166         if (m_clob == null) {
167             return null;
168         }
169         Reader JavaDoc retval = null;
170         try
171         {
172             retval = (Reader JavaDoc) getCharacterStream.invoke(m_clob, null);
173         }
174         catch (Throwable JavaDoc e)
175         {
176             throw new SQLException JavaDoc(e.getMessage());
177         }
178         return retval;
179     }
180
181     public Writer JavaDoc getCharacterOutputStream() throws SQLException JavaDoc
182     {
183         if (m_clob == null) {
184             return null;
185         }
186         Writer JavaDoc retval = null;
187         try
188         {
189             retval = (Writer JavaDoc) getCharacterOutputStream.invoke(m_clob, null);
190         }
191         catch (Throwable JavaDoc e)
192         {
193             throw new SQLException JavaDoc(e.getMessage());
194         }
195         return retval;
196     }
197
198     public int getBufferSize() throws SQLException JavaDoc
199     {
200         if (m_clob == null) {
201             return 0;
202         }
203         Integer JavaDoc retval = null;
204         try
205         {
206             retval = (Integer JavaDoc) getBufferSize.invoke(m_clob, null);
207         }
208         catch (Throwable JavaDoc e)
209         {
210             throw new SQLException JavaDoc(e.getMessage());
211         }
212         return retval.intValue();
213     }
214
215     public void close() throws SQLException JavaDoc
216     {
217         if (m_clob == null) {
218             return;
219         }
220         try
221         {
222             close.invoke(m_clob, null);
223         }
224         catch (Throwable JavaDoc e)
225         {
226             throw new SQLException JavaDoc(e.getMessage());
227         }
228
229     }
230
231     public void trim(long l) throws SQLException JavaDoc
232     {
233         if (m_clob == null) {
234             return;
235         }
236         try
237         {
238             trim.invoke(m_clob, new Object JavaDoc[]{new Long JavaDoc(l)});
239         }
240         catch (Throwable JavaDoc e)
241         {
242             throw new SQLException JavaDoc(e.getMessage());
243         }
244
245     }
246
247     public void freeTemporary() throws SQLException JavaDoc
248     {
249         if (m_clob == null) {
250             return;
251         }
252         try
253         {
254             freeTemporary.invoke(m_clob, null);
255         }
256         catch (Throwable JavaDoc e)
257         {
258             throw new SQLException JavaDoc(e.getMessage());
259         }
260     }
261 }
262
Popular Tags