📄 jdbcproxygenerator.java
字号:
iw.println("out = new NewProxyDatabaseMetaData( (DatabaseMetaData) out, parentPooledConnection );"); iw.downIndent(); iw.println("return out;"); iw.downIndent(); iw.println("}"); iw.println(); iw.println("synchronized void maybeDirtyTransaction()"); iw.println("{ txn_known_resolved = false; }"); super.generateExtraDeclarations( intfcl, genclass, iw ); } void generateFindMethodAndArgs( Method method, IndentedWriter iw ) throws IOException { iw.println("Class[] argTypes = "); iw.println("{"); iw.upIndent(); Class[] argTypes = method.getParameterTypes(); for (int i = 0, len = argTypes.length; i < len; ++i) { if (i != 0) iw.println(","); iw.print( CodegenUtils.simpleClassName( argTypes[i] ) + ".class" ); } iw.println(); iw.downIndent(); iw.println("};"); iw.println("Method method = Connection.class.getMethod( \042" + method.getName() + "\042 , argTypes );"); iw.println(); iw.println("Object[] args = "); iw.println("{"); iw.upIndent(); for (int i = 0, len = argTypes.length; i < len; ++i) { if (i != 0) iw.println(","); String argName = CodegenUtils.generatedArgumentName( i ); Class argType = argTypes[i]; if (argType.isPrimitive()) { if (argType == boolean.class) iw.print( "Boolean.valueOf( " + argName + " )" ); else if (argType == byte.class) iw.print( "new Byte( " + argName + " )" ); else if (argType == char.class) iw.print( "new Character( " + argName + " )" ); else if (argType == short.class) iw.print( "new Short( " + argName + " )" ); else if (argType == int.class) iw.print( "new Integer( " + argName + " )" ); else if (argType == long.class) iw.print( "new Long( " + argName + " )" ); else if (argType == float.class) iw.print( "new Float( " + argName + " )" ); else if (argType == double.class) iw.print( "new Double( " + argName + " )" ); } else iw.print( argName ); } iw.downIndent(); iw.println("};"); } protected void generateExtraImports( IndentedWriter iw ) throws IOException { super.generateExtraImports( iw ); iw.println("import java.lang.reflect.InvocationTargetException;"); } } protected void generatePreDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException { generateTryOpener( iw ); } protected void generatePostDelegateCode( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException { generateTryCloserAndCatch( intfcl, genclass, method, iw ); } void generateTryOpener( IndentedWriter iw ) throws IOException { iw.println("try"); iw.println("{"); iw.upIndent(); } void generateTryCloserAndCatch( Class intfcl, String genclass, Method method, IndentedWriter iw ) throws IOException { iw.downIndent(); iw.println("}"); iw.println("catch (NullPointerException exc)"); iw.println("{"); iw.upIndent(); iw.println("if ( this.isDetached() )"); iw.println("{"); iw.upIndent(); //iw.println( "System.err.print(\042probably 'cuz we're closed -- \042);" ); //iw.println( "exc.printStackTrace();" ); if ( "close".equals( method.getName() ) ) { iw.println("if (Debug.DEBUG && logger.isLoggable( MLevel.FINE ))"); iw.println("{"); iw.upIndent(); iw.println("logger.log( MLevel.FINE, this + \042: close() called more than once.\042 );"); // premature-detach-debug-debug only! if (PREMATURE_DETACH_DEBUG) { iw.println("prematureDetachRecorder.record();"); iw.println("logger.warning( prematureDetachRecorder.getDump(\042Apparent multiple close of " + getInnerTypeName() + ".\042) );"); } // end-premature-detach-debug-only! iw.downIndent(); iw.println("}"); } else { // premature-detach-debug-debug only! if (PREMATURE_DETACH_DEBUG) { iw.println("prematureDetachRecorder.record();"); iw.println("logger.warning( prematureDetachRecorder.getDump(\042Use of already detached " + getInnerTypeName() + ".\042) );"); } // end-premature-detach-debug-only! iw.println( "throw SqlUtils.toSQLException(\042You can't operate on a closed " + getInnerTypeName() + "!!!\042, exc);"); } iw.downIndent(); iw.println("}"); iw.println( "else throw exc;" ); iw.downIndent(); iw.println("}"); iw.println("catch (Exception exc)"); iw.println("{"); iw.upIndent(); iw.println("if (! this.isDetached())"); iw.println("{"); iw.upIndent(); //iw.println( "exc.printStackTrace();" ); iw.println( "throw parentPooledConnection.handleThrowable( exc );" ); iw.downIndent(); iw.println("}"); iw.println("else throw SqlUtils.toSQLException( exc );"); iw.downIndent(); iw.println("}"); } protected void generateExtraDeclarations( Class intfcl, String genclass, IndentedWriter iw ) throws IOException { // premature-detach-debug-debug only! if (PREMATURE_DETACH_DEBUG) { iw.println("com.mchange.v2.debug.ThreadNameStackTraceRecorder prematureDetachRecorder"); iw.upIndent(); iw.println("= new com.mchange.v2.debug.ThreadNameStackTraceRecorder(\042Premature Detach Recorder\042);"); iw.downIndent(); } // end-premature-detach-debug-only! iw.println("private final static MLogger logger = MLog.getLogger( \042" + genclass + "\042 );"); iw.println(); iw.println("volatile NewPooledConnection parentPooledConnection;"); iw.println(); iw.println("ConnectionEventListener cel = new ConnectionEventListener()"); iw.println("{"); iw.upIndent(); iw.println("public void connectionErrorOccurred(ConnectionEvent evt)"); iw.println("{ /* DON'T detach()... IGNORE -- this could be an ordinary error. Leave it to the PooledConnection to test, but leave proxies intact */ }"); //BAD puppy -- iw.println("{ detach(); }"); iw.println(); iw.println("public void connectionClosed(ConnectionEvent evt)"); iw.println("{ detach(); }"); iw.downIndent(); iw.println("};"); iw.println(); iw.println("void attach( NewPooledConnection parentPooledConnection )"); iw.println("{"); iw.upIndent(); iw.println("this.parentPooledConnection = parentPooledConnection;"); iw.println("parentPooledConnection.addConnectionEventListener( cel );"); iw.downIndent(); iw.println("}"); iw.println(); iw.println("private void detach()"); iw.println("{"); iw.upIndent(); // factored out so we could define debug versions... writeDetachBody(iw); iw.downIndent(); iw.println("}"); iw.println(); iw.print( CodegenUtils.fqcnLastElement( genclass ) ); iw.println("( " + CodegenUtils.simpleClassName( intfcl ) + " inner, NewPooledConnection parentPooledConnection )"); iw.println("{"); iw.upIndent(); iw.println("this( inner );"); iw.println("attach( parentPooledConnection );"); generateExtraConstructorCode( intfcl, genclass, iw ); iw.downIndent(); iw.println("}"); iw.println(); iw.println("boolean isDetached()"); iw.println("{ return (this.parentPooledConnection == null); }"); } protected void writeDetachBody(IndentedWriter iw) throws IOException { // premature-detach-debug only if (PREMATURE_DETACH_DEBUG) { iw.println("prematureDetachRecorder.record();"); iw.println("if (this.isDetached())"); iw.upIndent(); iw.println("logger.warning( prematureDetachRecorder.getDump(\042Double Detach.\042) );"); iw.downIndent(); } // end premature-detach-debug only iw.println("parentPooledConnection.removeConnectionEventListener( cel );"); iw.println("parentPooledConnection = null;"); } protected void generateExtraImports( IndentedWriter iw ) throws IOException { iw.println("import java.sql.*;"); iw.println("import javax.sql.*;"); iw.println("import com.mchange.v2.log.*;"); iw.println("import java.lang.reflect.Method;"); iw.println("import com.mchange.v2.sql.SqlUtils;"); } void generateExtraConstructorCode( Class intfcl, String genclass, IndentedWriter iw ) throws IOException {} public static void main( String[] argv ) { try { if (argv.length != 1) { System.err.println("java " + JdbcProxyGenerator.class.getName() + " <source-root-directory>"); return; } File srcroot = new File( argv[0] ); if (! srcroot.exists() || !srcroot.canWrite() ) { System.err.println(JdbcProxyGenerator.class.getName() + " -- sourceroot: " + argv[0] + " must exist and be writable"); return; } DelegatorGenerator mdgen = new NewProxyMetaDataGenerator(); DelegatorGenerator rsgen = new NewProxyResultSetGenerator(); DelegatorGenerator stgen = new NewProxyAnyStatementGenerator(); DelegatorGenerator cngen = new NewProxyConnectionGenerator(); genclass( cngen, Connection.class, "com.mchange.v2.c3p0.impl.NewProxyConnection", srcroot ); genclass( stgen, Statement.class, "com.mchange.v2.c3p0.impl.NewProxyStatement", srcroot ); genclass( stgen, PreparedStatement.class, "com.mchange.v2.c3p0.impl.NewProxyPreparedStatement", srcroot ); genclass( stgen, CallableStatement.class, "com.mchange.v2.c3p0.impl.NewProxyCallableStatement", srcroot ); genclass( rsgen, ResultSet.class, "com.mchange.v2.c3p0.impl.NewProxyResultSet", srcroot ); genclass( mdgen, DatabaseMetaData.class, "com.mchange.v2.c3p0.impl.NewProxyDatabaseMetaData", srcroot ); } catch ( Exception e ) { e.printStackTrace(); } } static void genclass( DelegatorGenerator dg, Class intfcl, String fqcn, File srcroot ) throws IOException { File genDir = new File( srcroot, dirForFqcn( fqcn ) ); if (! genDir.exists() ) { System.err.println( JdbcProxyGenerator.class.getName() + " -- creating directory: " + genDir.getAbsolutePath() ); genDir.mkdirs(); } String fileName = CodegenUtils.fqcnLastElement( fqcn ) + ".java"; Writer w = null; try { w = new BufferedWriter( new FileWriter( new File( genDir, fileName ) ) ); dg.writeDelegator( intfcl, fqcn, w ); w.flush(); System.err.println("Generated " + fileName); } finally { try { if (w != null) w.close(); } catch ( Exception e ) { e.printStackTrace(); } } } static String dirForFqcn( String fqcn ) { int last_dot = fqcn.lastIndexOf('.'); StringBuffer sb = new StringBuffer( fqcn.substring( 0, last_dot + 1) ); for (int i = 0, len = sb.length(); i < len; ++i) if (sb.charAt(i) == '.') sb.setCharAt(i, '/'); return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -