1 6 package org.logicalcobwebs.proxool; 7 8 import org.logicalcobwebs.cglib.proxy.Enhancer; 9 import org.logicalcobwebs.cglib.proxy.Factory; 10 import org.logicalcobwebs.cglib.proxy.Callback; 11 import org.logicalcobwebs.cglib.core.NamingPolicy; 12 import org.logicalcobwebs.cglib.core.Predicate; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import java.sql.Connection ; 17 import java.sql.DatabaseMetaData ; 18 import java.sql.Statement ; 19 import java.sql.CallableStatement ; 20 import java.sql.PreparedStatement ; 21 import java.util.HashSet ; 22 import java.util.Set ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.lang.reflect.Modifier ; 26 27 35 class ProxyFactory { 36 37 private static final Log LOG = LogFactory.getLog(ProxyFactory.class); 38 39 private static Map interfaceMap = new HashMap (); 40 41 50 private static NamingPolicy NAMING_POLICY = new NamingPolicy() { 51 public String getClassName(String prefix, String source, Object key, Predicate names) { 52 StringBuffer sb = new StringBuffer (); 53 sb.append( 54 (prefix != null) ? 55 ( 56 prefix.startsWith("java") ? 57 "$" + prefix : prefix 58 ) 59 : "net.sf.cglib.empty.Object" 60 ); 61 sb.append("$$"); 62 sb.append(source.substring(source.lastIndexOf('.') + 1)); 63 sb.append("ByProxool$$"); 64 sb.append(Integer.toHexString(key.hashCode())); 65 String base = sb.toString(); 66 String attempt = base; 67 int index = 2; 68 while (names.evaluate(attempt)) { 69 attempt = base + "_" + index++; 70 } 71 72 return attempt; 73 } 74 }; 75 76 84 protected static Connection getWrappedConnection(ProxyConnection proxyConnection) { 85 return (Connection ) getProxy(proxyConnection.getConnection(), new WrappedConnection(proxyConnection), proxyConnection.getDefinition()); 86 } 87 88 96 protected static Statement getStatement(Statement delegate, ConnectionPool connectionPool, ProxyConnectionIF proxyConnection, String sqlStatement) { 97 return (Statement ) getProxy(delegate, new ProxyStatement(delegate, connectionPool, proxyConnection, sqlStatement), proxyConnection.getDefinition()); 98 } 99 100 106 protected static DatabaseMetaData getDatabaseMetaData(DatabaseMetaData databaseMetaData, Connection wrappedConnection) { 107 return (DatabaseMetaData ) getProxy(databaseMetaData, new ProxyDatabaseMetaData(databaseMetaData, wrappedConnection), null); 108 } 109 110 private static Object getProxy(Object delegate, Callback callback, ConnectionPoolDefinitionIF def) { 111 Enhancer e = new Enhancer(); 112 e.setNamingPolicy(NAMING_POLICY); 113 e.setInterfaces(getInterfaces(delegate.getClass(), def)); 114 e.setCallback(callback); 115 e.setClassLoader(ProxyFactory.class.getClassLoader()); 116 return e.create(); 117 } 118 119 126 protected static Statement getDelegateStatement(Statement statement) { 127 Statement ds = statement; 128 ProxyStatement ps = (ProxyStatement) ((Factory)statement).getCallback(0); 129 ds = ps.getDelegateStatement(); 130 return ds; 131 } 132 133 140 protected static Connection getDelegateConnection(Connection connection) { 141 WrappedConnection wc = (WrappedConnection) ((Factory)connection).getCallback(0); 142 return wc.getProxyConnection().getConnection(); 143 } 144 145 152 private static Class [] getInterfaces(Class clazz, ConnectionPoolDefinitionIF cpd) { 153 Class [] interfaceArray = (Class []) interfaceMap.get(clazz); 154 if (interfaceArray == null) { 155 Set interfaces = new HashSet (); 156 traverseInterfacesRecursively(interfaces, clazz); 157 if (cpd != null) { 158 if (Connection .class.isAssignableFrom(clazz)) { 162 Class injectableClass = cpd.getInjectableConnectionInterface(); 163 if (injectableClass != null) { 165 interfaces.add(injectableClass); 166 if (LOG.isDebugEnabled()) { 167 LOG.debug("Injecting " + injectableClass + " into " + clazz); 168 } 169 } 170 } 171 if (CallableStatement .class.isAssignableFrom(clazz)) { 172 if (LOG.isDebugEnabled()) { 173 LOG.debug("Getting injectableCallableStatementInterface"); 174 } 175 Class injectableClass = cpd.getInjectableCallableStatementInterface(); 176 if (injectableClass != null) { 178 interfaces.add(injectableClass); 179 if (LOG.isDebugEnabled()) { 180 LOG.debug("Injecting " + injectableClass + " into " + clazz); 181 } 182 } 183 } 184 if (PreparedStatement .class.isAssignableFrom(clazz)) { 185 Class injectableClass = cpd.getInjectablePreparedStatementInterface(); 186 if (injectableClass != null) { 188 interfaces.add(injectableClass); 189 if (LOG.isDebugEnabled()) { 190 LOG.debug("Injecting " + injectableClass + " into " + clazz); 191 } 192 } 193 } 194 if (Statement .class.isAssignableFrom(clazz)) { 195 Class injectableClass = cpd.getInjectableStatementInterface(); 196 if (injectableClass != null) { 198 interfaces.add(injectableClass); 199 if (LOG.isDebugEnabled()) { 200 LOG.debug("Injecting " + injectableClass + " into " + clazz); 201 } 202 } 203 } 204 } 205 interfaceArray = (Class []) interfaces.toArray(new Class [interfaces.size()]); 206 if (LOG.isDebugEnabled()) { 207 for (int i = 0; i < interfaceArray.length; i++) { 208 Class aClass = interfaceArray[i]; 209 LOG.debug("Implementing " + aClass); 210 } 211 } 212 interfaceMap.put(clazz, interfaceArray); 213 219 } 220 return interfaceArray; 221 } 222 223 230 private static void traverseInterfacesRecursively(Set interfaces, Class clazz) { 231 if (interfaces.contains(clazz)) { 233 239 } else { 240 245 Class [] interfaceArray = clazz.getInterfaces(); 246 for (int i = 0; i < interfaceArray.length; i++) { 247 252 traverseInterfacesRecursively(interfaces, interfaceArray[i]); 253 if (Modifier.isPublic(interfaceArray[i].getModifiers())) { 256 interfaces.add(interfaceArray[i]); 257 } 258 } 259 Class superClazz = clazz.getSuperclass(); 260 if (superClazz != null) { 261 traverseInterfacesRecursively(interfaces, superClazz); 262 } 263 268 } 269 } 270 271 276 public static WrappedConnection getWrappedConnection(Connection connection) { 277 return (WrappedConnection) ((Factory)connection).getCallback(0); 278 } 279 280 } 281 282 394 | Popular Tags |