Coverage Report - org.apache.ojb.broker.platforms.ClobWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ClobWrapper
N/A
N/A
3.125
 
 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;
 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  
  * 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.1 2007-08-24 22:17:35 ewestfal Exp $
 34  
  */
 35  
 public class ClobWrapper
 36  
 {
 37  
         protected Object m_clob;
 38  
 
 39  
     // Fields - values must be looked up via reflection not be compile-time Oracle-version dependent
 40  
     protected static Field durationSession;
 41  
     protected static Field durationCall;
 42  
     protected static Field modeReadOnly;
 43  
     protected static Field modeReadWrite;
 44  
 
 45  
         // Methods
 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  
          * Initialize all methods and fields via reflection.
 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  
             // ignore it
 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  
         // Passing null to invoke static method
 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  
 }