KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jdbc > xa > ConnectionWrapper


1 /*
2  * $Id: ConnectionWrapper.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.lang.reflect.Proxy JavaDoc;
17 import java.sql.CallableStatement JavaDoc;
18 import java.sql.Connection JavaDoc;
19 import java.sql.DatabaseMetaData JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.SQLWarning JavaDoc;
23 import java.sql.Savepoint JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.sql.XAConnection JavaDoc;
28 import javax.transaction.Transaction JavaDoc;
29 import javax.transaction.TransactionManager JavaDoc;
30
31 /**
32  * TODO
33  */

34 public class ConnectionWrapper implements Connection JavaDoc
35 {
36
37     private XAConnection JavaDoc xaCon;
38     private Connection JavaDoc con;
39     private TransactionManager JavaDoc tm;
40     private Transaction JavaDoc tx;
41
42     public ConnectionWrapper(XAConnection JavaDoc xaCon, TransactionManager JavaDoc tm) throws SQLException JavaDoc
43     {
44         this.xaCon = xaCon;
45         this.con = xaCon.getConnection();
46         this.tm = tm;
47         this.tx = null;
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see java.sql.Connection#getHoldability()
54      */

55     public int getHoldability() throws SQLException JavaDoc
56     {
57         return con.getHoldability();
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see java.sql.Connection#getTransactionIsolation()
64      */

65     public int getTransactionIsolation() throws SQLException JavaDoc
66     {
67         return con.getTransactionIsolation();
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see java.sql.Connection#clearWarnings()
74      */

75     public void clearWarnings() throws SQLException JavaDoc
76     {
77         con.clearWarnings();
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see java.sql.Connection#close()
84      */

85     public void close() throws SQLException JavaDoc
86     {
87         con.close();
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see java.sql.Connection#commit()
94      */

95     public void commit() throws SQLException JavaDoc
96     {
97         con.commit();
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see java.sql.Connection#rollback()
104      */

105     public void rollback() throws SQLException JavaDoc
106     {
107         con.rollback();
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see java.sql.Connection#getAutoCommit()
114      */

115     public boolean getAutoCommit() throws SQLException JavaDoc
116     {
117         return con.getAutoCommit();
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see java.sql.Connection#isClosed()
124      */

125     public boolean isClosed() throws SQLException JavaDoc
126     {
127         return con.isClosed();
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see java.sql.Connection#isReadOnly()
134      */

135     public boolean isReadOnly() throws SQLException JavaDoc
136     {
137         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 JavaDoc
146     {
147         con.setHoldability(holdability);
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see java.sql.Connection#setTransactionIsolation(int)
154      */

155     public void setTransactionIsolation(int level) throws SQLException JavaDoc
156     {
157         con.setTransactionIsolation(level);
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see java.sql.Connection#setAutoCommit(boolean)
164      */

165     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc
166     {
167         con.setAutoCommit(autoCommit);
168     }
169
170     /*
171      * (non-Javadoc)
172      *
173      * @see java.sql.Connection#setReadOnly(boolean)
174      */

175     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc
176     {
177         con.setReadOnly(readOnly);
178     }
179
180     /*
181      * (non-Javadoc)
182      *
183      * @see java.sql.Connection#getCatalog()
184      */

185     public String JavaDoc getCatalog() throws SQLException JavaDoc
186     {
187         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 JavaDoc catalog) throws SQLException JavaDoc
196     {
197         con.setCatalog(catalog);
198     }
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see java.sql.Connection#getMetaData()
204      */

205     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
206     {
207         return con.getMetaData();
208     }
209
210     /*
211      * (non-Javadoc)
212      *
213      * @see java.sql.Connection#getWarnings()
214      */

215     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
216     {
217         return con.getWarnings();
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see java.sql.Connection#setSavepoint()
224      */

225     public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc
226     {
227         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 JavaDoc savepoint) throws SQLException JavaDoc
236     {
237         con.releaseSavepoint(savepoint);
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see java.sql.Connection#rollback(java.sql.Savepoint)
244      */

245     public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
246     {
247         con.rollback();
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see java.sql.Connection#createStatement()
254      */

255     public Statement JavaDoc createStatement() throws SQLException JavaDoc
256     {
257         Statement JavaDoc st = con.createStatement();
258         return (Statement JavaDoc)Proxy.newProxyInstance(Statement JavaDoc.class.getClassLoader(),
259             new Class JavaDoc[]{Statement JavaDoc.class}, new StatementInvocationHandler(st));
260     }
261
262     /*
263      * (non-Javadoc)
264      *
265      * @see java.sql.Connection#createStatement(int, int)
266      */

267     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc
268     {
269         Statement JavaDoc st = con.createStatement(resultSetType, resultSetConcurrency);
270         return (Statement JavaDoc)Proxy.newProxyInstance(Statement JavaDoc.class.getClassLoader(),
271             new Class JavaDoc[]{Statement JavaDoc.class}, new StatementInvocationHandler(st));
272     }
273
274     /*
275      * (non-Javadoc)
276      *
277      * @see java.sql.Connection#createStatement(int, int, int)
278      */

279     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
280         throws SQLException JavaDoc
281     {
282         Statement JavaDoc st = con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
283         return (Statement JavaDoc)Proxy.newProxyInstance(Statement JavaDoc.class.getClassLoader(),
284             new Class JavaDoc[]{Statement JavaDoc.class}, new StatementInvocationHandler(st));
285     }
286
287     /*
288      * (non-Javadoc)
289      *
290      * @see java.sql.Connection#getTypeMap()
291      */

292     public Map JavaDoc getTypeMap() throws SQLException JavaDoc
293     {
294         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 JavaDoc map) throws SQLException JavaDoc
303     {
304         con.setTypeMap(map);
305     }
306
307     /*
308      * (non-Javadoc)
309      *
310      * @see java.sql.Connection#nativeSQL(java.lang.String)
311      */

312     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
313     {
314         return con.nativeSQL(sql);
315     }
316
317     /*
318      * (non-Javadoc)
319      *
320      * @see java.sql.Connection#prepareCall(java.lang.String)
321      */

322     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc
323     {
324         CallableStatement JavaDoc cs = con.prepareCall(sql);
325         return (CallableStatement JavaDoc)Proxy.newProxyInstance(CallableStatement JavaDoc.class.getClassLoader(),
326             new Class JavaDoc[]{CallableStatement JavaDoc.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 JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
335         throws SQLException JavaDoc
336     {
337         CallableStatement JavaDoc cs = con.prepareCall(sql, resultSetType, resultSetConcurrency);
338         return (CallableStatement JavaDoc)Proxy.newProxyInstance(CallableStatement JavaDoc.class.getClassLoader(),
339             new Class JavaDoc[]{CallableStatement JavaDoc.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 JavaDoc prepareCall(String JavaDoc sql,
348                                          int resultSetType,
349                                          int resultSetConcurrency,
350                                          int resultSetHoldability) throws SQLException JavaDoc
351     {
352         CallableStatement JavaDoc cs = con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
353         return (CallableStatement JavaDoc)Proxy.newProxyInstance(CallableStatement JavaDoc.class.getClassLoader(),
354             new Class JavaDoc[]{CallableStatement JavaDoc.class}, new StatementInvocationHandler(cs));
355     }
356
357     /*
358      * (non-Javadoc)
359      *
360      * @see java.sql.Connection#prepareStatement(java.lang.String)
361      */

362     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc
363     {
364         PreparedStatement JavaDoc ps = con.prepareStatement(sql);
365         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
366             new Class JavaDoc[]{PreparedStatement JavaDoc.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 JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
375     {
376         PreparedStatement JavaDoc ps = con.prepareStatement(sql, autoGeneratedKeys);
377         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
378             new Class JavaDoc[]{PreparedStatement JavaDoc.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 JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
387         throws SQLException JavaDoc
388     {
389         PreparedStatement JavaDoc ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency);
390         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
391             new Class JavaDoc[]{PreparedStatement JavaDoc.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 JavaDoc prepareStatement(String JavaDoc sql,
400                                               int resultSetType,
401                                               int resultSetConcurrency,
402                                               int resultSetHoldability) throws SQLException JavaDoc
403     {
404         PreparedStatement JavaDoc ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency,
405             resultSetHoldability);
406         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
407             new Class JavaDoc[]{PreparedStatement JavaDoc.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 JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
416     {
417         PreparedStatement JavaDoc ps = con.prepareStatement(sql, columnIndexes);
418         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
419             new Class JavaDoc[]{PreparedStatement JavaDoc.class}, new StatementInvocationHandler(ps));
420     }
421
422     /*
423      * (non-Javadoc)
424      *
425      * @see java.sql.Connection#setSavepoint(java.lang.String)
426      */

427     public Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc
428     {
429         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 JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
439     {
440         PreparedStatement JavaDoc ps = con.prepareStatement(sql, columnNames);
441         return (PreparedStatement JavaDoc)Proxy.newProxyInstance(PreparedStatement JavaDoc.class.getClassLoader(),
442             new Class JavaDoc[]{PreparedStatement JavaDoc.class}, new StatementInvocationHandler(ps));
443     }
444
445     protected void enlist() throws Exception JavaDoc
446     {
447         if (tm != null && tx == null)
448         {
449             tx = tm.getTransaction();
450             if (tx != null)
451             {
452                 tx.enlistResource(xaCon.getXAResource());
453             }
454         }
455     }
456
457     protected class StatementInvocationHandler implements InvocationHandler JavaDoc
458     {
459
460         private Statement JavaDoc statement;
461
462         public StatementInvocationHandler(Statement JavaDoc statement)
463         {
464             this.statement = statement;
465         }
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 JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
474         {
475             if (method.getName().startsWith("execute"))
476             {
477                 enlist();
478             }
479             try
480             {
481                 return method.invoke(statement, args);
482             }
483             catch (InvocationTargetException JavaDoc ex)
484             {
485                 throw ex.getTargetException();
486             }
487         }
488
489     }
490
491 }
492
Popular Tags