Coverage Report - org.apache.ojb.broker.util.WrappedConnection
 
Classes in this File Line Coverage Branch Coverage Complexity
WrappedConnection
N/A
N/A
1.273
 
 1  
 package org.apache.ojb.broker.util;
 2  
 
 3  
 /* Copyright 2002-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 java.sql.CallableStatement;
 19  
 import java.sql.Connection;
 20  
 import java.sql.DatabaseMetaData;
 21  
 import java.sql.PreparedStatement;
 22  
 import java.sql.SQLException;
 23  
 import java.sql.SQLWarning;
 24  
 import java.sql.Statement;
 25  
 import java.util.Map;
 26  
 
 27  
 import org.apache.commons.lang.builder.ToStringBuilder;
 28  
 import org.apache.commons.lang.builder.ToStringStyle;
 29  
 
 30  
 /**
 31  
  * Wrapper class for connections.
 32  
  * Simplified version of {@link org.apache.commons.dbcp.DelegatingConnection}
 33  
  */
 34  
 public class WrappedConnection implements Connection
 35  
 {
 36  
     private Connection _conn = null;
 37  
     private boolean _isClosed = false;
 38  
 
 39  
     public WrappedConnection(Connection c)
 40  
     {
 41  
         _conn = c;
 42  
     }
 43  
 
 44  
     /**
 45  
      * Returns my underlying {@link Connection}.
 46  
      * @return my underlying {@link Connection}.
 47  
      */
 48  
     public Connection getDelegate()
 49  
     {
 50  
         return _conn;
 51  
     }
 52  
 
 53  
     /**
 54  
      * If my underlying <tt>Connection</tt> is not a
 55  
      * <tt>WrappedConnection</tt>, returns it,
 56  
      * otherwise recursively invokes this method on
 57  
      * my delegate.
 58  
      * <p>
 59  
      * Hence this method will return the first
 60  
      * delegate that is not a <tt>WrappedConnection</tt>,
 61  
      * or <tt>null</tt> when no non-<tt>WrappedConnection</tt>
 62  
      * delegate can be found by transversing this chain.
 63  
      * <p>
 64  
      * This method is useful when you may have nested
 65  
      * <tt>WrappedConnection</tt>s, and you want to make
 66  
      * sure to obtain a "genuine" {@link java.sql.Connection}.
 67  
      */
 68  
     public Connection getInnermostDelegate()
 69  
     {
 70  
         Connection c = _conn;
 71  
         while (c != null && c instanceof WrappedConnection)
 72  
         {
 73  
             c = ((WrappedConnection) c).getDelegate();
 74  
             if (this == c)
 75  
             {
 76  
                 return null;
 77  
             }
 78  
         }
 79  
         return c;
 80  
     }
 81  
 
 82  
     /** Sets my delegate. */
 83  
     public void setDelegate(Connection c)
 84  
     {
 85  
         _conn = c;
 86  
     }
 87  
 
 88  
     protected void checkOpen() throws SQLException
 89  
     {
 90  
         if (_isClosed)
 91  
         {
 92  
             throw new SQLException("Connection is closed. " + this);
 93  
         }
 94  
     }
 95  
 
 96  
     /**
 97  
      * Activate the connection
 98  
      */
 99  
     public void activateConnection()
 100  
     {
 101  
         _isClosed = false;
 102  
         if (_conn instanceof WrappedConnection)
 103  
         {
 104  
             ((WrappedConnection) _conn).activateConnection();
 105  
         }
 106  
     }
 107  
 
 108  
     /**
 109  
      * Passivate the connection
 110  
      */
 111  
     public void passivateConnection() throws SQLException
 112  
     {
 113  
         _isClosed = true;
 114  
         if (_conn instanceof WrappedConnection)
 115  
         {
 116  
             ((WrappedConnection) _conn).passivateConnection();
 117  
         }
 118  
     }
 119  
 
 120  
     public String toString()
 121  
     {
 122  
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
 123  
                 .append("wrapped connection", (_conn != null ? _conn.toString() : null))
 124  
                 .toString();
 125  
     }
 126  
 
 127  
     /**
 128  
      * Closes the underlying connection, and close
 129  
      * any Statements that were not explicitly closed.
 130  
      */
 131  
     public void close() throws SQLException
 132  
     {
 133  
         passivateConnection();
 134  
         _conn.close();
 135  
     }
 136  
 
 137  
     public boolean isClosed() throws SQLException
 138  
     {
 139  
         if (_isClosed || _conn.isClosed())
 140  
         {
 141  
             return true;
 142  
         }
 143  
         return false;
 144  
     }
 145  
 
 146  
     public Statement createStatement() throws SQLException
 147  
     {
 148  
         checkOpen();
 149  
         return _conn.createStatement();
 150  
     }
 151  
 
 152  
     public Statement createStatement(int resultSetType,
 153  
                                      int resultSetConcurrency)
 154  
             throws SQLException
 155  
     {
 156  
         checkOpen();
 157  
         return _conn.createStatement(resultSetType, resultSetConcurrency);
 158  
     }
 159  
 
 160  
     public PreparedStatement prepareStatement(String sql) throws SQLException
 161  
     {
 162  
         checkOpen();
 163  
         return _conn.prepareStatement(sql);
 164  
     }
 165  
 
 166  
     public PreparedStatement prepareStatement(String sql,
 167  
                                               int resultSetType,
 168  
                                               int resultSetConcurrency)
 169  
             throws SQLException
 170  
     {
 171  
         checkOpen();
 172  
         return _conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
 173  
     }
 174  
 
 175  
     public CallableStatement prepareCall(String sql) throws SQLException
 176  
     {
 177  
         checkOpen();
 178  
         return _conn.prepareCall(sql);
 179  
     }
 180  
 
 181  
     public CallableStatement prepareCall(String sql,
 182  
                                          int resultSetType,
 183  
                                          int resultSetConcurrency)
 184  
             throws SQLException
 185  
     {
 186  
         checkOpen();
 187  
         return _conn.prepareCall(sql, resultSetType, resultSetConcurrency);
 188  
     }
 189  
 
 190  
     public void clearWarnings() throws SQLException
 191  
     {
 192  
         checkOpen();
 193  
         _conn.clearWarnings();
 194  
     }
 195  
 
 196  
     public void commit() throws SQLException
 197  
     {
 198  
         checkOpen();
 199  
         _conn.commit();
 200  
     }
 201  
 
 202  
     public boolean getAutoCommit() throws SQLException
 203  
     {
 204  
         checkOpen();
 205  
         return _conn.getAutoCommit();
 206  
     }
 207  
 
 208  
     public String getCatalog() throws SQLException
 209  
     {
 210  
         checkOpen();
 211  
         return _conn.getCatalog();
 212  
     }
 213  
 
 214  
     public DatabaseMetaData getMetaData() throws SQLException
 215  
     {
 216  
         checkOpen();
 217  
         return _conn.getMetaData();
 218  
     }
 219  
 
 220  
     public int getTransactionIsolation() throws SQLException
 221  
     {
 222  
         checkOpen();
 223  
         return _conn.getTransactionIsolation();
 224  
     }
 225  
 
 226  
     public Map getTypeMap() throws SQLException
 227  
     {
 228  
         checkOpen();
 229  
         return _conn.getTypeMap();
 230  
     }
 231  
 
 232  
     public SQLWarning getWarnings() throws SQLException
 233  
     {
 234  
         checkOpen();
 235  
         return _conn.getWarnings();
 236  
     }
 237  
 
 238  
     public boolean isReadOnly() throws SQLException
 239  
     {
 240  
         checkOpen();
 241  
         return _conn.isReadOnly();
 242  
     }
 243  
 
 244  
     public String nativeSQL(String sql) throws SQLException
 245  
     {
 246  
         checkOpen();
 247  
         return _conn.nativeSQL(sql);
 248  
     }
 249  
 
 250  
     public void rollback() throws SQLException
 251  
     {
 252  
         checkOpen();
 253  
         _conn.rollback();
 254  
     }
 255  
 
 256  
     public void setAutoCommit(boolean autoCommit) throws SQLException
 257  
     {
 258  
         checkOpen();
 259  
         _conn.setAutoCommit(autoCommit);
 260  
     }
 261  
 
 262  
     public void setCatalog(String catalog) throws SQLException
 263  
     {
 264  
         checkOpen();
 265  
         _conn.setCatalog(catalog);
 266  
     }
 267  
 
 268  
     public void setReadOnly(boolean readOnly) throws SQLException
 269  
     {
 270  
         checkOpen();
 271  
         _conn.setReadOnly(readOnly);
 272  
     }
 273  
 
 274  
     public void setTransactionIsolation(int level) throws SQLException
 275  
     {
 276  
         checkOpen();
 277  
         _conn.setTransactionIsolation(level);
 278  
     }
 279  
 
 280  
     public void setTypeMap(Map map) throws SQLException
 281  
     {
 282  
         checkOpen();
 283  
         _conn.setTypeMap(map);
 284  
     }
 285  
 
 286  
 
 287  
 
 288  
     // ------------------- JDBC 3.0 -----------------------------------------
 289  
     // Will be uncommented by the build process on a JDBC 3.0 system
 290  
 
 291  
 //#ifdef JDBC30
 292  
 
 293  
     public int getHoldability() throws SQLException {
 294  
         checkOpen();
 295  
         return _conn.getHoldability();
 296  
     }
 297  
 
 298  
     public void setHoldability(int holdability) throws SQLException {
 299  
         checkOpen();
 300  
         _conn.setHoldability(holdability);
 301  
     }
 302  
 
 303  
     public java.sql.Savepoint setSavepoint() throws SQLException {
 304  
         checkOpen();
 305  
         return _conn.setSavepoint();
 306  
     }
 307  
 
 308  
     public java.sql.Savepoint setSavepoint(String name) throws SQLException {
 309  
         checkOpen();
 310  
         return _conn.setSavepoint(name);
 311  
     }
 312  
 
 313  
     public void rollback(java.sql.Savepoint savepoint) throws SQLException {
 314  
         checkOpen();
 315  
         _conn.rollback(savepoint);
 316  
     }
 317  
 
 318  
     public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException {
 319  
         checkOpen();
 320  
         _conn.releaseSavepoint(savepoint);
 321  
     }
 322  
 
 323  
     public Statement createStatement(int resultSetType,
 324  
                                      int resultSetConcurrency,
 325  
                                      int resultSetHoldability)
 326  
         throws SQLException {
 327  
         checkOpen();
 328  
         return _conn.createStatement(resultSetType, resultSetConcurrency,
 329  
                                      resultSetHoldability);
 330  
     }
 331  
 
 332  
     public PreparedStatement prepareStatement(String sql, int resultSetType,
 333  
                                               int resultSetConcurrency,
 334  
                                               int resultSetHoldability)
 335  
         throws SQLException {
 336  
         checkOpen();
 337  
         return _conn.prepareStatement(sql, resultSetType,
 338  
                                       resultSetConcurrency,
 339  
                                       resultSetHoldability);
 340  
     }
 341  
 
 342  
     public CallableStatement prepareCall(String sql, int resultSetType,
 343  
                                          int resultSetConcurrency,
 344  
                                          int resultSetHoldability)
 345  
         throws SQLException {
 346  
         checkOpen();
 347  
         return _conn.prepareCall(sql, resultSetType,
 348  
                                  resultSetConcurrency,
 349  
                                  resultSetHoldability);
 350  
     }
 351  
 
 352  
     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
 353  
         throws SQLException {
 354  
         checkOpen();
 355  
         return _conn.prepareStatement(sql, autoGeneratedKeys);
 356  
     }
 357  
 
 358  
     public PreparedStatement prepareStatement(String sql, int columnIndexes[])
 359  
         throws SQLException {
 360  
         checkOpen();
 361  
         return _conn.prepareStatement(sql, columnIndexes);
 362  
     }
 363  
 
 364  
     public PreparedStatement prepareStatement(String sql, String columnNames[])
 365  
         throws SQLException {
 366  
         checkOpen();
 367  
         return _conn.prepareStatement(sql, columnNames);
 368  
       }
 369  
 
 370  
 //#endif
 371  
 }