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&eacute;n</a>
033     * @version CVS $Id: ClobWrapper.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
034     */
035    public class ClobWrapper
036    {
037            protected Object m_clob;
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 getCharacterStream;
051            protected static Method getCharacterOutputStream;
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            {
061                    try
062                    {
063                            Class clobClass = ClassHelper.getClass("oracle.sql.CLOB", false);
064                            createTemporary = clobClass.getMethod("createTemporary", new Class[]{Connection.class, Boolean.TYPE, Integer.TYPE});
065                freeTemporary = clobClass.getMethod("freeTemporary", null);
066                            open = clobClass.getMethod("open", new Class[]{Integer.TYPE});
067                            isOpen = clobClass.getMethod("isOpen", null);
068                            getCharacterStream = clobClass.getMethod("getCharacterStream", null);
069                            getCharacterOutputStream = clobClass.getMethod("getCharacterOutputStream", null);
070                            getBufferSize = clobClass.getMethod("getBufferSize", null);
071                            close = clobClass.getMethod("close", null);
072                            trim = clobClass.getMethod("trim", new Class[]{Long.TYPE});
073    
074                durationSession = ClassHelper.getField(clobClass, "DURATION_SESSION");
075                durationCall = ClassHelper.getField(clobClass, "DURATION_CALL");
076                modeReadOnly = ClassHelper.getField(clobClass, "MODE_READONLY");
077                modeReadWrite = ClassHelper.getField(clobClass, "MODE_READWRITE");
078                    }
079                    catch (Exception ignore)
080                    {
081                // ignore it
082                    }
083            }
084    
085            public Object getClob()
086            {
087                    return m_clob;
088            }
089    
090            public void setClob(Object clob)
091            {
092                    m_clob = clob;
093            }
094    
095        protected static int staticIntFieldValue(Field field) {
096            int value = 0;
097            try {
098                value = field.getInt(null);
099            } 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    }