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.InputStream;
022: import java.io.OutputStream;
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 BLOB 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="mailto:erik@cj.com">Erik Forkalsrud</a>
033: * @author <a href="martin.kalen@curalia.se">Martin Kalén</a>
034: * @version CVS $Id: BlobWrapper.java,v 1.7.2.1 2005/12/21 22:26:40 tomdz Exp $
035: */
036: public class BlobWrapper {
037: protected Object m_blob;
038:
039: // Fields - values must be looked up via reflection not be compile-time Oracle-version dependent
040: protected static Field durationSession;
041: protected static Field durationCall;
042: protected static Field modeReadOnly;
043: protected static Field modeReadWrite;
044:
045: // Methods
046: protected static Method createTemporary;
047: protected static Method freeTemporary;
048: protected static Method open;
049: protected static Method isOpen;
050: protected static Method getBinaryStream;
051: protected static Method getBinaryOutputStream;
052: protected static Method getBufferSize;
053: protected static Method close;
054: protected static Method trim;
055:
056: /**
057: * Initialize all methods and fields via reflection.
058: */
059: static {
060: try {
061: Class blobClass = ClassHelper.getClass("oracle.sql.BLOB",
062: false);
063: createTemporary = blobClass.getMethod("createTemporary",
064: new Class[] { Connection.class, Boolean.TYPE,
065: Integer.TYPE });
066: freeTemporary = blobClass.getMethod("freeTemporary", null);
067: open = blobClass.getMethod("open",
068: new Class[] { Integer.TYPE });
069: isOpen = blobClass.getMethod("isOpen", null);
070: getBinaryStream = blobClass.getMethod("getBinaryStream",
071: null);
072: getBinaryOutputStream = blobClass.getMethod(
073: "getBinaryOutputStream", null);
074: getBufferSize = blobClass.getMethod("getBufferSize", null);
075: close = blobClass.getMethod("close", null);
076: trim = blobClass.getMethod("trim",
077: new Class[] { Long.TYPE });
078:
079: durationSession = ClassHelper.getField(blobClass,
080: "DURATION_SESSION");
081: durationCall = ClassHelper.getField(blobClass,
082: "DURATION_CALL");
083: modeReadOnly = ClassHelper.getField(blobClass,
084: "MODE_READONLY");
085: modeReadWrite = ClassHelper.getField(blobClass,
086: "MODE_READWRITE");
087: } catch (Exception ignore) {
088: // ignore it
089: }
090: }
091:
092: public Object getBlob() {
093: return m_blob;
094: }
095:
096: public void setBlob(Object blob) {
097: m_blob = blob;
098: }
099:
100: protected static int staticIntFieldValue(Field field) {
101: int value = 0;
102: try {
103: value = field.getInt(null);
104: } catch (Exception ignore) {
105: value = -1;
106: }
107: return value;
108: }
109:
110: public static int getDurationSessionValue() {
111: return staticIntFieldValue(durationSession);
112: }
113:
114: public static int getDurationCallValue() {
115: return staticIntFieldValue(durationCall);
116: }
117:
118: public static int getModeReadOnlyValue() {
119: return staticIntFieldValue(modeReadOnly);
120: }
121:
122: public static int getModeReadWriteValue() {
123: return staticIntFieldValue(modeReadWrite);
124: }
125:
126: public static BlobWrapper createTemporary(Connection conn,
127: boolean b, int i) throws Exception {
128: BlobWrapper retval = new BlobWrapper();
129: // Passing null to invoke static method
130: retval.setBlob(createTemporary
131: .invoke(null,
132: new Object[] { conn,
133: BooleanUtils.toBooleanObject(b),
134: new Integer(i) }));
135: return retval;
136: }
137:
138: public void open(int i) throws SQLException {
139: if (m_blob == null) {
140: return;
141: }
142: try {
143: open.invoke(m_blob, new Object[] { new Integer(i) });
144: } catch (Throwable e) {
145: throw new SQLException(e.getMessage());
146: }
147: }
148:
149: public boolean isOpen() throws SQLException {
150: if (m_blob == null) {
151: return false;
152: }
153:
154: boolean blobOpen = false;
155: try {
156: Boolean retval = (Boolean) isOpen.invoke(m_blob, null);
157: if (retval != null) {
158: blobOpen = retval.booleanValue();
159: }
160: } catch (Throwable e) {
161: throw new SQLException(e.getMessage());
162: }
163: return blobOpen;
164: }
165:
166: public InputStream getBinaryStream() throws SQLException {
167: if (m_blob == null) {
168: return null;
169: }
170: InputStream retval = null;
171: try {
172: retval = (InputStream) getBinaryStream.invoke(m_blob, null);
173: } catch (Throwable e) {
174: throw new SQLException(e.getMessage());
175: }
176: return retval;
177: }
178:
179: public OutputStream getBinaryOutputStream() throws SQLException {
180: if (m_blob == null) {
181: return null;
182: }
183: OutputStream retval = null;
184: try {
185: retval = (OutputStream) getBinaryOutputStream.invoke(
186: m_blob, null);
187: } catch (Throwable e) {
188: throw new SQLException(e.getMessage());
189: }
190: return retval;
191: }
192:
193: public int getBufferSize() throws SQLException {
194: if (m_blob == null) {
195: return 0;
196: }
197: Integer retval = null;
198: try {
199: retval = (Integer) getBufferSize.invoke(m_blob, null);
200: } catch (Throwable e) {
201: throw new SQLException(e.getMessage());
202: }
203: return retval.intValue();
204: }
205:
206: public void close() throws SQLException {
207: if (m_blob == null) {
208: return;
209: }
210: try {
211: close.invoke(m_blob, null);
212: } catch (Throwable e) {
213: throw new SQLException(e.getMessage());
214: }
215:
216: }
217:
218: public void trim(long l) throws SQLException {
219: if (m_blob == null) {
220: return;
221: }
222: try {
223: trim.invoke(m_blob, new Object[] { new Long(l) });
224: } catch (Throwable e) {
225: throw new SQLException(e.getMessage());
226: }
227:
228: }
229:
230: public void freeTemporary() throws SQLException {
231: if (m_blob == null) {
232: return;
233: }
234: try {
235: freeTemporary.invoke(m_blob, null);
236: } catch (Throwable e) {
237: throw new SQLException(e.getMessage());
238: }
239: }
240:
241: }
|