001: package org.apache.ojb.broker.platforms;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.BooleanUtils;
019: import org.apache.ojb.broker.util.ClassHelper;
020:
021: import java.io.Reader;
022: import java.io.Writer;
023: import java.lang.reflect.Method;
024: import java.lang.reflect.Field;
025: import java.sql.Connection;
026: import java.sql.SQLException;
027:
028: /**
029: * Wraps the Oracle CLOB type and makes it accessible via reflection
030: * without having to import the Oracle Classes.
031: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
032: * @author <a href="martin.kalen@curalia.se">Martin Kalén</a>
033: * @version CVS $Id: ClobWrapper.java,v 1.10.2.1 2005/12/21 22:26:40 tomdz Exp $
034: */
035: public class ClobWrapper {
036: protected Object m_clob;
037:
038: // Fields - values must be looked up via reflection not be compile-time Oracle-version dependent
039: protected static Field durationSession;
040: protected static Field durationCall;
041: protected static Field modeReadOnly;
042: protected static Field modeReadWrite;
043:
044: // Methods
045: protected static Method createTemporary;
046: protected static Method freeTemporary;
047: protected static Method open;
048: protected static Method isOpen;
049: protected static Method getCharacterStream;
050: protected static Method getCharacterOutputStream;
051: protected static Method getBufferSize;
052: protected static Method close;
053: protected static Method trim;
054:
055: /**
056: * Initialize all methods and fields via reflection.
057: */
058: static {
059: try {
060: Class clobClass = ClassHelper.getClass("oracle.sql.CLOB",
061: false);
062: createTemporary = clobClass.getMethod("createTemporary",
063: new Class[] { Connection.class, Boolean.TYPE,
064: Integer.TYPE });
065: freeTemporary = clobClass.getMethod("freeTemporary", null);
066: open = clobClass.getMethod("open",
067: new Class[] { Integer.TYPE });
068: isOpen = clobClass.getMethod("isOpen", null);
069: getCharacterStream = clobClass.getMethod(
070: "getCharacterStream", null);
071: getCharacterOutputStream = clobClass.getMethod(
072: "getCharacterOutputStream", null);
073: getBufferSize = clobClass.getMethod("getBufferSize", null);
074: close = clobClass.getMethod("close", null);
075: trim = clobClass.getMethod("trim",
076: new Class[] { Long.TYPE });
077:
078: durationSession = ClassHelper.getField(clobClass,
079: "DURATION_SESSION");
080: durationCall = ClassHelper.getField(clobClass,
081: "DURATION_CALL");
082: modeReadOnly = ClassHelper.getField(clobClass,
083: "MODE_READONLY");
084: modeReadWrite = ClassHelper.getField(clobClass,
085: "MODE_READWRITE");
086: } catch (Exception ignore) {
087: // ignore it
088: }
089: }
090:
091: public Object getClob() {
092: return m_clob;
093: }
094:
095: public void setClob(Object clob) {
096: m_clob = clob;
097: }
098:
099: protected static int staticIntFieldValue(Field field) {
100: int value = 0;
101: try {
102: value = field.getInt(null);
103: } catch (Exception ignore) {
104: value = -1;
105: }
106: return value;
107: }
108:
109: public static int getDurationSessionValue() {
110: return staticIntFieldValue(durationSession);
111: }
112:
113: public static int getDurationCallValue() {
114: return staticIntFieldValue(durationCall);
115: }
116:
117: public static int getModeReadOnlyValue() {
118: return staticIntFieldValue(modeReadOnly);
119: }
120:
121: public static int getModeReadWriteValue() {
122: return staticIntFieldValue(modeReadWrite);
123: }
124:
125: public static ClobWrapper createTemporary(Connection conn,
126: boolean b, int i) throws Exception {
127: ClobWrapper retval = new ClobWrapper();
128: // Passing null to invoke static method
129: retval.setClob(createTemporary
130: .invoke(null,
131: new Object[] { conn,
132: BooleanUtils.toBooleanObject(b),
133: new Integer(i) }));
134: return retval;
135: }
136:
137: public void open(int i) throws SQLException {
138: if (m_clob == null) {
139: return;
140: }
141: try {
142: open.invoke(m_clob, new Object[] { new Integer(i) });
143: } catch (Throwable e) {
144: throw new SQLException(e.getMessage());
145: }
146: }
147:
148: public boolean isOpen() throws SQLException {
149: if (m_clob == null) {
150: return false;
151: }
152: boolean clobOpen = false;
153: try {
154: Boolean retval = (Boolean) isOpen.invoke(m_clob, null);
155: if (retval != null) {
156: clobOpen = retval.booleanValue();
157: }
158: } catch (Throwable e) {
159: throw new SQLException(e.getMessage());
160: }
161: return clobOpen;
162: }
163:
164: public Reader getCharacterStream() throws SQLException {
165: if (m_clob == null) {
166: return null;
167: }
168: Reader retval = null;
169: try {
170: retval = (Reader) getCharacterStream.invoke(m_clob, null);
171: } catch (Throwable e) {
172: throw new SQLException(e.getMessage());
173: }
174: return retval;
175: }
176:
177: public Writer getCharacterOutputStream() throws SQLException {
178: if (m_clob == null) {
179: return null;
180: }
181: Writer retval = null;
182: try {
183: retval = (Writer) getCharacterOutputStream.invoke(m_clob,
184: null);
185: } catch (Throwable e) {
186: throw new SQLException(e.getMessage());
187: }
188: return retval;
189: }
190:
191: public int getBufferSize() throws SQLException {
192: if (m_clob == null) {
193: return 0;
194: }
195: Integer retval = null;
196: try {
197: retval = (Integer) getBufferSize.invoke(m_clob, null);
198: } catch (Throwable e) {
199: throw new SQLException(e.getMessage());
200: }
201: return retval.intValue();
202: }
203:
204: public void close() throws SQLException {
205: if (m_clob == null) {
206: return;
207: }
208: try {
209: close.invoke(m_clob, null);
210: } catch (Throwable e) {
211: throw new SQLException(e.getMessage());
212: }
213:
214: }
215:
216: public void trim(long l) throws SQLException {
217: if (m_clob == null) {
218: return;
219: }
220: try {
221: trim.invoke(m_clob, new Object[] { new Long(l) });
222: } catch (Throwable e) {
223: throw new SQLException(e.getMessage());
224: }
225:
226: }
227:
228: public void freeTemporary() throws SQLException {
229: if (m_clob == null) {
230: return;
231: }
232: try {
233: freeTemporary.invoke(m_clob, null);
234: } catch (Throwable e) {
235: throw new SQLException(e.getMessage());
236: }
237: }
238: }
|