001    package org.apache.ojb.broker.util;
002    
003    /* Copyright 2002-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 java.sql.Array;
019    import java.sql.Blob;
020    import java.sql.CallableStatement;
021    import java.sql.Clob;
022    import java.sql.Connection;
023    import java.sql.DatabaseMetaData;
024    import java.sql.NClob;
025    import java.sql.PreparedStatement;
026    import java.sql.SQLClientInfoException;
027    import java.sql.SQLException;
028    import java.sql.SQLWarning;
029    import java.sql.SQLXML;
030    import java.sql.Statement;
031    import java.sql.Struct;
032    import java.util.Map;
033    import java.util.Properties;
034    
035    import org.apache.commons.lang.builder.ToStringBuilder;
036    import org.apache.commons.lang.builder.ToStringStyle;
037    
038    /**
039     * Wrapper class for connections.
040     * Simplified version of {@link org.apache.commons.dbcp.DelegatingConnection}
041     */
042    public class WrappedConnection implements Connection
043    {
044        private Connection _conn = null;
045        private boolean _isClosed = false;
046    
047        public WrappedConnection(Connection c)
048        {
049            _conn = c;
050        }
051    
052        /**
053         * Returns my underlying {@link Connection}.
054         * @return my underlying {@link Connection}.
055         */
056        public Connection getDelegate()
057        {
058            return _conn;
059        }
060    
061        /**
062         * If my underlying <tt>Connection</tt> is not a
063         * <tt>WrappedConnection</tt>, returns it,
064         * otherwise recursively invokes this method on
065         * my delegate.
066         * <p>
067         * Hence this method will return the first
068         * delegate that is not a <tt>WrappedConnection</tt>,
069         * or <tt>null</tt> when no non-<tt>WrappedConnection</tt>
070         * delegate can be found by transversing this chain.
071         * <p>
072         * This method is useful when you may have nested
073         * <tt>WrappedConnection</tt>s, and you want to make
074         * sure to obtain a "genuine" {@link java.sql.Connection}.
075         */
076        public Connection getInnermostDelegate()
077        {
078            Connection c = _conn;
079            while (c != null && c instanceof WrappedConnection)
080            {
081                c = ((WrappedConnection) c).getDelegate();
082                if (this == c)
083                {
084                    return null;
085                }
086            }
087            return c;
088        }
089    
090        /** Sets my delegate. */
091        public void setDelegate(Connection c)
092        {
093            _conn = c;
094        }
095    
096        protected void checkOpen() throws SQLException
097        {
098            if (_isClosed)
099            {
100                throw new SQLException("Connection is closed. " + this);
101            }
102        }
103    
104        /**
105         * Activate the connection
106         */
107        public void activateConnection()
108        {
109            _isClosed = false;
110            if (_conn instanceof WrappedConnection)
111            {
112                ((WrappedConnection) _conn).activateConnection();
113            }
114        }
115    
116        /**
117         * Passivate the connection
118         */
119        public void passivateConnection() throws SQLException
120        {
121            _isClosed = true;
122            if (_conn instanceof WrappedConnection)
123            {
124                ((WrappedConnection) _conn).passivateConnection();
125            }
126        }
127    
128        public String toString()
129        {
130            return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
131                    .append("wrapped connection", (_conn != null ? _conn.toString() : null))
132                    .toString();
133        }
134    
135        /**
136         * Closes the underlying connection, and close
137         * any Statements that were not explicitly closed.
138         */
139        public void close() throws SQLException
140        {
141            passivateConnection();
142            _conn.close();
143        }
144    
145        public boolean isClosed() throws SQLException
146        {
147            if (_isClosed || _conn.isClosed())
148            {
149                return true;
150            }
151            return false;
152        }
153    
154        public Statement createStatement() throws SQLException
155        {
156            checkOpen();
157            return _conn.createStatement();
158        }
159    
160        public Statement createStatement(int resultSetType,
161                                         int resultSetConcurrency)
162                throws SQLException
163        {
164            checkOpen();
165            return _conn.createStatement(resultSetType, resultSetConcurrency);
166        }
167    
168        public PreparedStatement prepareStatement(String sql) throws SQLException
169        {
170            checkOpen();
171            return _conn.prepareStatement(sql);
172        }
173    
174        public PreparedStatement prepareStatement(String sql,
175                                                  int resultSetType,
176                                                  int resultSetConcurrency)
177                throws SQLException
178        {
179            checkOpen();
180            return _conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
181        }
182    
183        public CallableStatement prepareCall(String sql) throws SQLException
184        {
185            checkOpen();
186            return _conn.prepareCall(sql);
187        }
188    
189        public CallableStatement prepareCall(String sql,
190                                             int resultSetType,
191                                             int resultSetConcurrency)
192                throws SQLException
193        {
194            checkOpen();
195            return _conn.prepareCall(sql, resultSetType, resultSetConcurrency);
196        }
197    
198        public void clearWarnings() throws SQLException
199        {
200            checkOpen();
201            _conn.clearWarnings();
202        }
203    
204        public void commit() throws SQLException
205        {
206            checkOpen();
207            _conn.commit();
208        }
209    
210        public boolean getAutoCommit() throws SQLException
211        {
212            checkOpen();
213            return _conn.getAutoCommit();
214        }
215    
216        public String getCatalog() throws SQLException
217        {
218            checkOpen();
219            return _conn.getCatalog();
220        }
221    
222        public DatabaseMetaData getMetaData() throws SQLException
223        {
224            checkOpen();
225            return _conn.getMetaData();
226        }
227    
228        public int getTransactionIsolation() throws SQLException
229        {
230            checkOpen();
231            return _conn.getTransactionIsolation();
232        }
233    
234        public Map getTypeMap() throws SQLException
235        {
236            checkOpen();
237            return _conn.getTypeMap();
238        }
239    
240        public SQLWarning getWarnings() throws SQLException
241        {
242            checkOpen();
243            return _conn.getWarnings();
244        }
245    
246        public boolean isReadOnly() throws SQLException
247        {
248            checkOpen();
249            return _conn.isReadOnly();
250        }
251    
252        public String nativeSQL(String sql) throws SQLException
253        {
254            checkOpen();
255            return _conn.nativeSQL(sql);
256        }
257    
258        public void rollback() throws SQLException
259        {
260            checkOpen();
261            _conn.rollback();
262        }
263    
264        public void setAutoCommit(boolean autoCommit) throws SQLException
265        {
266            checkOpen();
267            _conn.setAutoCommit(autoCommit);
268        }
269    
270        public void setCatalog(String catalog) throws SQLException
271        {
272            checkOpen();
273            _conn.setCatalog(catalog);
274        }
275    
276        public void setReadOnly(boolean readOnly) throws SQLException
277        {
278            checkOpen();
279            _conn.setReadOnly(readOnly);
280        }
281    
282        public void setTransactionIsolation(int level) throws SQLException
283        {
284            checkOpen();
285            _conn.setTransactionIsolation(level);
286        }
287    
288        @SuppressWarnings("unchecked")
289            public void setTypeMap(@SuppressWarnings("rawtypes") Map map) throws SQLException
290        {
291            checkOpen();
292            _conn.setTypeMap(map);
293        }
294    
295        @Override
296            public boolean isWrapperFor(Class<?> arg0) throws SQLException {
297                    checkOpen();
298                    return _conn.isWrapperFor(arg0);
299            }
300    
301            @Override
302            public <T> T unwrap(Class<T> arg0) throws SQLException {
303                    checkOpen();
304                    return _conn.unwrap(arg0);
305            }
306    
307            @Override
308            public Array createArrayOf(String typeName, Object[] elements)
309                            throws SQLException {
310                    checkOpen();
311                    return _conn.createArrayOf(typeName, elements);
312            }
313    
314            @Override
315            public Blob createBlob() throws SQLException {
316                    checkOpen();
317                    return _conn.createBlob();
318            }
319    
320            @Override
321            public Clob createClob() throws SQLException {
322                    checkOpen();
323                    return _conn.createClob();
324            }
325    
326            @Override
327            public NClob createNClob() throws SQLException {
328                    checkOpen();
329                    return _conn.createNClob();
330            }
331    
332            @Override
333            public SQLXML createSQLXML() throws SQLException {
334                    checkOpen();
335                    return _conn.createSQLXML();
336            }
337    
338            @Override
339            public Struct createStruct(String typeName, Object[] attributes)
340                            throws SQLException {
341                    checkOpen();
342                    return _conn.createStruct(typeName, attributes);
343            }
344    
345            @Override
346            public Properties getClientInfo() throws SQLException {
347                    checkOpen();
348                    return _conn.getClientInfo();
349            }
350    
351            @Override
352            public String getClientInfo(String name) throws SQLException {
353                    checkOpen();
354                    return _conn.getClientInfo(name);
355            }
356    
357            @Override
358            public boolean isValid(int timeout) throws SQLException {
359                    checkOpen();
360                    return _conn.isValid(timeout);
361            }
362    
363            @Override
364            public void setClientInfo(Properties properties)
365                            throws SQLClientInfoException {
366                     _conn.setClientInfo(properties);
367                    
368            }
369    
370            @Override
371            public void setClientInfo(String name, String value)
372                            throws SQLClientInfoException {
373                    _conn.setClientInfo(name, value);
374                    
375            }
376    
377        // ------------------- JDBC 3.0 -----------------------------------------
378        // Will be uncommented by the build process on a JDBC 3.0 system
379    
380    //#ifdef JDBC30
381    
382        public int getHoldability() throws SQLException {
383            checkOpen();
384            return _conn.getHoldability();
385        }
386    
387        public void setHoldability(int holdability) throws SQLException {
388            checkOpen();
389            _conn.setHoldability(holdability);
390        }
391    
392        public java.sql.Savepoint setSavepoint() throws SQLException {
393            checkOpen();
394            return _conn.setSavepoint();
395        }
396    
397        public java.sql.Savepoint setSavepoint(String name) throws SQLException {
398            checkOpen();
399            return _conn.setSavepoint(name);
400        }
401    
402        public void rollback(java.sql.Savepoint savepoint) throws SQLException {
403            checkOpen();
404            _conn.rollback(savepoint);
405        }
406    
407        public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException {
408            checkOpen();
409            _conn.releaseSavepoint(savepoint);
410        }
411    
412        public Statement createStatement(int resultSetType,
413                                         int resultSetConcurrency,
414                                         int resultSetHoldability)
415            throws SQLException {
416            checkOpen();
417            return _conn.createStatement(resultSetType, resultSetConcurrency,
418                                         resultSetHoldability);
419        }
420    
421        public PreparedStatement prepareStatement(String sql, int resultSetType,
422                                                  int resultSetConcurrency,
423                                                  int resultSetHoldability)
424            throws SQLException {
425            checkOpen();
426            return _conn.prepareStatement(sql, resultSetType,
427                                          resultSetConcurrency,
428                                          resultSetHoldability);
429        }
430    
431        public CallableStatement prepareCall(String sql, int resultSetType,
432                                             int resultSetConcurrency,
433                                             int resultSetHoldability)
434            throws SQLException {
435            checkOpen();
436            return _conn.prepareCall(sql, resultSetType,
437                                     resultSetConcurrency,
438                                     resultSetHoldability);
439        }
440    
441        public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
442            throws SQLException {
443            checkOpen();
444            return _conn.prepareStatement(sql, autoGeneratedKeys);
445        }
446    
447        public PreparedStatement prepareStatement(String sql, int columnIndexes[])
448            throws SQLException {
449            checkOpen();
450            return _conn.prepareStatement(sql, columnIndexes);
451        }
452    
453        public PreparedStatement prepareStatement(String sql, String columnNames[])
454            throws SQLException {
455            checkOpen();
456            return _conn.prepareStatement(sql, columnNames);
457          }
458            
459    //#endif
460    }