Coverage Report - org.mule.providers.jdbc.xa.ConnectionWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionWrapper
0%
0/71
0%
0/14
1.15
ConnectionWrapper$StatementInvocationHandler
0%
0/8
0%
0/1
1.15
 
 1  
 /*
 2  
  * $Id: ConnectionWrapper.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.jdbc.xa;
 12  
 
 13  
 import java.lang.reflect.InvocationHandler;
 14  
 import java.lang.reflect.InvocationTargetException;
 15  
 import java.lang.reflect.Method;
 16  
 import java.lang.reflect.Proxy;
 17  
 import java.sql.CallableStatement;
 18  
 import java.sql.Connection;
 19  
 import java.sql.DatabaseMetaData;
 20  
 import java.sql.PreparedStatement;
 21  
 import java.sql.SQLException;
 22  
 import java.sql.SQLWarning;
 23  
 import java.sql.Savepoint;
 24  
 import java.sql.Statement;
 25  
 import java.util.Map;
 26  
 
 27  
 import javax.sql.XAConnection;
 28  
 import javax.transaction.Transaction;
 29  
 import javax.transaction.TransactionManager;
 30  
 
 31  
 /**
 32  
  * TODO
 33  
  */
 34  
 public class ConnectionWrapper implements Connection
 35  
 {
 36  
 
 37  
     private XAConnection xaCon;
 38  
     private Connection con;
 39  
     private TransactionManager tm;
 40  
     private Transaction tx;
 41  
 
 42  
     public ConnectionWrapper(XAConnection xaCon, TransactionManager tm) throws SQLException
 43  0
     {
 44  0
         this.xaCon = xaCon;
 45  0
         this.con = xaCon.getConnection();
 46  0
         this.tm = tm;
 47  0
         this.tx = null;
 48  0
     }
 49  
 
 50  
     /*
 51  
      * (non-Javadoc)
 52  
      * 
 53  
      * @see java.sql.Connection#getHoldability()
 54  
      */
 55  
     public int getHoldability() throws SQLException
 56  
     {
 57  0
         return con.getHoldability();
 58  
     }
 59  
 
 60  
     /*
 61  
      * (non-Javadoc)
 62  
      * 
 63  
      * @see java.sql.Connection#getTransactionIsolation()
 64  
      */
 65  
     public int getTransactionIsolation() throws SQLException
 66  
     {
 67  0
         return con.getTransactionIsolation();
 68  
     }
 69  
 
 70  
     /*
 71  
      * (non-Javadoc)
 72  
      * 
 73  
      * @see java.sql.Connection#clearWarnings()
 74  
      */
 75  
     public void clearWarnings() throws SQLException
 76  
     {
 77  0
         con.clearWarnings();
 78  0
     }
 79  
 
 80  
     /*
 81  
      * (non-Javadoc)
 82  
      * 
 83  
      * @see java.sql.Connection#close()
 84  
      */
 85  
     public void close() throws SQLException
 86  
     {
 87  0
         con.close();
 88  0
     }
 89  
 
 90  
     /*
 91  
      * (non-Javadoc)
 92  
      * 
 93  
      * @see java.sql.Connection#commit()
 94  
      */
 95  
     public void commit() throws SQLException
 96  
     {
 97  0
         con.commit();
 98  0
     }
 99  
 
 100  
     /*
 101  
      * (non-Javadoc)
 102  
      * 
 103  
      * @see java.sql.Connection#rollback()
 104  
      */
 105  
     public void rollback() throws SQLException
 106  
     {
 107  0
         con.rollback();
 108  0
     }
 109  
 
 110  
     /*
 111  
      * (non-Javadoc)
 112  
      * 
 113  
      * @see java.sql.Connection#getAutoCommit()
 114  
      */
 115  
     public boolean getAutoCommit() throws SQLException
 116  
     {
 117  0
         return con.getAutoCommit();
 118  
     }
 119  
 
 120  
     /*
 121  
      * (non-Javadoc)
 122  
      * 
 123  
      * @see java.sql.Connection#isClosed()
 124  
      */
 125  
     public boolean isClosed() throws SQLException
 126  
     {
 127  0
         return con.isClosed();
 128  
     }
 129  
 
 130  
     /*
 131  
      * (non-Javadoc)
 132  
      * 
 133  
      * @see java.sql.Connection#isReadOnly()
 134  
      */
 135  
     public boolean isReadOnly() throws SQLException
 136  
     {
 137  0
         return con.isReadOnly();
 138  
     }
 139  
 
 140  
     /*
 141  
      * (non-Javadoc)
 142  
      * 
 143  
      * @see java.sql.Connection#setHoldability(int)
 144  
      */
 145  
     public void setHoldability(int holdability) throws SQLException
 146  
     {
 147  0
         con.setHoldability(holdability);
 148  0
     }
 149  
 
 150  
     /*
 151  
      * (non-Javadoc)
 152  
      * 
 153  
      * @see java.sql.Connection#setTransactionIsolation(int)
 154  
      */
 155  
     public void setTransactionIsolation(int level) throws SQLException
 156  
     {
 157  0
         con.setTransactionIsolation(level);
 158  0
     }
 159  
 
 160  
     /*
 161  
      * (non-Javadoc)
 162  
      * 
 163  
      * @see java.sql.Connection#setAutoCommit(boolean)
 164  
      */
 165  
     public void setAutoCommit(boolean autoCommit) throws SQLException
 166  
     {
 167  0
         con.setAutoCommit(autoCommit);
 168  0
     }
 169  
 
 170  
     /*
 171  
      * (non-Javadoc)
 172  
      * 
 173  
      * @see java.sql.Connection#setReadOnly(boolean)
 174  
      */
 175  
     public void setReadOnly(boolean readOnly) throws SQLException
 176  
     {
 177  0
         con.setReadOnly(readOnly);
 178  0
     }
 179  
 
 180  
     /*
 181  
      * (non-Javadoc)
 182  
      * 
 183  
      * @see java.sql.Connection#getCatalog()
 184  
      */
 185  
     public String getCatalog() throws SQLException
 186  
     {
 187  0
         return con.getCatalog();
 188  
     }
 189  
 
 190  
     /*
 191  
      * (non-Javadoc)
 192  
      * 
 193  
      * @see java.sql.Connection#setCatalog(java.lang.String)
 194  
      */
 195  
     public void setCatalog(String catalog) throws SQLException
 196  
     {
 197  0
         con.setCatalog(catalog);
 198  0
     }
 199  
 
 200  
     /*
 201  
      * (non-Javadoc)
 202  
      * 
 203  
      * @see java.sql.Connection#getMetaData()
 204  
      */
 205  
     public DatabaseMetaData getMetaData() throws SQLException
 206  
     {
 207  0
         return con.getMetaData();
 208  
     }
 209  
 
 210  
     /*
 211  
      * (non-Javadoc)
 212  
      * 
 213  
      * @see java.sql.Connection#getWarnings()
 214  
      */
 215  
     public SQLWarning getWarnings() throws SQLException
 216  
     {
 217  0
         return con.getWarnings();
 218  
     }
 219  
 
 220  
     /*
 221  
      * (non-Javadoc)
 222  
      * 
 223  
      * @see java.sql.Connection#setSavepoint()
 224  
      */
 225  
     public Savepoint setSavepoint() throws SQLException
 226  
     {
 227  0
         return con.setSavepoint();
 228  
     }
 229  
 
 230  
     /*
 231  
      * (non-Javadoc)
 232  
      * 
 233  
      * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
 234  
      */
 235  
     public void releaseSavepoint(Savepoint savepoint) throws SQLException
 236  
     {
 237  0
         con.releaseSavepoint(savepoint);
 238  0
     }
 239  
 
 240  
     /*
 241  
      * (non-Javadoc)
 242  
      * 
 243  
      * @see java.sql.Connection#rollback(java.sql.Savepoint)
 244  
      */
 245  
     public void rollback(Savepoint savepoint) throws SQLException
 246  
     {
 247  0
         con.rollback();
 248  0
     }
 249  
 
 250  
     /*
 251  
      * (non-Javadoc)
 252  
      * 
 253  
      * @see java.sql.Connection#createStatement()
 254  
      */
 255  
     public Statement createStatement() throws SQLException
 256  
     {
 257  0
         Statement st = con.createStatement();
 258  0
         return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(),
 259  
             new Class[]{Statement.class}, new StatementInvocationHandler(st));
 260  
     }
 261  
 
 262  
     /*
 263  
      * (non-Javadoc)
 264  
      * 
 265  
      * @see java.sql.Connection#createStatement(int, int)
 266  
      */
 267  
     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
 268  
     {
 269  0
         Statement st = con.createStatement(resultSetType, resultSetConcurrency);
 270  0
         return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(),
 271  
             new Class[]{Statement.class}, new StatementInvocationHandler(st));
 272  
     }
 273  
 
 274  
     /*
 275  
      * (non-Javadoc)
 276  
      * 
 277  
      * @see java.sql.Connection#createStatement(int, int, int)
 278  
      */
 279  
     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
 280  
         throws SQLException
 281  
     {
 282  0
         Statement st = con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
 283  0
         return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(),
 284  
             new Class[]{Statement.class}, new StatementInvocationHandler(st));
 285  
     }
 286  
 
 287  
     /*
 288  
      * (non-Javadoc)
 289  
      * 
 290  
      * @see java.sql.Connection#getTypeMap()
 291  
      */
 292  
     public Map getTypeMap() throws SQLException
 293  
     {
 294  0
         return con.getTypeMap();
 295  
     }
 296  
 
 297  
     /*
 298  
      * (non-Javadoc)
 299  
      * 
 300  
      * @see java.sql.Connection#setTypeMap(java.util.Map)
 301  
      */
 302  
     public void setTypeMap(Map map) throws SQLException
 303  
     {
 304  0
         con.setTypeMap(map);
 305  0
     }
 306  
 
 307  
     /*
 308  
      * (non-Javadoc)
 309  
      * 
 310  
      * @see java.sql.Connection#nativeSQL(java.lang.String)
 311  
      */
 312  
     public String nativeSQL(String sql) throws SQLException
 313  
     {
 314  0
         return con.nativeSQL(sql);
 315  
     }
 316  
 
 317  
     /*
 318  
      * (non-Javadoc)
 319  
      * 
 320  
      * @see java.sql.Connection#prepareCall(java.lang.String)
 321  
      */
 322  
     public CallableStatement prepareCall(String sql) throws SQLException
 323  
     {
 324  0
         CallableStatement cs = con.prepareCall(sql);
 325  0
         return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(),
 326  
             new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs));
 327  
     }
 328  
 
 329  
     /*
 330  
      * (non-Javadoc)
 331  
      * 
 332  
      * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
 333  
      */
 334  
     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
 335  
         throws SQLException
 336  
     {
 337  0
         CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency);
 338  0
         return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(),
 339  
             new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs));
 340  
     }
 341  
 
 342  
     /*
 343  
      * (non-Javadoc)
 344  
      * 
 345  
      * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
 346  
      */
 347  
     public CallableStatement prepareCall(String sql,
 348  
                                          int resultSetType,
 349  
                                          int resultSetConcurrency,
 350  
                                          int resultSetHoldability) throws SQLException
 351  
     {
 352  0
         CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
 353  0
         return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(),
 354  
             new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs));
 355  
     }
 356  
 
 357  
     /*
 358  
      * (non-Javadoc)
 359  
      * 
 360  
      * @see java.sql.Connection#prepareStatement(java.lang.String)
 361  
      */
 362  
     public PreparedStatement prepareStatement(String sql) throws SQLException
 363  
     {
 364  0
         PreparedStatement ps = con.prepareStatement(sql);
 365  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 366  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 367  
     }
 368  
 
 369  
     /*
 370  
      * (non-Javadoc)
 371  
      * 
 372  
      * @see java.sql.Connection#prepareStatement(java.lang.String, int)
 373  
      */
 374  
     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
 375  
     {
 376  0
         PreparedStatement ps = con.prepareStatement(sql, autoGeneratedKeys);
 377  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 378  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 379  
     }
 380  
 
 381  
     /*
 382  
      * (non-Javadoc)
 383  
      * 
 384  
      * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
 385  
      */
 386  
     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
 387  
         throws SQLException
 388  
     {
 389  0
         PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency);
 390  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 391  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 392  
     }
 393  
 
 394  
     /*
 395  
      * (non-Javadoc)
 396  
      * 
 397  
      * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
 398  
      */
 399  
     public PreparedStatement prepareStatement(String sql,
 400  
                                               int resultSetType,
 401  
                                               int resultSetConcurrency,
 402  
                                               int resultSetHoldability) throws SQLException
 403  
     {
 404  0
         PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency,
 405  
             resultSetHoldability);
 406  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 407  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 408  
     }
 409  
 
 410  
     /*
 411  
      * (non-Javadoc)
 412  
      * 
 413  
      * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
 414  
      */
 415  
     public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
 416  
     {
 417  0
         PreparedStatement ps = con.prepareStatement(sql, columnIndexes);
 418  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 419  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 420  
     }
 421  
 
 422  
     /*
 423  
      * (non-Javadoc)
 424  
      * 
 425  
      * @see java.sql.Connection#setSavepoint(java.lang.String)
 426  
      */
 427  
     public Savepoint setSavepoint(String name) throws SQLException
 428  
     {
 429  0
         return con.setSavepoint(name);
 430  
     }
 431  
 
 432  
     /*
 433  
      * (non-Javadoc)
 434  
      * 
 435  
      * @see java.sql.Connection#prepareStatement(java.lang.String,
 436  
      *      java.lang.String[])
 437  
      */
 438  
     public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
 439  
     {
 440  0
         PreparedStatement ps = con.prepareStatement(sql, columnNames);
 441  0
         return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(),
 442  
             new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps));
 443  
     }
 444  
 
 445  
     protected void enlist() throws Exception
 446  
     {
 447  0
         if (tm != null && tx == null)
 448  
         {
 449  0
             tx = tm.getTransaction();
 450  0
             if (tx != null)
 451  
             {
 452  0
                 tx.enlistResource(xaCon.getXAResource());
 453  
             }
 454  
         }
 455  0
     }
 456  
 
 457  
     protected class StatementInvocationHandler implements InvocationHandler
 458  
     {
 459  
 
 460  
         private Statement statement;
 461  
 
 462  
         public StatementInvocationHandler(Statement statement)
 463  0
         {
 464  0
             this.statement = statement;
 465  0
         }
 466  
 
 467  
         /*
 468  
          * (non-Javadoc)
 469  
          * 
 470  
          * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
 471  
          *      java.lang.reflect.Method, java.lang.Object[])
 472  
          */
 473  
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
 474  
         {
 475  0
             if (method.getName().startsWith("execute"))
 476  
             {
 477  0
                 enlist();
 478  
             }
 479  
             try
 480  
             {
 481  0
                 return method.invoke(statement, args);
 482  
             }
 483  0
             catch (InvocationTargetException ex)
 484  
             {
 485  0
                 throw ex.getTargetException();
 486  
             }
 487  
         }
 488  
 
 489  
     }
 490  
 
 491  
 }