⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcproxygenerator.java

📁 c3p0数据库连接池实现源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Distributed as part of c3p0 v.0.9.1-pre6 * * Copyright (C) 2005 Machinery For Change, Inc. * * Author: Steve Waldman <swaldman@mchange.com> * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2.1, as  * published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY 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 * along with this software; see the file LICENSE.  If not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */package com.mchange.v2.c3p0.codegen;import java.io.*;import java.lang.reflect.*;import java.sql.*;import com.mchange.v2.codegen.*;import com.mchange.v2.codegen.intfc.*;import com.mchange.v2.c3p0.C3P0ProxyConnection;import com.mchange.v2.c3p0.C3P0ProxyStatement;public abstract class JdbcProxyGenerator extends DelegatorGenerator{    final static boolean PREMATURE_DETACH_DEBUG = false;    JdbcProxyGenerator()    {	this.setGenerateInnerSetter( false );	this.setGenerateInnerGetter( false );	this.setGenerateNoArgConstructor( false );	this.setGenerateWrappingConstructor( true );	this.setClassModifiers( Modifier.PUBLIC | Modifier.FINAL );	this.setMethodModifiers( Modifier.PUBLIC | Modifier.FINAL );    }    abstract String getInnerTypeName();    static final class NewProxyMetaDataGenerator extends JdbcProxyGenerator    { 	String getInnerTypeName()	{ return "DatabaseMetaData"; }	protected void generateDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    String mname   = method.getName();	    Class  retType = method.getReturnType();	    	    if ( ResultSet.class.isAssignableFrom( retType ) )		{		    iw.println("ResultSet innerResultSet = inner." + CodegenUtils.methodCall( method ) + ";");		    iw.println("if (innerResultSet == null) return null;");		    iw.println("return new NewProxyResultSet( innerResultSet, parentPooledConnection, inner, this );"); 		} 	    else if ( mname.equals( "getConnection" ) ) 		{		    iw.println("return this.proxyCon;"); 		}	    else		super.generateDelegateCode( intfcl, genclass, method, iw );	}	protected void generatePreDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    if ( method.getExceptionTypes().length > 0 )		super.generatePreDelegateCode( intfcl, genclass, method, iw );	}		protected void generatePostDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    if ( method.getExceptionTypes().length > 0 )		super.generatePostDelegateCode( intfcl, genclass, method, iw );	}	protected void generateExtraDeclarations( Class intfcl, String genclass, IndentedWriter iw ) throws IOException	{	    super.generateExtraDeclarations( intfcl, genclass, iw );	    iw.println();	    iw.println("NewProxyConnection proxyCon;");	    iw.println();	    iw.print( CodegenUtils.fqcnLastElement( genclass ) );	    iw.println("( " + CodegenUtils.simpleClassName( intfcl ) + " inner, NewPooledConnection parentPooledConnection, NewProxyConnection proxyCon )");	    iw.println("{");	    iw.upIndent();	    iw.println("this( inner, parentPooledConnection );");	    iw.println("this.proxyCon = proxyCon;");	    iw.downIndent();	    iw.println("}");	}    }    static final class NewProxyResultSetGenerator extends JdbcProxyGenerator    {	String getInnerTypeName()	{ return "ResultSet"; }	protected void generateDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    iw.println("if (proxyConn != null) proxyConn.maybeDirtyTransaction();");	    iw.println();	    String mname   = method.getName();	    Class  retType = method.getReturnType();	    if ( mname.equals("close") )		{		    iw.println("if (! this.isDetached())");		    iw.println("{");		    iw.upIndent();		    iw.println("if (creator instanceof Statement)");		    iw.upIndent(); 		    iw.println("parentPooledConnection.markInactiveResultSetForStatement( (Statement) creator, inner );");		    iw.downIndent();		    iw.println("else if (creator instanceof DatabaseMetaData)");		    iw.upIndent(); 		    iw.println("parentPooledConnection.markInactiveMetaDataResultSet( inner );");		    iw.downIndent();		    iw.println("else if (creator instanceof Connection)");		    iw.upIndent(); 		    iw.println("parentPooledConnection.markInactiveRawConnectionResultSet( inner );");		    iw.downIndent();		    iw.println("else throw new InternalError(\042Must be Statement or DatabaseMetaData -- Bad Creator: \042 + creator);"); 		    iw.println("this.detach();");		    iw.println("inner.close();");		    iw.println("this.inner = null;");		    iw.downIndent();		    iw.println("}");		}	    else if ( mname.equals("getStatement") )		{		    iw.println("if (creator instanceof Statement)");		    iw.upIndent(); 		    iw.println("return (Statement) creatorProxy;");		    iw.downIndent();		    iw.println("else if (creator instanceof DatabaseMetaData)");		    iw.upIndent(); 		    iw.println("return null;");		    iw.downIndent();		    iw.println("else throw new InternalError(\042Must be Statement or DatabaseMetaData -- Bad Creator: \042 + creator);");		}	    else if ( mname.equals("isClosed") )		{		    iw.println( "return this.isDetached();" );		}	    else		super.generateDelegateCode( intfcl, genclass, method, iw );	}	protected void generateExtraDeclarations( Class intfcl, String genclass, IndentedWriter iw ) throws IOException	{	    super.generateExtraDeclarations( intfcl, genclass, iw );	    iw.println();	    iw.println("Object creator;");	    iw.println("Object creatorProxy;");	    iw.println("NewProxyConnection proxyConn;");	    iw.println();	    iw.print( CodegenUtils.fqcnLastElement( genclass ) );	    iw.println("( " + CodegenUtils.simpleClassName( intfcl ) + " inner, NewPooledConnection parentPooledConnection, Object c, Object cProxy )");	    iw.println("{");	    iw.upIndent();	    iw.println("this( inner, parentPooledConnection );");	    iw.println("this.creator      = c;");	    iw.println("this.creatorProxy = cProxy;");	    iw.println("if (creatorProxy instanceof NewProxyConnection) this.proxyConn = (NewProxyConnection) cProxy;");	    iw.downIndent();	    iw.println("}");	}	protected void generatePreDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    super.generatePreDelegateCode( intfcl, genclass, method, iw );	}    }    static final class NewProxyAnyStatementGenerator extends JdbcProxyGenerator    {	String getInnerTypeName()	{ return "Statement"; }	private final static boolean CONCURRENT_ACCESS_DEBUG = false;	{	    this.setExtraInterfaces( new Class[] { C3P0ProxyStatement.class } );	}	protected void generateDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    iw.println("maybeDirtyTransaction();");	    iw.println();	    String mname   = method.getName();	    Class  retType = method.getReturnType();	    if ( ResultSet.class.isAssignableFrom( retType ) )		{		    iw.println("ResultSet innerResultSet = inner." + CodegenUtils.methodCall( method ) + ";");		    iw.println("if (innerResultSet == null) return null;");		    iw.println("parentPooledConnection.markActiveResultSetForStatement( inner, innerResultSet );");		    iw.println("return new NewProxyResultSet( innerResultSet, parentPooledConnection, inner, this );"); 		}	    else if ( mname.equals("getConnection") )		{ 		    iw.println("if (! this.isDetached())"); 		    iw.upIndent();		    iw.println("return creatorProxy;"); 		    iw.downIndent(); 		    iw.println("else"); 		    iw.upIndent(); 		    iw.println("throw new SQLException(\"You cannot operate on a closed Statement!\");"); 		    iw.downIndent();		}	    else if ( mname.equals("close") )		{		    iw.println("if (! this.isDetached())");		    iw.println("{");		    iw.upIndent();		    		    iw.println("if ( is_cached )");		    iw.upIndent();		    iw.println("parentPooledConnection.checkinStatement( inner );");		    iw.downIndent();		    iw.println("else");		    iw.println("{");		    iw.upIndent();		    iw.println("parentPooledConnection.markInactiveUncachedStatement( inner );");		    iw.println("try{ inner.close(); }");		    iw.println("catch (Exception e )");		    iw.println("{");		    iw.upIndent();		    iw.println("if (logger.isLoggable( MLevel.WARNING ))");		    iw.upIndent();		    iw.println("logger.log( MLevel.WARNING, \042Exception on close of inner statement.\042, e);");		    iw.downIndent();		    iw.println( "SQLException sqle = SqlUtils.toSQLException( e );" );		    iw.println( "throw sqle;" );		    iw.downIndent();		    iw.println("}");		    iw.downIndent();		    iw.println("}");		    iw.println();		    iw.println("this.detach();");		    iw.println("this.inner = null;");		    iw.println("this.creatorProxy = null;");		    iw.downIndent();		    iw.println("}");		}	    else if ( mname.equals("isClosed") )		{		    iw.println( "return this.isDetached();" );		}	    else		super.generateDelegateCode( intfcl, genclass, method, iw );	}	protected void generatePreDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    // concurrent-access-debug only	    if (CONCURRENT_ACCESS_DEBUG)		{		    iw.println("Object record;");		    iw.println("synchronized (concurrentAccessRecorder)");		    iw.println("{");		    iw.upIndent();		    iw.println("record = concurrentAccessRecorder.record();");		    iw.println("int num_concurrent_clients = concurrentAccessRecorder.size();");		    iw.println("if (num_concurrent_clients != 1)");		    iw.upIndent();		    iw.println("logger.log(MLevel.WARNING, " +			       "concurrentAccessRecorder.getDump(\042Apparent concurrent access! (\042 + num_concurrent_clients + \042 clients.\042) );");		    iw.downIndent();		    iw.downIndent();		    iw.println("}");		    iw.println();		}	    // end concurrent-access-debug only	    super.generatePreDelegateCode( intfcl, genclass, method, iw );	}    	protected void generatePostDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException 	{	    super.generatePostDelegateCode( intfcl, genclass, method, iw );	    // concurrent-access-debug only	    if (CONCURRENT_ACCESS_DEBUG)		{		    iw.println("finally");		    iw.println("{");		    iw.upIndent();		    iw.println("concurrentAccessRecorder.remove( record );");		    iw.downIndent();		    iw.println("}");		}	    // end concurrent-access-debug only	}	protected void generateExtraDeclarations( Class intfcl, String genclass, IndentedWriter iw ) throws IOException	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -