1 16 package com.jdon.aop.interceptor; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.aopalliance.intercept.MethodInterceptor; 24 import org.aopalliance.intercept.MethodInvocation; 25 26 import com.jdon.aop.reflection.ProxyMethodInvocation; 27 import com.jdon.bussinessproxy.TargetMetaDef; 28 import com.jdon.bussinessproxy.target.TargetServiceFactory; 29 import com.jdon.container.ContainerWrapper; 30 import com.jdon.container.finder.ComponentKeys; 31 import com.jdon.container.finder.ContainerCallback; 32 import com.jdon.controller.cache.InstanceCache; 33 import com.jdon.controller.pool.CommonsPoolFactory; 34 import com.jdon.controller.pool.Pool; 35 import com.jdon.controller.pool.PoolConfigure; 36 import com.jdon.controller.pool.Poolable; 37 import com.jdon.util.Debug; 38 39 47 public class PoolInterceptor implements MethodInterceptor { 48 private final static String module = PoolInterceptor.class.getName(); 49 50 53 private Map poolFactorys; 54 55 private TargetServiceFactory targetServiceFactory; 56 57 private ContainerCallback containerCallback; 58 59 private PoolConfigure poolConfigure; 60 61 private List isPoolableCache = new ArrayList (); 62 63 private List unPoolableCache = new ArrayList (); 64 65 68 public PoolInterceptor(TargetServiceFactory targetServiceFactory, 69 ContainerCallback containerCallback, PoolConfigure poolConfigure) { 70 super(); 71 this.targetServiceFactory = targetServiceFactory; 72 this.containerCallback = containerCallback; 73 this.poolConfigure = poolConfigure; 74 this.poolFactorys = new HashMap (); 75 } 76 77 82 public Object invoke(MethodInvocation invocation) throws Throwable { 83 ProxyMethodInvocation proxyMethodInvocation = (ProxyMethodInvocation) invocation; 84 TargetMetaDef targetMetaDef = proxyMethodInvocation.getTargetMetaDef(); 85 if (targetMetaDef.isEJB()) 86 return invocation.proceed(); 87 88 if (!isPoolabe(targetMetaDef)) { 89 return invocation.proceed(); } 93 Debug.logVerbose("[JdonFramework] enter PoolInterceptor", module); 94 CommonsPoolFactory commonsPoolFactory = (CommonsPoolFactory)poolFactorys.get(targetMetaDef.getCacheKey()); 95 if (commonsPoolFactory == null) { 96 commonsPoolFactory = getCommonsPoolFactory(targetMetaDef); 97 poolFactorys.put(targetMetaDef.getCacheKey(), commonsPoolFactory); 98 } 99 100 Pool pool = commonsPoolFactory.getPool(); 101 Object poa = null; 102 Object result = null; 103 try { 104 poa = pool.acquirePoolable(); 105 Debug.logVerbose("[JdonFramework] borrow a object:" + targetMetaDef.getClassName() 106 + " id:" + poa.hashCode() + " from pool", module); 107 Debug.logVerbose("[JdonFramework]pool state: active=" + pool.getNumActive() + " free=" + pool.getNumIdle(), module); 108 109 proxyMethodInvocation.setThis(poa); 112 result = invocation.proceed(); 113 } catch (Exception ex) { 114 Debug.logError(ex, module); 115 } finally { 116 if (poa != null) { 117 pool.releasePoolable(poa); 118 Debug.logVerbose("[JdonFramework] realease a object:" + targetMetaDef.getClassName() 119 + " to pool", module); 120 } 121 } 122 return result; 123 } 124 125 133 private CommonsPoolFactory getCommonsPoolFactory(TargetMetaDef targetMetaDef) { 134 CommonsPoolFactory commonsPoolFactory = null; 135 try { 136 ContainerWrapper containerWrapper = containerCallback.getContainerWrapper(); 137 InstanceCache instanceCache = (InstanceCache)containerWrapper.lookup(ComponentKeys.INSTANCE_CACHE); 138 139 String key = targetMetaDef.getCacheKey() + " CommonsPoolFactory"; 140 141 commonsPoolFactory = (CommonsPoolFactory) instanceCache.get(key); 142 if (commonsPoolFactory == null) { 143 Debug.logVerbose("[JdonFramework] first time call commonsPoolFactory, create it:" 144 + key, module); 145 commonsPoolFactory = new CommonsPoolFactory( 146 targetServiceFactory, targetMetaDef, poolConfigure 147 .getMaxPoolSize()); 148 instanceCache.put(key, commonsPoolFactory); 149 } 150 151 } catch (Exception ex) { 152 Debug.logError(ex, module); 153 } 154 return commonsPoolFactory; 155 } 156 157 public boolean isPoolabe(TargetMetaDef targetMetaDef) { 158 boolean found = false; 159 if (isPoolableCache.contains(targetMetaDef.getName())) { 160 found = true; 161 }else if(!unPoolableCache.contains(targetMetaDef.getName())){ 162 Debug.logVerbose("[JdonFramework] check if it is a Poolable", module); 163 ContainerWrapper containerWrapper = containerCallback.getContainerWrapper(); 164 Class thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName()); 165 if (Poolable.class.isAssignableFrom(thisCLass)) { 166 found = true; 167 isPoolableCache.add(targetMetaDef.getName()); 168 }else{ 169 unPoolableCache.add(targetMetaDef.getName()); 170 } 171 } 172 return found; 173 } 174 175 } 176 | Popular Tags |