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 import org.logicalcobwebs.cglib.proxy.InvocationHandler; 14 import org.logicalcobwebs.proxool.proxy.InvokerFacade; 15 16 import java.lang.reflect.InvocationTargetException ; 17 import java.lang.reflect.Method ; 18 import java.sql.Statement ; 19 import java.sql.Connection ; 20 21 30 class ProxyStatement extends AbstractProxyStatement implements MethodInterceptor { 31 32 private static final Log LOG = LogFactory.getLog(ProxyStatement.class); 33 34 private static final String EXECUTE_FRAGMENT = "execute"; 35 36 private static final String EXECUTE_BATCH_METHOD = "executeBatch"; 37 38 private static final String ADD_BATCH_METHOD = "addBatch"; 39 40 private static final String EQUALS_METHOD = "equals"; 41 42 private static final String CLOSE_METHOD = "close"; 43 44 private static final String GET_CONNECTION_METHOD = "getConnection"; 45 46 private static final String FINALIZE_METHOD = "finalize"; 47 48 private static final String SET_NULL_METHOD = "setNull"; 49 50 private static final String SET_PREFIX = "set"; 51 52 public ProxyStatement(Statement statement, ConnectionPool connectionPool, ProxyConnectionIF proxyConnection, String sqlStatement) { 53 super(statement, connectionPool, proxyConnection, sqlStatement); 54 } 55 56 public Object intercept(Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable { 57 return invoke(proxy, method, args); 58 } 59 60 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 61 Object result = null; 62 long startTime = System.currentTimeMillis(); 63 final int argCount = args != null ? args.length : 0; 64 65 Method concreteMethod = InvokerFacade.getConcreteMethod(getStatement().getClass(), method); 66 67 if (concreteMethod.getName().equals(ADD_BATCH_METHOD)) { 69 if (argCount > 0 && args[0] instanceof String ) { 71 setSqlStatementIfNull((String ) args[0]); 72 } 73 appendToSqlLog(); 74 } else if (concreteMethod.getName().equals(EXECUTE_BATCH_METHOD)) { 75 startExecute(); 77 } else if (concreteMethod.getName().startsWith(EXECUTE_FRAGMENT)) { 78 if (argCount > 0 && args[0] instanceof String ) { 80 setSqlStatementIfNull((String ) args[0]); 81 } 82 appendToSqlLog(); 83 startExecute(); 84 } 85 86 Exception exception = null; 89 try { 90 if (concreteMethod.getName().equals(EQUALS_METHOD) && argCount == 1) { 91 result = (equals(args[0])) ? Boolean.TRUE : Boolean.FALSE; 92 } else if (concreteMethod.getName().equals(CLOSE_METHOD) && argCount == 0) { 93 close(); 94 } else if (concreteMethod.getName().equals(GET_CONNECTION_METHOD) && argCount == 0) { 95 result = getConnection(); 96 } else if (concreteMethod.getName().equals(FINALIZE_METHOD) && argCount == 0) { 97 finalize(); 98 } else { 99 try { 100 result = concreteMethod.invoke(getStatement(), args); 101 } catch (IllegalAccessException e) { 102 LOG.debug("Ignoring IllegalAccessException whilst invoking the " + concreteMethod + " concrete method and trying the " + method + " method directly."); 108 InvokerFacade.overrideConcreteMethod(getStatement().getClass(), method, method); 111 result = method.invoke(getStatement(), args); 112 } 113 } 114 115 if (isTrace()) { 117 try { 118 119 if (concreteMethod.getName().equals(SET_NULL_METHOD) && argCount > 0 && args[0] instanceof Integer ) { 121 int index = ((Integer ) args[0]).intValue(); 122 putParameter(index, null); 123 } else if (concreteMethod.getName().startsWith(SET_PREFIX) && argCount > 1 && args[0] instanceof Integer ) { 124 int index = ((Integer ) args[0]).intValue(); 125 putParameter(index, args[1]); 126 } 127 128 } catch (Exception e) { 129 LOG.error("Ignoring error during dump", e); 131 } 132 } 133 } catch (InvocationTargetException e) { 134 if (e.getTargetException() instanceof Exception ) { 135 exception = (Exception ) e.getTargetException(); 136 } else { 137 exception = e; 138 } 139 if (testException(e.getTargetException())) { 140 FatalSqlExceptionHelper.throwFatalSQLException(getConnectionPool().getDefinition().getFatalSqlExceptionWrapper(), e.getTargetException()); 142 } 143 throw e.getTargetException(); 144 } catch (Exception e) { 145 exception = e; 146 if (testException(e)) { 147 FatalSqlExceptionHelper.throwFatalSQLException(getConnectionPool().getDefinition().getFatalSqlExceptionWrapper(), e); 149 } 150 throw e; 151 } finally { 152 153 if (concreteMethod.getName().equals(EXECUTE_BATCH_METHOD) || concreteMethod.getName().startsWith(EXECUTE_FRAGMENT)) { 155 trace(startTime, exception); 156 } 157 158 } 159 160 return result; 161 162 } 163 164 } 165 166 284 | Popular Tags |