001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import org.logicalcobwebs.cglib.proxy.MethodInterceptor;
012: import org.logicalcobwebs.cglib.proxy.MethodProxy;
013:
014: import java.lang.reflect.InvocationTargetException;
015: import java.lang.reflect.Method;
016: import java.sql.Connection;
017: import java.sql.DatabaseMetaData;
018:
019: /**
020: * Delegates to a normal Coonection for everything but the close()
021: * method (when it puts itself back into the pool instead).
022: * @version $Revision: 1.9 $, $Date: 2006/01/18 14:40:02 $
023: * @author billhorsman
024: * @author $Author: billhorsman $ (current maintainer)
025: */
026: class ProxyDatabaseMetaData implements MethodInterceptor {
027:
028: private static final Log LOG = LogFactory
029: .getLog(ProxyDatabaseMetaData.class);
030:
031: private static final String GET_CONNECTION_METHOD = "getConnection";
032:
033: private static final String EQUALS_METHOD = "equals";
034:
035: private static final String FINALIZE_METHOD = "finalize";
036:
037: private DatabaseMetaData databaseMetaData;
038:
039: private Connection wrappedConnection;
040:
041: /**
042: * @param databaseMetaData the meta data we use to delegate all calls to (except getConnection())
043: * @param wrappedConnection the connection we return if asked for the connection
044: */
045: public ProxyDatabaseMetaData(DatabaseMetaData databaseMetaData,
046: Connection wrappedConnection) {
047: this .databaseMetaData = databaseMetaData;
048: this .wrappedConnection = wrappedConnection;
049: }
050:
051: public Object intercept(Object proxy, Method method, Object[] args,
052: MethodProxy methodProxy) throws Throwable {
053: Object result = null;
054: int argCount = args != null ? args.length : 0;
055: try {
056: if (method.getName().equals(GET_CONNECTION_METHOD)) {
057: result = getConnection();
058: } else if (method.getName().equals(EQUALS_METHOD)
059: && argCount == 1) {
060: result = new Boolean(equals(args[0]));
061: } else if (method.getName().equals(FINALIZE_METHOD)) {
062: super .finalize();
063: } else {
064: result = method.invoke(getDatabaseMetaData(), args);
065: }
066: } catch (InvocationTargetException e) {
067: throw e.getTargetException();
068: } catch (Exception e) {
069: LOG.error("Unexpected invocation exception", e);
070: throw new RuntimeException(
071: "Unexpected invocation exception: "
072: + e.getMessage());
073: }
074:
075: return result;
076: }
077:
078: /**
079: * Whether the underlying databaseMetaData are equal
080: * @param obj the object (probably another databaseMetaData) that we
081: * are being compared to
082: * @return whether they are the same
083: */
084: public boolean equals(Object obj) {
085: return databaseMetaData.hashCode() == obj.hashCode();
086: }
087:
088: /**
089: * We don't want to ask the DatabaseMetaData object for the
090: * connection or we will get the delegate instead of the Proxool
091: * one.
092: * @see DatabaseMetaData#getConnection
093: */
094: public Connection getConnection() {
095: return wrappedConnection;
096: }
097:
098: /**
099: * Get the DatabaseMetaData from the connection
100: * @return databaseMetaData
101: */
102: protected DatabaseMetaData getDatabaseMetaData() {
103: return databaseMetaData;
104: }
105:
106: /**
107: * @see Object#toString
108: */
109: public String toString() {
110: return databaseMetaData.toString();
111: }
112:
113: }
114:
115: /*
116: Revision history:
117: $Log: ProxyDatabaseMetaData.java,v $
118: Revision 1.9 2006/01/18 14:40:02 billhorsman
119: Unbundled Jakarta's Commons Logging.
120:
121: Revision 1.8 2004/06/02 20:50:47 billhorsman
122: Dropped obsolete InvocationHandler reference and injectable interface stuff.
123:
124: Revision 1.7 2004/03/23 21:19:45 billhorsman
125: Added disposable wrapper to proxied connection. And made proxied objects implement delegate interfaces too.
126:
127: Revision 1.6 2003/12/12 19:29:47 billhorsman
128: Now uses Cglib 2.0
129:
130: Revision 1.5 2003/09/10 22:21:04 chr32
131: Removing > jdk 1.2 dependencies.
132:
133: Revision 1.4 2003/03/03 11:11:58 billhorsman
134: fixed licence
135:
136: Revision 1.3 2003/02/06 17:41:04 billhorsman
137: now uses imported logging
138:
139: Revision 1.2 2003/01/31 16:53:18 billhorsman
140: checkstyle
141:
142: Revision 1.1 2003/01/31 14:33:18 billhorsman
143: fix for DatabaseMetaData
144:
145: Revision 1.21 2003/01/27 18:26:38 billhorsman
146: refactoring of ProxyConnection and ProxyStatement to
147: make it easier to write JDK 1.2 patch
148:
149: Revision 1.20 2002/12/19 00:08:36 billhorsman
150: automatic closure of statements when a connection is closed
151:
152: Revision 1.19 2002/12/17 17:15:39 billhorsman
153: Better synchronization of status stuff
154:
155: Revision 1.18 2002/12/03 12:24:00 billhorsman
156: fixed fatal sql exception
157:
158: Revision 1.17 2002/11/12 20:24:12 billhorsman
159: checkstyle
160:
161: Revision 1.16 2002/11/12 20:18:23 billhorsman
162: Made connection resetter a bit more friendly. Now, if it encounters any problems
163: during reset then that connection is thrown away. This is going to cause you
164: problems if you always close connections in an unstable state (e.g. with transactions
165: open. But then again, it's better to know about that as soon as possible, right?
166:
167: Revision 1.15 2002/11/07 18:56:22 billhorsman
168: fixed NullPointerException introduced yesterday on isClose() method
169:
170: Revision 1.14 2002/11/07 12:38:04 billhorsman
171: performance improvement - only reset when it might be necessary
172:
173: Revision 1.13 2002/11/06 20:26:49 billhorsman
174: improved doc, added connection resetting, and made
175: isClosed() work correctly
176:
177: Revision 1.12 2002/11/02 13:57:33 billhorsman
178: checkstyle
179:
180: Revision 1.11 2002/10/30 21:25:09 billhorsman
181: move createStatement into ProxyFactory
182:
183: Revision 1.10 2002/10/30 21:19:17 billhorsman
184: make use of ProxyFactory
185:
186: Revision 1.9 2002/10/28 19:51:34 billhorsman
187: Fixed NullPointerException when calling connection.createProxyStatement()
188:
189: Revision 1.8 2002/10/28 19:28:25 billhorsman
190: checkstyle
191:
192: Revision 1.7 2002/10/28 08:20:23 billhorsman
193: draft sql dump stuff
194:
195: Revision 1.6 2002/10/25 15:59:32 billhorsman
196: made non-public where possible
197:
198: Revision 1.5 2002/10/24 18:15:09 billhorsman
199: removed unnecessary debug
200:
201: Revision 1.4 2002/10/17 15:29:18 billhorsman
202: fixes so that equals() works
203:
204: Revision 1.3 2002/09/19 10:33:57 billhorsman
205: added ProxyConnection#toString
206:
207: Revision 1.2 2002/09/18 13:48:56 billhorsman
208: checkstyle and doc
209:
210: Revision 1.1.1.1 2002/09/13 08:13:30 billhorsman
211: new
212:
213: Revision 1.10 2002/08/24 19:57:15 billhorsman
214: checkstyle changes
215:
216: Revision 1.9 2002/08/24 19:42:26 billhorsman
217: new proxy stuff to work with JDK 1.4
218:
219: Revision 1.6 2002/07/02 11:19:08 billhorsman
220: layout code and imports
221:
222: Revision 1.5 2002/06/28 11:19:47 billhorsman
223: improved doc
224:
225: */
|