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 org.springframework.aop.support.DefaultIntroductionAdvisor;
  18. import org.springframework.aop.support.DelegatingIntroductionInterceptor;
  19. import org.springframework.beans.BeansException;
  20. import org.springframework.beans.factory.BeanFactory;
  21. import org.springframework.beans.factory.BeanInitializationException;
  22. import org.springframework.beans.factory.DisposableBean;
  23. /**
  24. * Abstract superclass for pooling TargetSources that maintains a pool of
  25. * target instances, acquiring and releasing a target object from the pool
  26. * for each method invocation. This class is independent of pooling technology.
  27. *
  28. * <p>Subclasses must implement the getTarget() and releaseTarget() methods
  29. * to work with their chosen pool. The newPrototypeInstance() method inherited
  30. * from AbstractPrototypeBasedTargetSource can be used to create objects to put
  31. * in the pool. Subclasses must also implement some of the monitoring methods from
  32. * the PoolingConfig interface. This class provides the getPoolingConfigMixin()
  33. * method to return an IntroductionAdvisor making these stats available on proxied
  34. * objects.
  35. *
  36. * <p>This class implements DisposableBean to force subclasses to implement
  37. * a destroy() method to close down their pool.
  38. *
  39. * @author Rod Johnson
  40. * @version $Id: AbstractPoolingTargetSource.java,v 1.8 2004/04/20 21:53:58 jhoeller Exp $
  41. * @see #getTarget
  42. * @see #releaseTarget
  43. * @see #destroy
  44. */
  45. public abstract class AbstractPoolingTargetSource extends AbstractPrototypeBasedTargetSource
  46. implements PoolingConfig, DisposableBean {
  47. /** The size of the pool */
  48. private int maxSize;
  49. /**
  50. * Set the maximum size of the pool.
  51. */
  52. public void setMaxSize(int maxSize) {
  53. this.maxSize = maxSize;
  54. }
  55. /**
  56. * Return the maximum size of the pool.
  57. */
  58. public int getMaxSize() {
  59. return this.maxSize;
  60. }
  61. public final void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  62. super.setBeanFactory(beanFactory);
  63. try {
  64. createPool(beanFactory);
  65. }
  66. catch (BeansException ex) {
  67. throw ex;
  68. }
  69. catch (Exception ex) {
  70. throw new BeanInitializationException("Could not create instance pool", ex);
  71. }
  72. }
  73. /**
  74. * Create the pool.
  75. * @param beanFactory owning BeanFactory, in case we need collaborators from it
  76. * (normally our own properties are sufficient)
  77. * @throws Exception to avoid placing constraints on pooling APIs
  78. */
  79. protected abstract void createPool(BeanFactory beanFactory) throws Exception;
  80. /**
  81. * Acquire an object from the pool.
  82. * @return an object from the pool
  83. * @throws Exception we may need to deal with checked exceptions from pool
  84. * APIs, so we're forgiving with our exception signature
  85. */
  86. public abstract Object getTarget() throws Exception;
  87. /**
  88. * Return the given object to the pool.
  89. * @param target object that must have been acquired from the pool
  90. * via a call to getTarget()
  91. * @throws Exception to allow pooling APIs to throw exception
  92. * @see #getTarget
  93. */
  94. public abstract void releaseTarget(Object target) throws Exception;
  95. /**
  96. * Return an IntroductionAdvisor that providing a mixin
  97. * exposing statistics about the pool maintained by this object.
  98. */
  99. public DefaultIntroductionAdvisor getPoolingConfigMixin() {
  100. DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this);
  101. return new DefaultIntroductionAdvisor(dii, PoolingConfig.class);
  102. }
  103. }