1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.aop.target;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.Set;
  20. import org.springframework.aop.IntroductionAdvisor;
  21. import org.springframework.aop.support.DefaultIntroductionAdvisor;
  22. import org.springframework.aop.support.DelegatingIntroductionInterceptor;
  23. import org.springframework.beans.factory.DisposableBean;
  24. /**
  25. * Alternative to an object pool. This TargetSource uses a threading model in which
  26. * every thread has its own copy of the target. There's no contention for targets.
  27. * Target object creation is kept to a minimum on the running server.
  28. *
  29. * <p>Application code is written as to a normal pool; callers can't assume they
  30. * will be dealing with the same instance in invocations in different threads.
  31. * However, state can be relied on during the operations of a single thread:
  32. * for example, if one caller makes repeated calls on the AOP proxy.
  33. *
  34. * <p>Cleanup is performed in the destroy() method from DisposableBean.
  35. *
  36. * @author Rod Johnson
  37. * @version $Id: ThreadLocalTargetSource.java,v 1.9 2004/04/20 21:54:13 jhoeller Exp $
  38. * @see #destroy
  39. */
  40. public final class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
  41. implements ThreadLocalTargetSourceStats, DisposableBean {
  42. /**
  43. * ThreadLocal holding the target associated with the current
  44. * thread. Unlike most ThreadLocals, which are static, this variable
  45. * is meant to be per thread per instance of the ThreadLocalTargetSource class.
  46. */
  47. private ThreadLocal targetInThread = new ThreadLocal();
  48. /**
  49. * Set of managed targets, enabling us to keep track of the targets we've created.
  50. */
  51. private Set targetSet = new HashSet();
  52. private int invocations;
  53. private int hits;
  54. /**
  55. * Implementation of abstract getTarget() method.
  56. * We look for a target held in a ThreadLocal. If
  57. * we don't find one, we create one and bind it to the thread.
  58. * No synchronization is required.
  59. */
  60. public Object getTarget() {
  61. ++this.invocations;
  62. Object target = this.targetInThread.get();
  63. if (target == null) {
  64. if (logger.isInfoEnabled()) {
  65. logger.info("No target for apartment prototype '" + getTargetBeanName() +
  66. "' found in thread: creating one and binding it to thread '" +
  67. Thread.currentThread().getName() + "'");
  68. }
  69. // associate target with ThreadLocal
  70. target = newPrototypeInstance();
  71. this.targetInThread.set(target);
  72. this.targetSet.add(target);
  73. }
  74. else {
  75. ++this.hits;
  76. }
  77. return target;
  78. }
  79. public void releaseTarget(Object o) {
  80. // do nothing
  81. }
  82. /**
  83. * Dispose of targets if necessary; clear ThreadLocal.
  84. */
  85. public void destroy() {
  86. logger.info("Destroying ThreadLocal bindings");
  87. for (Iterator it = this.targetSet.iterator(); it.hasNext(); ) {
  88. Object target = it.next();
  89. if (target instanceof DisposableBean) {
  90. try {
  91. ((DisposableBean) target).destroy();
  92. }
  93. catch (Exception ex) {
  94. // do nothing
  95. logger.warn("Thread-bound target of class '" + target.getClass() +
  96. "' threw exception from destroy() method", ex);
  97. }
  98. }
  99. }
  100. this.targetSet.clear();
  101. // Clear ThreadLocal
  102. this.targetInThread = null;
  103. }
  104. public int getInvocations() {
  105. return invocations;
  106. }
  107. public int getHits() {
  108. return hits;
  109. }
  110. public int getObjects() {
  111. return targetSet.size();
  112. }
  113. /**
  114. * Return an introduction advisor mixin that allows the AOP proxy to be
  115. * case to ThreadLocalInvokerStats.
  116. */
  117. public IntroductionAdvisor getStatsMixin() {
  118. DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this);
  119. return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class);
  120. }
  121. }