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.apache.commons.pool.ObjectPool;
  18. import org.apache.commons.pool.PoolableObjectFactory;
  19. import org.apache.commons.pool.impl.GenericObjectPool;
  20. import org.springframework.beans.factory.BeanFactory;
  21. import org.springframework.beans.factory.DisposableBean;
  22. /**
  23. * Jakarta Commons pooling implementation extending AbstractPoolingInvokerInterceptor
  24. * @author Rod Johnson
  25. * @version $Id: CommonsPoolTargetSource.java,v 1.4 2004/03/18 02:46:13 trisberg Exp $
  26. */
  27. public class CommonsPoolTargetSource extends AbstractPoolingTargetSource
  28. implements PoolableObjectFactory {
  29. /**
  30. * Jakarta Commons object pool
  31. */
  32. private ObjectPool pool;
  33. /**
  34. * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
  35. */
  36. protected final void createPool(BeanFactory beanFactory) {
  37. logger.info("Creating Commons object pool");
  38. this.pool = createObjectPool();
  39. }
  40. /**
  41. * Subclasses can override this if they want to return a different
  42. * Commons pool to GenericObject pool.
  43. * They should apply properties to the pool here
  44. * @return an empty Commons pool
  45. */
  46. protected ObjectPool createObjectPool() {
  47. GenericObjectPool gop = new GenericObjectPool(this);
  48. gop.setMaxActive(getMaxSize());
  49. return gop;
  50. }
  51. public Object getTarget() throws Exception {
  52. return this.pool.borrowObject();
  53. }
  54. public void releaseTarget(Object target) throws Exception {
  55. this.pool.returnObject(target);
  56. }
  57. public int getActive() {
  58. return this.pool.getNumActive();
  59. }
  60. public int getFree() {
  61. return this.pool.getNumIdle();
  62. }
  63. //---------------------------------------------------------------------
  64. // Implementation of DisposableBean interface
  65. //---------------------------------------------------------------------
  66. /**
  67. * @see org.springframework.beans.factory.DisposableBean#destroy()
  68. */
  69. public void destroy() throws Exception {
  70. logger.info("Closing Commons pool");
  71. this.pool.close();
  72. }
  73. //----------------------------------------------------------------------------
  74. // Implementation of org.apache.commons.pool.PoolableObjectFactory interface
  75. //----------------------------------------------------------------------------
  76. /**
  77. * @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
  78. */
  79. public Object makeObject() {
  80. return newPrototypeInstance();
  81. }
  82. /**
  83. * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object)
  84. */
  85. public void destroyObject(Object o) throws Exception {
  86. if (o instanceof DisposableBean) {
  87. ((DisposableBean) o).destroy();
  88. }
  89. }
  90. /**
  91. * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object)
  92. */
  93. public boolean validateObject(Object o) {
  94. return true;
  95. }
  96. /**
  97. * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object)
  98. */
  99. public void activateObject(Object o) throws Exception {
  100. }
  101. /**
  102. * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object)
  103. */
  104. public void passivateObject(Object o) throws Exception {
  105. }
  106. }