1 6 package org.logicalcobwebs.proxool; 7 8 import org.logicalcobwebs.cglib.proxy.InvocationHandler; 9 import org.logicalcobwebs.cglib.proxy.MethodInterceptor; 10 import org.logicalcobwebs.cglib.proxy.MethodProxy; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.logicalcobwebs.proxool.proxy.InvokerFacade; 14 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.InvocationTargetException ; 17 import java.sql.Statement ; 18 import java.sql.SQLException ; 19 import java.sql.Connection ; 20 21 28 public class WrappedConnection implements MethodInterceptor { 29 30 private static final Log LOG = LogFactory.getLog(WrappedConnection.class); 31 32 private static final String CLOSE_METHOD = "close"; 33 34 private static final String IS_CLOSED_METHOD = "isClosed"; 35 36 private static final String EQUALS_METHOD = "equals"; 37 38 private static final String GET_META_DATA_METHOD = "getMetaData"; 39 40 private static final String FINALIZE_METHOD = "finalize"; 41 42 private static final String HASH_CODE_METHOD = "hashCode"; 43 44 private static final String TO_STRING_METHOD = "toString"; 45 46 51 private ProxyConnection proxyConnection; 52 53 private long id; 54 55 private String alias; 56 57 62 private boolean manuallyClosed; 63 64 68 public WrappedConnection(ProxyConnection proxyConnection) { 69 this.proxyConnection = proxyConnection; 70 this.id = proxyConnection.getId(); 71 this.alias= proxyConnection.getDefinition().getAlias(); 72 } 73 74 78 public ProxyConnection getProxyConnection() { 79 return proxyConnection; 80 } 81 82 86 public Object intercept(Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable { 87 return invoke(proxy, method, args); 88 } 89 90 106 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 107 Object result = null; 108 int argCount = args != null ? args.length : 0; 109 Method concreteMethod = method; 110 if (proxyConnection != null && proxyConnection.getConnection() != null) { 111 concreteMethod = InvokerFacade.getConcreteMethod(proxyConnection.getConnection().getClass(), method); 112 } 113 try { 114 if (proxyConnection != null && proxyConnection.isReallyClosed()) { 115 if (concreteMethod.getName().equals(IS_CLOSED_METHOD)) { 117 } else if (concreteMethod.getName().equals(CLOSE_METHOD)) { 119 } else if (manuallyClosed) { 121 throw new SQLException ("You can't perform any operations on a connection after you've called close()"); 124 } else { 125 throw new SQLException ("You can't perform any operations on this connection. It has been automatically closed by Proxool for some reason (see logs)."); 128 } 129 } 130 if (concreteMethod.getName().equals(CLOSE_METHOD)) { 131 if (proxyConnection != null && !proxyConnection.isReallyClosed()) { 134 proxyConnection.close(); 135 proxyConnection = null; 137 manuallyClosed = true; 138 } 139 } else if (concreteMethod.getName().equals(EQUALS_METHOD) && argCount == 1) { 140 result = equals(args[0]) ? Boolean.TRUE : Boolean.FALSE; 141 } else if (concreteMethod.getName().equals(HASH_CODE_METHOD) && argCount == 0) { 142 result = new Integer (hashCode()); 143 } else if (concreteMethod.getName().equals(IS_CLOSED_METHOD) && argCount == 0) { 144 result = (proxyConnection == null || proxyConnection.isClosed()) ? Boolean.TRUE : Boolean.FALSE; 145 } else if (concreteMethod.getName().equals(GET_META_DATA_METHOD) && argCount == 0) { 146 if (proxyConnection != null) { 147 Connection connection = ProxyFactory.getWrappedConnection(proxyConnection); 148 result = ProxyFactory.getDatabaseMetaData(proxyConnection.getConnection().getMetaData(), connection); 149 } else { 150 throw new SQLException ("You can't perform a " + concreteMethod.getName() + " operation after the connection has been closed"); 151 } 152 } else if (concreteMethod.getName().equals(FINALIZE_METHOD)) { 153 super.finalize(); 154 } else if (concreteMethod.getName().equals(TO_STRING_METHOD)) { 155 result = toString(); 156 } else { 157 if (proxyConnection != null) { 158 if (concreteMethod.getName().startsWith(ConnectionResetter.MUTATOR_PREFIX)) { 159 proxyConnection.setNeedToReset(true); 160 } 161 try { 162 result = concreteMethod.invoke(proxyConnection.getConnection(), args); 163 } catch (IllegalAccessException e) { 164 LOG.debug("Ignoring IllegalAccessException whilst invoking the " + concreteMethod + " concrete method and trying the " + method + " method directly."); 170 InvokerFacade.overrideConcreteMethod(proxyConnection.getConnection().getClass(), method, method); 173 result = method.invoke(proxyConnection.getConnection(), args); 174 } 175 } else { 176 throw new SQLException ("You can't perform a " + concreteMethod.getName() + " operation after the connection has been closed"); 177 } 178 } 179 180 if (result instanceof Statement ) { 183 String sqlStatement = null; 189 if (argCount > 0 && args[0] instanceof String ) { 190 sqlStatement = (String ) args[0]; 191 } 192 193 proxyConnection.addOpenStatement((Statement ) result); 195 196 result = ProxyFactory.getStatement((Statement ) result, proxyConnection.getConnectionPool(), proxyConnection, sqlStatement); 197 198 } 199 200 } catch (InvocationTargetException e) { 201 if (FatalSqlExceptionHelper.testException(proxyConnection.getDefinition(), e.getTargetException())) { 203 FatalSqlExceptionHelper.throwFatalSQLException(proxyConnection.getDefinition().getFatalSqlExceptionWrapper(), e.getTargetException()); 204 } 205 throw e.getTargetException(); 206 } catch (SQLException e) { 207 throw new SQLException ("Couldn't perform the operation " + concreteMethod.getName() + ": " + e.getMessage()); 208 } catch (Exception e) { 209 LOG.error("Unexpected invocation exception", e); 210 if (FatalSqlExceptionHelper.testException(proxyConnection.getDefinition(), e)) { 211 FatalSqlExceptionHelper.throwFatalSQLException(proxyConnection.getDefinition().getFatalSqlExceptionWrapper(), e); 212 } 213 throw new RuntimeException ("Unexpected invocation exception: " 214 + e.getMessage()); 215 } 216 return result; 217 } 218 219 224 public long getId() { 225 return id; 226 } 227 228 232 public String getAlias() { 233 return alias; 234 } 235 236 242 public boolean equals(Object obj) { 243 if (obj instanceof Connection ) { 244 final WrappedConnection wc = ProxyFactory.getWrappedConnection((Connection ) obj); 245 if (wc != null && wc.getId() > 0 && getId() > 0) { 246 return wc.getId() == getId(); 247 } else { 248 return false; 249 } 250 } else { 251 return false; 252 } 253 } 254 255 258 public String toString() { 259 if (proxyConnection != null) { 260 return hashCode() + "(" + proxyConnection.getConnection().toString() + ")"; 261 } else { 262 return hashCode() + "(out of scope)"; 263 } 264 } 265 } 266 | Popular Tags |