1 16 17 package org.springframework.aop.target; 18 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 24 import org.springframework.aop.IntroductionAdvisor; 25 import org.springframework.aop.support.DefaultIntroductionAdvisor; 26 import org.springframework.aop.support.DelegatingIntroductionInterceptor; 27 import org.springframework.beans.BeansException; 28 import org.springframework.beans.factory.DisposableBean; 29 30 51 public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource 52 implements ThreadLocalTargetSourceStats, DisposableBean { 53 54 59 private final ThreadLocal targetInThread = new ThreadLocal (); 60 61 64 private final Set targetSet = Collections.synchronizedSet(new HashSet ()); 65 66 private int invocationCount; 67 68 private int hitCount; 69 70 71 76 public Object getTarget() throws BeansException { 77 ++this.invocationCount; 78 Object target = this.targetInThread.get(); 79 if (target == null) { 80 if (logger.isDebugEnabled()) { 81 logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " + 82 "creating one and binding it to thread '" + Thread.currentThread().getName() + "'"); 83 } 84 target = newPrototypeInstance(); 86 this.targetInThread.set(target); 87 this.targetSet.add(target); 88 } 89 else { 90 ++this.hitCount; 91 } 92 return target; 93 } 94 95 99 public void destroy() { 100 logger.debug("Destroying ThreadLocalTargetSource bindings"); 101 synchronized (this.targetSet) { 102 for (Iterator it = this.targetSet.iterator(); it.hasNext(); ) { 103 destroyPrototypeInstance(it.next()); 104 } 105 this.targetSet.clear(); 106 } 107 this.targetInThread.set(null); 109 } 110 111 112 public int getInvocationCount() { 113 return this.invocationCount; 114 } 115 116 public int getHitCount() { 117 return this.hitCount; 118 } 119 120 public int getObjectCount() { 121 return this.targetSet.size(); 122 } 123 124 125 129 public IntroductionAdvisor getStatsMixin() { 130 DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); 131 return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class); 132 } 133 134 } 135 | Popular Tags |