[Proxool-cvs] proxool/src/java/org/logicalcobwebs/proxool ProxyDatabaseMetaData.java,1.7,1.8 ProxyFa
UNMAINTAINED!
Brought to you by:
billhorsman
From: <bil...@us...> - 2004-06-02 20:50:57
|
Update of /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/proxool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20116/src/java/org/logicalcobwebs/proxool Modified Files: ProxyDatabaseMetaData.java ProxyFactory.java WrappedConnection.java Log Message: Dropped obsolete InvocationHandler reference and injectable interface stuff. Index: ProxyDatabaseMetaData.java =================================================================== RCS file: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/proxool/ProxyDatabaseMetaData.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ProxyDatabaseMetaData.java 23 Mar 2004 21:19:45 -0000 1.7 --- ProxyDatabaseMetaData.java 2 Jun 2004 20:50:47 -0000 1.8 *************** *** 11,20 **** import org.logicalcobwebs.cglib.proxy.MethodInterceptor; import org.logicalcobwebs.cglib.proxy.MethodProxy; - import org.logicalcobwebs.cglib.proxy.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Connection; - import java.sql.SQLException; import java.sql.DatabaseMetaData; --- 11,18 ---- *************** *** 26,30 **** * @author $Author$ (current maintainer) */ ! class ProxyDatabaseMetaData implements MethodInterceptor, InvocationHandler { private static final Log LOG = LogFactory.getLog(ProxyDatabaseMetaData.class); --- 24,28 ---- * @author $Author$ (current maintainer) */ ! class ProxyDatabaseMetaData implements MethodInterceptor { private static final Log LOG = LogFactory.getLog(ProxyDatabaseMetaData.class); *************** *** 40,53 **** private Connection wrappedConnection; ! public ProxyDatabaseMetaData(Connection connection, Connection wrappedConnection) throws SQLException { ! databaseMetaData = connection.getMetaData(); this.wrappedConnection = wrappedConnection; } ! public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { ! return invoke(proxy, method, args); ! } ! ! public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; int argCount = args != null ? args.length : 0; --- 38,51 ---- private Connection wrappedConnection; ! /** ! * @param databaseMetaData the meta data we use to delegate all calls to (except getConnection()) ! * @param wrappedConnection the connection we return if asked for the connection ! */ ! public ProxyDatabaseMetaData(DatabaseMetaData databaseMetaData, Connection wrappedConnection) { ! this.databaseMetaData = databaseMetaData; this.wrappedConnection = wrappedConnection; } ! public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { Object result = null; int argCount = args != null ? args.length : 0; *************** *** 113,116 **** --- 111,117 ---- Revision history: $Log$ + Revision 1.8 2004/06/02 20:50:47 billhorsman + Dropped obsolete InvocationHandler reference and injectable interface stuff. + Revision 1.7 2004/03/23 21:19:45 billhorsman Added disposable wrapper to proxied connection. And made proxied objects implement delegate interfaces too. Index: ProxyFactory.java =================================================================== RCS file: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/proxool/ProxyFactory.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** ProxyFactory.java 23 Mar 2004 21:19:45 -0000 1.26 --- ProxyFactory.java 2 Jun 2004 20:50:47 -0000 1.27 *************** *** 6,10 **** package org.logicalcobwebs.proxool; ! import org.logicalcobwebs.cglib.proxy.Proxy; import org.logicalcobwebs.logging.Log; import org.logicalcobwebs.logging.LogFactory; --- 6,11 ---- package org.logicalcobwebs.proxool; ! import org.logicalcobwebs.cglib.proxy.Enhancer; ! import org.logicalcobwebs.cglib.proxy.Factory; import org.logicalcobwebs.logging.Log; import org.logicalcobwebs.logging.LogFactory; *************** *** 12,17 **** import java.sql.Connection; import java.sql.DatabaseMetaData; - import java.sql.SQLException; import java.sql.Statement; import java.util.HashSet; import java.util.Set; --- 13,19 ---- import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Statement; + import java.sql.CallableStatement; + import java.sql.PreparedStatement; import java.util.HashSet; import java.util.Set; *************** *** 44,50 **** protected static Connection getWrappedConnection(ProxyConnection proxyConnection) { final WrappedConnection wrappedConnection = new WrappedConnection(proxyConnection); Object delegate = Proxy.newProxyInstance( proxyConnection.getConnection().getClass().getClassLoader(), ! getInterfaces(proxyConnection.getConnection().getClass()), wrappedConnection); return (Connection) delegate; --- 46,64 ---- protected static Connection getWrappedConnection(ProxyConnection proxyConnection) { final WrappedConnection wrappedConnection = new WrappedConnection(proxyConnection); + /* Object delegate = Proxy.newProxyInstance( proxyConnection.getConnection().getClass().getClassLoader(), ! getInterfaces(proxyConnection.getConnection().getClass(), proxyConnection.getConnectionPool()), ! wrappedConnection); ! */ ! /* ! Object delegate = Enhancer.create( ! proxyConnection.getConnection().getClass(), ! getInterfaces(proxyConnection.getConnection().getClass(), proxyConnection.getConnectionPool()), ! wrappedConnection); ! */ ! Object delegate = Enhancer.create( ! null, ! getInterfaces(proxyConnection.getConnection().getClass(), proxyConnection.getConnectionPool()), wrappedConnection); return (Connection) delegate; *************** *** 60,64 **** --- 74,81 ---- protected static Statement getDelegateStatement(Statement statement) { Statement ds = statement; + /* ProxyStatement ps = (ProxyStatement) Proxy.getInvocationHandler(statement); + */ + ProxyStatement ps = (ProxyStatement) ((Factory)statement).getCallback(0); ds = ps.getDelegateStatement(); return ds; *************** *** 73,85 **** */ protected static Connection getDelegateConnection(Connection connection) { WrappedConnection wc = (WrappedConnection) Proxy.getInvocationHandler(connection); return wc.getProxyConnection().getConnection(); } protected static Statement getStatement(Statement delegate, ConnectionPool connectionPool, ProxyConnectionIF proxyConnection, String sqlStatement) { return (Statement) Proxy.newProxyInstance( delegate.getClass().getClassLoader(), ! getInterfaces(delegate.getClass()), new ProxyStatement(delegate, connectionPool, proxyConnection, sqlStatement)); } --- 90,118 ---- */ protected static Connection getDelegateConnection(Connection connection) { + /* WrappedConnection wc = (WrappedConnection) Proxy.getInvocationHandler(connection); + */ + WrappedConnection wc = (WrappedConnection) ((Factory)connection).getCallback(0); return wc.getProxyConnection().getConnection(); } protected static Statement getStatement(Statement delegate, ConnectionPool connectionPool, ProxyConnectionIF proxyConnection, String sqlStatement) { + Object o = Enhancer.create( + null, + getInterfaces(delegate.getClass(), connectionPool), + new ProxyStatement(delegate, connectionPool, proxyConnection, sqlStatement)); + /* + Object o = Enhancer.create( + delegate.getClass(), + getInterfaces(delegate.getClass(), connectionPool), + new ProxyStatement(delegate, connectionPool, proxyConnection, sqlStatement)); + */ + return (Statement) o; + /* return (Statement) Proxy.newProxyInstance( delegate.getClass().getClassLoader(), ! getInterfaces(delegate.getClass(), connectionPool), new ProxyStatement(delegate, connectionPool, proxyConnection, sqlStatement)); + */ } *************** *** 91,99 **** * @return an array of classes (all interfaces) that this class implements. */ ! private static Class[] getInterfaces(Class clazz) { Class[] interfaceArray = (Class[]) interfaceMap.get(clazz); if (interfaceArray == null) { Set interfaces = new HashSet(); traverseInterfacesRecursively(interfaces, clazz); interfaceArray = (Class[]) interfaces.toArray(new Class[interfaces.size()]); /* --- 124,151 ---- * @return an array of classes (all interfaces) that this class implements. */ ! private static Class[] getInterfaces(Class clazz, ConnectionPool connectionPool) { Class[] interfaceArray = (Class[]) interfaceMap.get(clazz); if (interfaceArray == null) { Set interfaces = new HashSet(); traverseInterfacesRecursively(interfaces, clazz); + if (connectionPool != null) { + // Work out which interface we should be injecting (if it has been configured). Make sure + // we check CallableStatement then PreparedStatement then Statement or all three will get + // caught by Statement + Class injectableClass = null; + if (Connection.class.isAssignableFrom(clazz)) { + injectableClass = connectionPool.getDefinition().getInjectableConnectionInterface(); + } else if (CallableStatement.class.isAssignableFrom(clazz)) { + injectableClass = connectionPool.getDefinition().getInjectableCallableStatementInterface(); + } else if (PreparedStatement.class.isAssignableFrom(clazz)) { + injectableClass = connectionPool.getDefinition().getInjectablePreparedStatementInterface(); + } else if (Statement.class.isAssignableFrom(clazz)) { + injectableClass = connectionPool.getDefinition().getInjectableStatementInterface(); + } + // Inject it if it was configured. + if (injectableClass != null) { + interfaces.add(injectableClass); + } + } interfaceArray = (Class[]) interfaces.toArray(new Class[interfaces.size()]); /* *************** *** 166,174 **** /** * Create a new DatabaseMetaData from a connection ! * @param connection the proxy connection we are using * @return databaseMetaData - * @throws SQLException if the delegfate connection couldn't get the metaData */ ! protected static DatabaseMetaData getDatabaseMetaData(Connection connection, Connection wrappedConnection) throws SQLException { return (DatabaseMetaData) Proxy.newProxyInstance( DatabaseMetaData.class.getClassLoader(), --- 218,227 ---- /** * Create a new DatabaseMetaData from a connection ! * @param databaseMetaData the meta data we use to delegate all calls to (except getConnection()) ! * @param wrappedConnection the connection we return if asked for the connection * @return databaseMetaData */ ! protected static DatabaseMetaData getDatabaseMetaData(DatabaseMetaData databaseMetaData, Connection wrappedConnection) { ! /* return (DatabaseMetaData) Proxy.newProxyInstance( DatabaseMetaData.class.getClassLoader(), *************** *** 176,179 **** --- 229,244 ---- new ProxyDatabaseMetaData(connection, wrappedConnection) ); + */ + ProxyDatabaseMetaData proxyDatabaseMetaData = new ProxyDatabaseMetaData(databaseMetaData, wrappedConnection); + Object delegate = Enhancer.create( + null, + getInterfaces(databaseMetaData.getClass(), null), + proxyDatabaseMetaData); + /* + Object delegate = Enhancer.create( + databaseMetaData.getClass(), + proxyDatabaseMetaData); + */ + return (DatabaseMetaData) delegate; } *************** *** 184,188 **** --- 249,256 ---- */ public static WrappedConnection getWrappedConnection(Connection connection) { + /* return (WrappedConnection) Proxy.getInvocationHandler(connection); + */ + return (WrappedConnection) ((Factory)connection).getCallback(0); } *************** *** 192,195 **** --- 260,266 ---- Revision history: $Log$ + Revision 1.27 2004/06/02 20:50:47 billhorsman + Dropped obsolete InvocationHandler reference and injectable interface stuff. + Revision 1.26 2004/03/23 21:19:45 billhorsman Added disposable wrapper to proxied connection. And made proxied objects implement delegate interfaces too. Index: WrappedConnection.java =================================================================== RCS file: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/proxool/WrappedConnection.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WrappedConnection.java 23 Mar 2004 21:19:45 -0000 1.1 --- WrappedConnection.java 2 Jun 2004 20:50:47 -0000 1.2 *************** *** 11,14 **** --- 11,15 ---- import org.logicalcobwebs.logging.Log; import org.logicalcobwebs.logging.LogFactory; + import org.logicalcobwebs.proxool.proxy.InvokerFacade; import java.lang.reflect.Method; *************** *** 25,29 **** * @since Proxool 0.9 */ ! public class WrappedConnection implements InvocationHandler, MethodInterceptor { private static final Log LOG = LogFactory.getLog(WrappedConnection.class); --- 26,30 ---- * @since Proxool 0.9 */ ! public class WrappedConnection implements MethodInterceptor { private static final Log LOG = LogFactory.getLog(WrappedConnection.class); *************** *** 98,103 **** Object result = null; int argCount = args != null ? args.length : 0; try { ! if (method.getName().equals(CLOSE_METHOD)) { // It's okay to close a connection twice. Only we ignore the // second time. --- 99,108 ---- Object result = null; int argCount = args != null ? args.length : 0; + Method concreteMethod = method; + if (proxyConnection != null && proxyConnection.getConnection() != null) { + concreteMethod = InvokerFacade.getConcreteMethod(proxyConnection.getConnection().getClass(), method); + } try { ! if (concreteMethod.getName().equals(CLOSE_METHOD)) { // It's okay to close a connection twice. Only we ignore the // second time. *************** *** 107,134 **** proxyConnection = null; } ! } else if (method.getName().equals(EQUALS_METHOD) && argCount == 1) { result = equals(args[0]) ? Boolean.TRUE : Boolean.FALSE; ! } else if (method.getName().equals(HASH_CODE_METHOD) && argCount == 0) { result = new Integer(hashCode()); ! } else if (method.getName().equals(IS_CLOSED_METHOD) && argCount == 0) { result = (proxyConnection == null || proxyConnection.isClosed()) ? Boolean.TRUE : Boolean.FALSE; ! } else if (method.getName().equals(GET_META_DATA_METHOD) && argCount == 0) { if (proxyConnection != null) { ! result = ProxyFactory.getDatabaseMetaData(proxyConnection.getConnection(),(Connection) proxy); } else { ! throw new SQLException("You can't perform a " + method.getName() + " operation after the connection has been closed"); } ! } else if (method.getName().equals(FINALIZE_METHOD)) { super.finalize(); ! } else if (method.getName().equals(TO_STRING_METHOD)) { result = toString(); } else { if (proxyConnection != null) { ! if (method.getName().startsWith(ConnectionResetter.MUTATOR_PREFIX)) { proxyConnection.setNeedToReset(true); } ! result = method.invoke(proxyConnection.getConnection(), args); } else { ! throw new SQLException("You can't perform a " + method.getName() + " operation after the connection has been closed"); } } --- 112,140 ---- proxyConnection = null; } ! } else if (concreteMethod.getName().equals(EQUALS_METHOD) && argCount == 1) { result = equals(args[0]) ? Boolean.TRUE : Boolean.FALSE; ! } else if (concreteMethod.getName().equals(HASH_CODE_METHOD) && argCount == 0) { result = new Integer(hashCode()); ! } else if (concreteMethod.getName().equals(IS_CLOSED_METHOD) && argCount == 0) { result = (proxyConnection == null || proxyConnection.isClosed()) ? Boolean.TRUE : Boolean.FALSE; ! } else if (concreteMethod.getName().equals(GET_META_DATA_METHOD) && argCount == 0) { if (proxyConnection != null) { ! Connection connection = ProxyFactory.getWrappedConnection(proxyConnection); ! result = ProxyFactory.getDatabaseMetaData(proxyConnection.getConnection().getMetaData(), connection); } else { ! throw new SQLException("You can't perform a " + concreteMethod.getName() + " operation after the connection has been closed"); } ! } else if (concreteMethod.getName().equals(FINALIZE_METHOD)) { super.finalize(); ! } else if (concreteMethod.getName().equals(TO_STRING_METHOD)) { result = toString(); } else { if (proxyConnection != null) { ! if (concreteMethod.getName().startsWith(ConnectionResetter.MUTATOR_PREFIX)) { proxyConnection.setNeedToReset(true); } ! result = concreteMethod.invoke(proxyConnection.getConnection(), args); } else { ! throw new SQLException("You can't perform a " + concreteMethod.getName() + " operation after the connection has been closed"); } } *************** *** 161,165 **** throw e.getTargetException(); } catch (SQLException e) { ! throw new SQLException("Couldn't perform the operation " + method.getName()); } catch (Exception e) { LOG.error("Unexpected invocation exception", e); --- 167,171 ---- throw e.getTargetException(); } catch (SQLException e) { ! throw new SQLException("Couldn't perform the operation " + concreteMethod.getName()); } catch (Exception e) { LOG.error("Unexpected invocation exception", e); *************** *** 199,204 **** if (obj instanceof Connection) { final WrappedConnection wc = ProxyFactory.getWrappedConnection((Connection) obj); ! if (wc != null) { ! return wc.hashCode() == hashCode(); } else { return false; --- 205,210 ---- if (obj instanceof Connection) { final WrappedConnection wc = ProxyFactory.getWrappedConnection((Connection) obj); ! if (wc != null && wc.getId() > 0 && getId() > 0) { ! return wc.getId() == getId(); } else { return false; *************** *** 223,226 **** --- 229,235 ---- Revision history: $Log$ + Revision 1.2 2004/06/02 20:50:47 billhorsman + Dropped obsolete InvocationHandler reference and injectable interface stuff. + Revision 1.1 2004/03/23 21:19:45 billhorsman Added disposable wrapper to proxied connection. And made proxied objects implement delegate interfaces too. |