KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ProxyDatabaseMetaData


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import org.logicalcobwebs.cglib.proxy.MethodInterceptor;
12 import org.logicalcobwebs.cglib.proxy.MethodProxy;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.sql.Connection JavaDoc;
17 import java.sql.DatabaseMetaData JavaDoc;
18
19 /**
20  * Delegates to a normal Coonection for everything but the close()
21  * method (when it puts itself back into the pool instead).
22  * @version $Revision: 1.9 $, $Date: 2006/01/18 14:40:02 $
23  * @author billhorsman
24  * @author $Author: billhorsman $ (current maintainer)
25  */

26 class ProxyDatabaseMetaData implements MethodInterceptor {
27
28     private static final Log LOG = LogFactory.getLog(ProxyDatabaseMetaData.class);
29
30     private static final String JavaDoc GET_CONNECTION_METHOD = "getConnection";
31
32     private static final String JavaDoc EQUALS_METHOD = "equals";
33
34     private static final String JavaDoc FINALIZE_METHOD = "finalize";
35
36     private DatabaseMetaData JavaDoc databaseMetaData;
37
38     private Connection JavaDoc wrappedConnection;
39
40     /**
41      * @param databaseMetaData the meta data we use to delegate all calls to (except getConnection())
42      * @param wrappedConnection the connection we return if asked for the connection
43      */

44     public ProxyDatabaseMetaData(DatabaseMetaData JavaDoc databaseMetaData, Connection JavaDoc wrappedConnection) {
45         this.databaseMetaData = databaseMetaData;
46         this.wrappedConnection = wrappedConnection;
47     }
48
49     public Object JavaDoc intercept(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args, MethodProxy methodProxy) throws Throwable JavaDoc {
50         Object JavaDoc result = null;
51         int argCount = args != null ? args.length : 0;
52         try {
53             if (method.getName().equals(GET_CONNECTION_METHOD)) {
54                 result = getConnection();
55             } else if (method.getName().equals(EQUALS_METHOD) && argCount == 1) {
56                 result = new Boolean JavaDoc(equals(args[0]));
57             } else if (method.getName().equals(FINALIZE_METHOD)) {
58                 super.finalize();
59             } else {
60                 result = method.invoke(getDatabaseMetaData(), args);
61             }
62         } catch (InvocationTargetException JavaDoc e) {
63             throw e.getTargetException();
64         } catch (Exception JavaDoc e) {
65             LOG.error("Unexpected invocation exception", e);
66             throw new RuntimeException JavaDoc("Unexpected invocation exception: "
67                     + e.getMessage());
68         }
69
70         return result;
71     }
72
73     /**
74      * Whether the underlying databaseMetaData are equal
75      * @param obj the object (probably another databaseMetaData) that we
76      * are being compared to
77      * @return whether they are the same
78      */

79     public boolean equals(Object JavaDoc obj) {
80         return databaseMetaData.hashCode() == obj.hashCode();
81     }
82
83     /**
84      * We don't want to ask the DatabaseMetaData object for the
85      * connection or we will get the delegate instead of the Proxool
86      * one.
87      * @see DatabaseMetaData#getConnection
88      */

89     public Connection JavaDoc getConnection() {
90         return wrappedConnection;
91     }
92
93     /**
94      * Get the DatabaseMetaData from the connection
95      * @return databaseMetaData
96      */

97     protected DatabaseMetaData JavaDoc getDatabaseMetaData() {
98         return databaseMetaData;
99     }
100
101     /**
102      * @see Object#toString
103      */

104     public String JavaDoc toString() {
105         return databaseMetaData.toString();
106     }
107
108 }
109
110 /*
111  Revision history:
112  $Log: ProxyDatabaseMetaData.java,v $
113  Revision 1.9 2006/01/18 14:40:02 billhorsman
114  Unbundled Jakarta's Commons Logging.
115
116  Revision 1.8 2004/06/02 20:50:47 billhorsman
117  Dropped obsolete InvocationHandler reference and injectable interface stuff.
118
119  Revision 1.7 2004/03/23 21:19:45 billhorsman
120  Added disposable wrapper to proxied connection. And made proxied objects implement delegate interfaces too.
121
122  Revision 1.6 2003/12/12 19:29:47 billhorsman
123  Now uses Cglib 2.0
124
125  Revision 1.5 2003/09/10 22:21:04 chr32
126  Removing > jdk 1.2 dependencies.
127
128  Revision 1.4 2003/03/03 11:11:58 billhorsman
129  fixed licence
130
131  Revision 1.3 2003/02/06 17:41:04 billhorsman
132  now uses imported logging
133
134  Revision 1.2 2003/01/31 16:53:18 billhorsman
135  checkstyle
136
137  Revision 1.1 2003/01/31 14:33:18 billhorsman
138  fix for DatabaseMetaData
139
140  Revision 1.21 2003/01/27 18:26:38 billhorsman
141  refactoring of ProxyConnection and ProxyStatement to
142  make it easier to write JDK 1.2 patch
143
144  Revision 1.20 2002/12/19 00:08:36 billhorsman
145  automatic closure of statements when a connection is closed
146
147  Revision 1.19 2002/12/17 17:15:39 billhorsman
148  Better synchronization of status stuff
149
150  Revision 1.18 2002/12/03 12:24:00 billhorsman
151  fixed fatal sql exception
152
153  Revision 1.17 2002/11/12 20:24:12 billhorsman
154  checkstyle
155
156  Revision 1.16 2002/11/12 20:18:23 billhorsman
157  Made connection resetter a bit more friendly. Now, if it encounters any problems
158  during reset then that connection is thrown away. This is going to cause you
159  problems if you always close connections in an unstable state (e.g. with transactions
160  open. But then again, it's better to know about that as soon as possible, right?
161
162  Revision 1.15 2002/11/07 18:56:22 billhorsman
163  fixed NullPointerException introduced yesterday on isClose() method
164
165  Revision 1.14 2002/11/07 12:38:04 billhorsman
166  performance improvement - only reset when it might be necessary
167
168  Revision 1.13 2002/11/06 20:26:49 billhorsman
169  improved doc, added connection resetting, and made
170  isClosed() work correctly
171
172  Revision 1.12 2002/11/02 13:57:33 billhorsman
173  checkstyle
174
175  Revision 1.11 2002/10/30 21:25:09 billhorsman
176  move createStatement into ProxyFactory
177
178  Revision 1.10 2002/10/30 21:19:17 billhorsman
179  make use of ProxyFactory
180
181  Revision 1.9 2002/10/28 19:51:34 billhorsman
182  Fixed NullPointerException when calling connection.createProxyStatement()
183
184  Revision 1.8 2002/10/28 19:28:25 billhorsman
185  checkstyle
186
187  Revision 1.7 2002/10/28 08:20:23 billhorsman
188  draft sql dump stuff
189
190  Revision 1.6 2002/10/25 15:59:32 billhorsman
191  made non-public where possible
192
193  Revision 1.5 2002/10/24 18:15:09 billhorsman
194  removed unnecessary debug
195
196  Revision 1.4 2002/10/17 15:29:18 billhorsman
197  fixes so that equals() works
198
199  Revision 1.3 2002/09/19 10:33:57 billhorsman
200  added ProxyConnection#toString
201
202  Revision 1.2 2002/09/18 13:48:56 billhorsman
203  checkstyle and doc
204
205  Revision 1.1.1.1 2002/09/13 08:13:30 billhorsman
206  new
207
208  Revision 1.10 2002/08/24 19:57:15 billhorsman
209  checkstyle changes
210
211  Revision 1.9 2002/08/24 19:42:26 billhorsman
212  new proxy stuff to work with JDK 1.4
213
214  Revision 1.6 2002/07/02 11:19:08 billhorsman
215  layout code and imports
216
217  Revision 1.5 2002/06/28 11:19:47 billhorsman
218  improved doc
219
220 */

221
Popular Tags