[hibernate-commits] Hibernate SVN: r13925 - in sandbox/trunk/jdbc-proxy/src: test/java/org/hibernate/jdbc/proxy and 1 other directory.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Thu Aug 16 02:38:43 EDT 2007
Author: steve.ebersole at jboss.com
Date: 2007-08-16 02:38:43 -0400 (Thu, 16 Aug 2007)
New Revision: 13925
Added:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/BasicStatementProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxyHandler.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxyHandler.java
Removed:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
Modified:
sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ProxyBuilder.java
sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
Log:
cleanup and renaming
Copied: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java (from rev 12934, sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxy.java)
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/AbstractStatementProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.HibernateException;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.JDBCServices;
+
+/**
+ * The InvocationHandler for intercepting messages to {@link Statement} proxies.
+ *
+ * @author Steve Ebersole
+ */
+public abstract class AbstractStatementProxyHandler implements InvocationHandler {
+ private static final Logger log = LoggerFactory.getLogger( AbstractStatementProxyHandler.class );
+
+ private ConnectionProxyHandler connectionProxyHandler;
+ private Connection connectionProxy;
+ private Statement statement;
+ private boolean valid = true;
+ private final int hashCode;
+
+ protected AbstractStatementProxyHandler(
+ Statement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ this.statement = statement;
+ this.connectionProxyHandler = connectionProxyHandler;
+ this.connectionProxy = connectionProxy;
+ this.hashCode = statement.hashCode();
+ }
+
+ private void errorIfInvalid() {
+ if ( !valid ) {
+ throw new HibernateException( "statment proxy is no longer valid" );
+ }
+ }
+
+ protected ConnectionProxyHandler getConnectionProxy() {
+ errorIfInvalid();
+ return connectionProxyHandler;
+ }
+
+ protected JDBCServices getJdbcServices() {
+ return getConnectionProxy().getJdbcServices();
+ }
+
+ protected JDBCContainer getJdbcContainer() {
+ return getConnectionProxy().getJdbcContainer();
+ }
+
+ protected Statement getStatement() {
+ errorIfInvalid();
+ return statement;
+ }
+
+ protected Statement getStatementWithoutChecks() {
+ return statement;
+ }
+
+ public final Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+ log.trace( "Handling invocation of statement method [{}]", methodName );
+
+ // basic Object methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "toString".equals( methodName ) ) {
+ return this.toString();
+ }
+ if ( "equals".equals( methodName ) ) {
+ return this.equals( args[0] );
+ }
+ if ( "hashCode".equals( methodName ) ) {
+ return this.hashCode();
+ }
+
+ // other methods allowed while invalid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "close".equals( methodName ) ) {
+ explicitClose( ( Statement ) proxy );
+ return null;
+ }
+ if ( "invalidate".equals( methodName ) ) {
+ invalidateHandle();
+ return null;
+ }
+
+ errorIfInvalid();
+
+ if ( "getWrappedStatement".equals( methodName ) ) {
+ return getStatementWithoutChecks();
+ }
+
+ if ( "getConnection".equals( methodName ) ) {
+ return connectionProxy;
+ }
+
+ beginningInvocationHandling( method, args );
+
+ try {
+ boolean exposingResultSet = doesMethodExposeResultSet( method );
+
+ Object result = method.invoke( statement, args );
+
+ if ( exposingResultSet ) {
+ result = ProxyBuilder.buildResultSet( ( ResultSet ) result, this, ( Statement ) proxy );
+ getJdbcContainer().register( ( ResultSet ) result );
+ }
+
+ return result;
+ }
+ catch ( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw connectionProxyHandler.getJdbcServices().getExceptionHelper()
+ .convert( ( SQLException ) realException, realException.getMessage() );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ protected void beginningInvocationHandling(Method method, Object[] args) {
+ }
+
+ private void explicitClose(Statement proxy) {
+ if ( valid ) {
+ getJdbcContainer().release( proxy );
+ }
+ }
+
+ private void invalidateHandle() {
+ connectionProxyHandler = null;
+ statement = null;
+ valid = false;
+ }
+
+ protected boolean doesMethodExposeResultSet(Method method) {
+ // todo : we still need to handle getGeneratedKeys, since the resultset there would expose the statement
+ return ResultSet.class.isAssignableFrom( method.getReturnType() )
+ && !method.getName().equals( "getGeneratedKeys" );
+ }
+
+ public String toString() {
+ return super.toString() + "[valid=" + valid + "]";
+ }
+
+ public int hashCode() {
+ return hashCode;
+ }
+}
Copied: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/BasicStatementProxyHandler.java (from rev 12914, sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java)
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/BasicStatementProxyHandler.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/BasicStatementProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.sql.Statement;
+import java.sql.Connection;
+import java.lang.reflect.Method;
+
+/**
+ * BasicStatementProxyHandler implementation
+ *
+ * @author Steve Ebersole
+ */
+public class BasicStatementProxyHandler extends AbstractStatementProxyHandler {
+ public BasicStatementProxyHandler(
+ Statement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ super( statement, connectionProxyHandler, connectionProxy );
+ }
+
+ protected void beginningInvocationHandling(Method method, Object[] args) {
+ if ( isExecution( method ) ) {
+ getJdbcServices().getSqlStatementLogger().logStatement( ( String ) args[0] );
+ }
+ }
+
+ private boolean isExecution(Method method) {
+ String methodName = method.getName();
+ return "execute".equals( methodName )
+ || "executeQuery".equals( methodName )
+ || "executeUpdate".equals( methodName );
+ }
+}
Deleted: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java 2007-08-16 04:34:59 UTC (rev 13924)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -1,145 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details. You should have received a
- * copy of the GNU Lesser General Public License, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Steve Ebersole
- */
-package org.hibernate.jdbc.proxy;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.CallableStatement;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.hibernate.HibernateException;
-import org.hibernate.jdbc.JDBCContainer;
-import org.hibernate.jdbc.JDBCServices;
-import org.hibernate.jdbc.impl.ConnectionObserver;
-import org.hibernate.jdbc.impl.LogicalConnectionImplementor;
-
-/**
- * ConnectionProxy implementation
- *
- * @author Steve Ebersole
- */
-public class ConnectionProxy implements InvocationHandler, ConnectionObserver {
-
- private static final Logger log = LoggerFactory.getLogger( ConnectionProxy.class );
-
- private boolean valid = true;
- private LogicalConnectionImplementor logicalConnection;
-
- public ConnectionProxy(LogicalConnectionImplementor logicalConnection) {
- this.logicalConnection = logicalConnection;
- this.logicalConnection.addObserver( this );
- }
-
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- String methodName = method.getName();
- log.trace( "Handling invocation of connection method [{}]", methodName );
-
- if ( "close".equals( methodName ) ) {
- explicitClose();
- return null;
- }
-
- if ( "toString".equals( methodName ) ) {
- return this.toString();
- }
-
- if ( !valid ) {
- throw new HibernateException( "connection handle is invalid" );
- }
-
- if ( "getWrappedConnection".equals( methodName ) ) {
- return extractPhysicalConnection();
- }
-
- try {
- boolean creatingBasicStatement = "createStatement".equals( methodName );
- boolean creatingPreparedStatement = "prepareStatement".equals( methodName );
- boolean creatingCallableStatement = "prepareCall".equals( methodName );
-
- Object result = method.invoke( extractPhysicalConnection(), args );
-
- if ( creatingBasicStatement || creatingPreparedStatement || creatingCallableStatement ) {
- if ( creatingPreparedStatement ) {
- result = ProxyBuilder.buildPreparedStatement( ( String ) args[0], ( PreparedStatement ) result, this );
- }
- else if ( creatingCallableStatement ) {
- result = ProxyBuilder.buildCallableStatement( ( String ) args[0], ( CallableStatement ) result, this );
- }
- else {
- result = ProxyBuilder.buildStatement( ( Statement ) result, this );
- }
- getJdbcContainer().register( ( Statement ) result );
- }
-
- return result;
- }
- catch( InvocationTargetException e ) {
- Throwable realException = e.getTargetException();
- if ( SQLException.class.isInstance( realException ) ) {
- throw logicalConnection.getJdbcServices().getExceptionHelper().convert( ( SQLException ) realException, realException.getMessage(), "???" );
- }
- else {
- throw realException;
- }
- }
- }
-
- private void explicitClose() {
- if ( valid ) {
- invalidateHandle();
- }
- }
-
- private void invalidateHandle() {
- log.trace( "Invalidating connection handle" );
- logicalConnection = null;
- valid = false;
- }
-
- private Connection extractPhysicalConnection() {
- return logicalConnection.getConnection();
- }
-
- /*package-protected*/ JDBCServices getJdbcServices() {
- return logicalConnection.getJdbcServices();
- }
-
- /*package-protected*/ JDBCContainer getJdbcContainer() {
- return logicalConnection.getJdbcContainer();
- }
-
- // JDBC ConnectionObserver impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- public void physicalConnectionObtained() {
- }
-
- public void physicalConnectionReleased() {
- log.info( "logical connection releasing its physical connection");
- }
-
- public void logicalConnectionClosed() {
- log.info( "*** logical connection closed ***" );
- invalidateHandle();
- }
-
-}
Copied: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java (from rev 12934, sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxy.java)
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ConnectionProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.HibernateException;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.JDBCServices;
+import org.hibernate.jdbc.impl.ConnectionObserver;
+import org.hibernate.jdbc.impl.LogicalConnectionImplementor;
+
+/**
+ * The InvocationHandler for intercepting messages to {@link Connection} proxies.
+ *
+ * @author Steve Ebersole
+ */
+public class ConnectionProxyHandler implements InvocationHandler, ConnectionObserver {
+ private static final Logger log = LoggerFactory.getLogger( ConnectionProxyHandler.class );
+
+ private LogicalConnectionImplementor logicalConnection;
+ private boolean valid = true;
+ private final int hashCode;
+
+ public ConnectionProxyHandler(LogicalConnectionImplementor logicalConnection) {
+ this.logicalConnection = logicalConnection;
+ this.logicalConnection.addObserver( this );
+ this.hashCode = this.logicalConnection.hashCode();
+ }
+
+ /**
+ * Access to our logical connection.
+ *
+ * @return the logical connection
+ */
+ protected LogicalConnectionImplementor getLogicalConnection() {
+ errorIfInvalid();
+ return logicalConnection;
+ }
+
+ private void errorIfInvalid() {
+ if ( !valid ) {
+ throw new HibernateException( "connection handle is invalid" );
+ }
+ }
+
+ /**
+ * Get reference to physical connection.
+ * <p/>
+ * NOTE : be sure this handler is still valid before calling!
+ *
+ * @return The physical connection
+ */
+ private Connection extractPhysicalConnection() {
+ return logicalConnection.getConnection();
+ }
+
+ /**
+ * Provide access to JDBCServices.
+ * <p/>
+ * NOTE : package-protected
+ *
+ * @return JDBCServices
+ */
+ JDBCServices getJdbcServices() {
+ return logicalConnection.getJdbcServices();
+ }
+
+ /**
+ * Provide access to JDBCContainer.
+ * <p/>
+ * NOTE : package-protected
+ *
+ * @return JDBCContainer
+ */
+ JDBCContainer getJdbcContainer() {
+ return logicalConnection.getJdbcContainer();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+ log.trace( "Handling invocation of connection method [{}]", methodName );
+
+ // basic Object methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "toString".equals( methodName ) ) {
+ return this.toString();
+ }
+ if ( "equals".equals( methodName ) ) {
+ return this.equals( args[0] );
+ }
+ if ( "hashCode".equals( methodName ) ) {
+ return this.hashCode();
+ }
+
+ // other methods allowed while invalid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "close".equals( methodName ) ) {
+ explicitClose();
+ return null;
+ }
+
+ errorIfInvalid();
+
+ if ( "getWrappedConnection".equals( methodName ) ) {
+ return extractPhysicalConnection();
+ }
+
+ try {
+ Object result = method.invoke( extractPhysicalConnection(), args );
+
+ if ( "createStatement".equals( methodName ) ) {
+ result = ProxyBuilder.buildStatement(
+ ( Statement ) result,
+ this,
+ ( Connection ) proxy
+ );
+ getJdbcContainer().register( ( Statement ) result );
+ }
+ else if ( "prepareStatement".equals( methodName ) ) {
+ result = ProxyBuilder.buildPreparedStatement(
+ ( String ) args[0],
+ ( PreparedStatement ) result,
+ this,
+ ( Connection ) proxy
+ );
+ getJdbcContainer().register( ( Statement ) result );
+ }
+ else if ( "prepareCall".equals( methodName ) ) {
+ result = ProxyBuilder.buildCallableStatement(
+ ( String ) args[0],
+ ( CallableStatement ) result,
+ this,
+ ( Connection ) proxy
+ );
+ getJdbcContainer().register( ( Statement ) result );
+ }
+
+ return result;
+ }
+ catch( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw logicalConnection.getJdbcServices().getExceptionHelper()
+ .convert( ( SQLException ) realException, realException.getMessage() );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ private void explicitClose() {
+ if ( valid ) {
+ invalidateHandle();
+ }
+ }
+
+ private void invalidateHandle() {
+ log.trace( "Invalidating connection handle" );
+ logicalConnection = null;
+ valid = false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void physicalConnectionObtained() {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void physicalConnectionReleased() {
+ log.info( "logical connection releasing its physical connection");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void logicalConnectionClosed() {
+ log.info( "*** logical connection closed ***" );
+ invalidateHandle();
+ }
+
+ public String toString() {
+ return super.toString() + "[valid=" + valid + "]";
+ }
+
+ public int hashCode() {
+ return hashCode;
+ }
+}
Deleted: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java 2007-08-16 04:34:59 UTC (rev 13924)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details. You should have received a
- * copy of the GNU Lesser General Public License, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Steve Ebersole
- */
-package org.hibernate.jdbc.proxy;
-
-import java.sql.Statement;
-import java.lang.reflect.Method;
-
-/**
- * PreparedStatementProxy implementation
- *
- * @author Steve Ebersole
- */
-public class PreparedStatementProxy extends AbstractStatementProxy {
- private final String sql;
-
- protected PreparedStatementProxy(String sql, Statement statement, ConnectionProxy connectionProxy) {
- super( statement, connectionProxy );
- connectionProxy.getJdbcServices().getSqlStatementLogger().logStatement( sql );
- this.sql = sql;
- }
-
- protected void beginningInvocationHandling(Method method, Object[] args) {
- if ( isExecution( method ) ) {
- logExecution();
- }
- else {
- journalPossibleParameterBind( method, args );
- }
- }
-
- private void journalPossibleParameterBind(Method method, Object[] args) {
- String methodName = method.getName();
- // todo : is this enough???
- if ( methodName.startsWith( "set" ) && args != null && args.length >= 2 ) {
- journalParameterBind( method, args );
- }
- }
-
- private void journalParameterBind(Method method, Object[] args) {
- }
-
- private boolean isExecution(Method method) {
- return false;
- }
-
- private void logExecution() {
- }
-}
Copied: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxyHandler.java (from rev 12934, sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxy.java)
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxyHandler.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/PreparedStatementProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.sql.Statement;
+import java.sql.Connection;
+import java.lang.reflect.Method;
+
+/**
+ * PreparedStatementProxyHandler implementation
+ *
+ * @author Steve Ebersole
+ */
+public class PreparedStatementProxyHandler extends AbstractStatementProxyHandler {
+ private final String sql;
+
+ protected PreparedStatementProxyHandler(
+ String sql,
+ Statement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ super( statement, connectionProxyHandler, connectionProxy );
+ connectionProxyHandler.getJdbcServices().getSqlStatementLogger().logStatement( sql );
+ this.sql = sql;
+ }
+
+ protected void beginningInvocationHandling(Method method, Object[] args) {
+ if ( isExecution( method ) ) {
+ logExecution();
+ }
+ else {
+ journalPossibleParameterBind( method, args );
+ }
+ }
+
+ private void journalPossibleParameterBind(Method method, Object[] args) {
+ String methodName = method.getName();
+ // todo : is this enough???
+ if ( methodName.startsWith( "set" ) && args != null && args.length >= 2 ) {
+ journalParameterBind( method, args );
+ }
+ }
+
+ private void journalParameterBind(Method method, Object[] args) {
+ }
+
+ private boolean isExecution(Method method) {
+ return false;
+ }
+
+ private void logExecution() {
+ }
+}
Modified: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ProxyBuilder.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ProxyBuilder.java 2007-08-16 04:34:59 UTC (rev 13924)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ProxyBuilder.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -32,18 +32,32 @@
*/
public class ProxyBuilder {
- // static for now simply to alleviate question about where to keep it...
+ public static final Class[] CONNECTION_PROXY_INTERFACES = new Class[] {
+ Connection.class,
+ ConnectionWrapper.class
+ };
- public static final Class[] CONNECTION_PROXY_INTERFACES = new Class[] { Connection.class, ConnectionWrapper.class };
+ public static final Class[] STMNT_PROXY_INTERFACES = new Class[] {
+ Statement.class,
+ StatementWrapperImplementor.class
+ };
+ public static final Class[] PREPARED_STMNT_PROXY_INTERFACES = new Class[] {
+ PreparedStatement.class,
+ StatementWrapperImplementor.class
+ };
+ public static final Class[] CALLABLE_STMNT_PROXY_INTERFACES = new Class[] {
+ CallableStatement.class,
+ StatementWrapperImplementor.class
+ };
- public static final Class[] STMNT_PROXY_INTERFACES = new Class[] { Statement.class, StatementWrapperImplementor.class };
- public static final Class[] PREPARED_STMNT_PROXY_INTERFACES = new Class[] { PreparedStatement.class, StatementWrapperImplementor.class };
- public static final Class[] CALLABLE_STMNT_PROXY_INTERFACES = new Class[] { CallableStatement.class, StatementWrapperImplementor.class };
+ public static final Class[] RESULTSET_PROXY_INTERFACES = new Class[] {
+ ResultSet.class,
+ ResultSetWrapperImplementor.class
+ };
- public static final Class[] RESULTSET_PROXY_INTERFACES = new Class[] { ResultSet.class, ResultSetWrapperImplementor.class };
public static Connection buildConnection(LogicalConnectionImplementor logicalConnection) {
- ConnectionProxy proxyHandler = new ConnectionProxy( logicalConnection );
+ ConnectionProxyHandler proxyHandler = new ConnectionProxyHandler( logicalConnection );
return ( Connection ) Proxy.newProxyInstance(
ConnectionWrapper.class.getClassLoader(),
CONNECTION_PROXY_INTERFACES,
@@ -51,8 +65,15 @@
);
}
- public static Statement buildStatement(Statement statement, ConnectionProxy connectionProxy) {
- StatementProxy proxyHandler = new StatementProxy( statement, connectionProxy );
+ public static Statement buildStatement(
+ Statement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ BasicStatementProxyHandler proxyHandler = new BasicStatementProxyHandler(
+ statement,
+ connectionProxyHandler,
+ connectionProxy
+ );
return ( Statement ) Proxy.newProxyInstance(
StatementWrapperImplementor.class.getClassLoader(),
STMNT_PROXY_INTERFACES,
@@ -60,8 +81,17 @@
);
}
- public static PreparedStatement buildPreparedStatement(String sql, PreparedStatement statement, ConnectionProxy connectionProxy) {
- PreparedStatementProxy proxyHandler = new PreparedStatementProxy( sql, statement, connectionProxy );
+ public static PreparedStatement buildPreparedStatement(
+ String sql,
+ Statement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ PreparedStatementProxyHandler proxyHandler = new PreparedStatementProxyHandler(
+ sql,
+ statement,
+ connectionProxyHandler,
+ connectionProxy
+ );
return ( PreparedStatement ) Proxy.newProxyInstance(
StatementWrapperImplementor.class.getClassLoader(),
PREPARED_STMNT_PROXY_INTERFACES,
@@ -69,8 +99,17 @@
);
}
- public static CallableStatement buildCallableStatement(String sql, CallableStatement statement, ConnectionProxy connectionProxy) {
- PreparedStatementProxy proxyHandler = new PreparedStatementProxy( sql, statement, connectionProxy );
+ public static CallableStatement buildCallableStatement(
+ String sql,
+ CallableStatement statement,
+ ConnectionProxyHandler connectionProxyHandler,
+ Connection connectionProxy) {
+ PreparedStatementProxyHandler proxyHandler = new PreparedStatementProxyHandler(
+ sql,
+ statement,
+ connectionProxyHandler,
+ connectionProxy
+ );
return ( CallableStatement ) Proxy.newProxyInstance(
StatementWrapperImplementor.class.getClassLoader(),
CALLABLE_STMNT_PROXY_INTERFACES,
@@ -78,8 +117,11 @@
);
}
- public static ResultSet buildResultSet(ResultSet resultSet, AbstractStatementProxy statementProxy) {
- ResultSetProxy proxyHandler = new ResultSetProxy( resultSet, statementProxy );
+ public static ResultSet buildResultSet(
+ ResultSet resultSet,
+ AbstractStatementProxyHandler statementProxyHandler,
+ Statement statementProxy) {
+ ResultSetProxyHandler proxyHandler = new ResultSetProxyHandler( resultSet, statementProxyHandler, statementProxy );
return ( ResultSet ) Proxy.newProxyInstance(
ResultSetWrapperImplementor.class.getClassLoader(),
RESULTSET_PROXY_INTERFACES,
Copied: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxyHandler.java (from rev 12934, sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxy.java)
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxyHandler.java (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/ResultSetProxyHandler.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.HibernateException;
+import org.hibernate.jdbc.JDBCContainer;
+import org.hibernate.jdbc.JDBCServices;
+
+/**
+ * The InvocationHandler for intercepting messages to {@link ResultSet} proxies.
+ *
+ * @author Steve Ebersole
+ */
+public class ResultSetProxyHandler implements InvocationHandler {
+ private static final Logger log = LoggerFactory.getLogger( ResultSetProxyHandler.class );
+
+ private AbstractStatementProxyHandler statementProxyHandler;
+ private Statement statementProxy;
+ private ResultSet resultSet;
+ private boolean valid = true;
+ private final int hashCode;
+
+ public ResultSetProxyHandler(
+ ResultSet resultSet,
+ AbstractStatementProxyHandler statementProxyHandler,
+ Statement statementProxy) {
+ this.resultSet = resultSet;
+ this.statementProxyHandler = statementProxyHandler;
+ this.statementProxy = statementProxy;
+ this.hashCode = resultSet.hashCode();
+ }
+
+ protected ResultSet getResultSet() {
+ errorIfInvalid();
+ return resultSet;
+ }
+
+ protected ResultSet getResultSetWithoutChecks() {
+ return resultSet;
+ }
+
+ protected void errorIfInvalid() {
+ if ( !valid ) {
+ throw new HibernateException( "resultset handle is no longer valid" );
+ }
+ }
+
+ protected AbstractStatementProxyHandler getStatementProxy() {
+ return statementProxyHandler;
+ }
+
+ protected JDBCServices getJdbcServices() {
+ return getStatementProxy().getJdbcServices();
+ }
+
+ protected JDBCContainer getJdbcContainer() {
+ return getStatementProxy().getJdbcContainer();
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String methodName = method.getName();
+ log.trace( "Handling invocation of resultset method [{}]", methodName );
+
+ // basic Object methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "toString".equals( methodName ) ) {
+ return this.toString();
+ }
+ if ( "equals".equals( methodName ) ) {
+ return this.equals( args[0] );
+ }
+ if ( "hashCode".equals( methodName ) ) {
+ return this.hashCode();
+ }
+
+ // other methods allowed while invalid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ if ( "close".equals( methodName ) ) {
+ explicitClose( ( ResultSet ) proxy );
+ return null;
+ }
+ if ( "invalidate".equals( methodName ) ) {
+ invalidateHandle();
+ return null;
+ }
+
+ errorIfInvalid();
+
+ if ( "getWrappedResultSet".equals( methodName ) ) {
+ return getResultSetWithoutChecks();
+ }
+
+ if ( "getStatement".equals( methodName ) ) {
+ return statementProxy;
+ }
+
+ try {
+ return method.invoke( resultSet, args );
+ }
+ catch ( InvocationTargetException e ) {
+ Throwable realException = e.getTargetException();
+ if ( SQLException.class.isInstance( realException ) ) {
+ throw getJdbcServices().getExceptionHelper()
+ .convert( ( SQLException ) realException, realException.getMessage() );
+ }
+ else {
+ throw realException;
+ }
+ }
+ }
+
+ private void explicitClose(ResultSet proxy) {
+ if ( valid ) {
+ statementProxyHandler.getJdbcContainer().release( proxy );
+ }
+ }
+
+ private void invalidateHandle() {
+ statementProxyHandler = null;
+ resultSet = null;
+ valid = false;
+ }
+
+ public String toString() {
+ return super.toString() + "[valid=" + valid + "]";
+ }
+
+ public int hashCode() {
+ return hashCode;
+ }
+}
Deleted: sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java 2007-08-16 04:34:59 UTC (rev 13924)
+++ sandbox/trunk/jdbc-proxy/src/main/java/org/hibernate/jdbc/proxy/StatementProxy.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details. You should have received a
- * copy of the GNU Lesser General Public License, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Steve Ebersole
- */
-package org.hibernate.jdbc.proxy;
-
-import java.sql.Statement;
-import java.lang.reflect.Method;
-
-/**
- * StatementProxy implementation
- *
- * @author Steve Ebersole
- */
-public class StatementProxy extends AbstractStatementProxy {
- protected StatementProxy(Statement statement, ConnectionProxy connectionProxy) {
- super( statement, connectionProxy );
- }
-
- protected void beginningInvocationHandling(Method method, Object[] args) {
- if ( isExecution( method ) ) {
- getJdbcServices().getSqlStatementLogger().logStatement( ( String ) args[0] );
- }
- }
-
- private boolean isExecution(Method method) {
- String methodName = method.getName();
- return "execute".equals( methodName )
- || "executeQuery".equals( methodName )
- || "executeUpdate".equals( methodName );
- }
-}
Modified: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-16 04:34:59 UTC (rev 13924)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java 2007-08-16 06:38:43 UTC (rev 13925)
@@ -20,7 +20,6 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,12 +58,7 @@
@Test
public void testBasicJdbcUsage() throws JDBCException {
LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_TRANSACTION, services );
- ConnectionProxy proxyHandler = new ConnectionProxy( logicalConnection );
- Connection proxiedConnection = ( Connection ) Proxy.newProxyInstance(
- getClass().getClassLoader(),
- new Class[] { Connection.class, ConnectionWrapper.class },
- proxyHandler
- );
+ Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
try {
try {
More information about the hibernate-commits
mailing list