1 16 package com.ibatis.common.jdbc.logging; 17 18 import com.ibatis.common.beans.ClassInfo; 19 import com.ibatis.common.logging.Log; 20 import com.ibatis.common.logging.LogFactory; 21 22 import java.lang.reflect.InvocationHandler ; 23 import java.lang.reflect.Method ; 24 import java.lang.reflect.Proxy ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.Statement ; 28 29 32 public class ConnectionLogProxy extends BaseLogProxy implements InvocationHandler { 33 34 private static final Log log = LogFactory.getLog(Connection .class); 35 36 private Connection connection; 37 38 private ConnectionLogProxy(Connection conn) { 39 super(); 40 this.connection = conn; 41 if (log.isDebugEnabled()) { 42 log.debug("{conn-" + id + "} Connection"); 43 } 44 } 45 46 public Object invoke(Object proxy, Method method, Object [] params) 47 throws Throwable { 48 try { 49 if ("prepareStatement".equals(method.getName())) { 50 PreparedStatement stmt = (PreparedStatement ) method.invoke(connection, params); 51 stmt = PreparedStatementLogProxy.newInstance(stmt, (String ) params[0]); 52 return stmt; 53 } else if ("prepareCall".equals(method.getName())) { 54 PreparedStatement stmt = (PreparedStatement ) method.invoke(connection, params); 55 stmt = PreparedStatementLogProxy.newInstance(stmt, (String ) params[0]); 56 return stmt; 57 } else if ("createStatement".equals(method.getName())) { 58 Statement stmt = (Statement ) method.invoke(connection, params); 59 stmt = StatementLogProxy.newInstance(stmt); 60 return stmt; 61 } else { 62 return method.invoke(connection, params); 63 } 64 } catch (Throwable t) { 65 throw ClassInfo.unwrapThrowable(t); 66 } 67 68 } 69 70 75 public static Connection newInstance(Connection conn) { 76 InvocationHandler handler = new ConnectionLogProxy(conn); 77 ClassLoader cl = Connection .class.getClassLoader(); 78 return (Connection ) Proxy.newProxyInstance(cl, new Class []{Connection .class}, handler); 79 } 80 81 } 82 | Popular Tags |