1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import org.logicalcobwebs.cglib.proxy.MethodInterceptor; 12 import org.logicalcobwebs.cglib.proxy.MethodProxy; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.sql.Connection ; 17 import java.sql.DatabaseMetaData ; 18 19 26 class ProxyDatabaseMetaData implements MethodInterceptor { 27 28 private static final Log LOG = LogFactory.getLog(ProxyDatabaseMetaData.class); 29 30 private static final String GET_CONNECTION_METHOD = "getConnection"; 31 32 private static final String EQUALS_METHOD = "equals"; 33 34 private static final String FINALIZE_METHOD = "finalize"; 35 36 private DatabaseMetaData databaseMetaData; 37 38 private Connection wrappedConnection; 39 40 44 public ProxyDatabaseMetaData(DatabaseMetaData databaseMetaData, Connection wrappedConnection) { 45 this.databaseMetaData = databaseMetaData; 46 this.wrappedConnection = wrappedConnection; 47 } 48 49 public Object intercept(Object proxy, Method method, Object [] args, MethodProxy methodProxy) throws Throwable { 50 Object result = null; 51 int argCount = args != null ? args.length : 0; 52 try { 53 if (method.getName().equals(GET_CONNECTION_METHOD)) { 54 result = getConnection(); 55 } else if (method.getName().equals(EQUALS_METHOD) && argCount == 1) { 56 result = new Boolean (equals(args[0])); 57 } else if (method.getName().equals(FINALIZE_METHOD)) { 58 super.finalize(); 59 } else { 60 result = method.invoke(getDatabaseMetaData(), args); 61 } 62 } catch (InvocationTargetException e) { 63 throw e.getTargetException(); 64 } catch (Exception e) { 65 LOG.error("Unexpected invocation exception", e); 66 throw new RuntimeException ("Unexpected invocation exception: " 67 + e.getMessage()); 68 } 69 70 return result; 71 } 72 73 79 public boolean equals(Object obj) { 80 return databaseMetaData.hashCode() == obj.hashCode(); 81 } 82 83 89 public Connection getConnection() { 90 return wrappedConnection; 91 } 92 93 97 protected DatabaseMetaData getDatabaseMetaData() { 98 return databaseMetaData; 99 } 100 101 104 public String toString() { 105 return databaseMetaData.toString(); 106 } 107 108 } 109 110 221 | Popular Tags |