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.1 2007-08-24 22:17:35 ewestfal Exp $
035 */
036 public class BlobWrapper
037 {
038 protected Object m_blob;
039
040 // Fields - values must be looked up via reflection not be compile-time Oracle-version dependent
041 protected static Field durationSession;
042 protected static Field durationCall;
043 protected static Field modeReadOnly;
044 protected static Field modeReadWrite;
045
046 // Methods
047 protected static Method createTemporary;
048 protected static Method freeTemporary;
049 protected static Method open;
050 protected static Method isOpen;
051 protected static Method getBinaryStream;
052 protected static Method getBinaryOutputStream;
053 protected static Method getBufferSize;
054 protected static Method close;
055 protected static Method trim;
056
057 /**
058 * Initialize all methods and fields via reflection.
059 */
060 static
061 {
062 try
063 {
064 Class blobClass = ClassHelper.getClass("oracle.sql.BLOB", false);
065 createTemporary = blobClass.getMethod("createTemporary", new Class[]{Connection.class, Boolean.TYPE, Integer.TYPE});
066 freeTemporary = blobClass.getMethod("freeTemporary", null);
067 open = blobClass.getMethod("open", new Class[]{Integer.TYPE});
068 isOpen = blobClass.getMethod("isOpen", null);
069 getBinaryStream = blobClass.getMethod("getBinaryStream", null);
070 getBinaryOutputStream = blobClass.getMethod("getBinaryOutputStream", null);
071 getBufferSize = blobClass.getMethod("getBufferSize", null);
072 close = blobClass.getMethod("close", null);
073 trim = blobClass.getMethod("trim", new Class[]{Long.TYPE});
074
075 durationSession = ClassHelper.getField(blobClass, "DURATION_SESSION");
076 durationCall = ClassHelper.getField(blobClass, "DURATION_CALL");
077 modeReadOnly = ClassHelper.getField(blobClass, "MODE_READONLY");
078 modeReadWrite = ClassHelper.getField(blobClass, "MODE_READWRITE");
079 }
080 catch (Exception ignore)
081 {
082 // ignore it
083 }
084 }
085
086 public Object getBlob()
087 {
088 return m_blob;
089 }
090
091 public void setBlob(Object blob)
092 {
093 m_blob = blob;
094 }
095
096 protected static int staticIntFieldValue(Field field) {
097 int value = 0;
098 try {
099 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 // Passing null to invoke static method
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 }