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.framework.autoproxy.target;
  17. import org.springframework.aop.target.AbstractPoolingTargetSource;
  18. import org.springframework.aop.target.AbstractPrototypeBasedTargetSource;
  19. import org.springframework.aop.target.CommonsPoolTargetSource;
  20. import org.springframework.beans.factory.BeanFactory;
  21. /**
  22. * Convenient superclass for TargetSource creators that create pooling TargetSources.
  23. * @author Rod Johnson
  24. * @version $Id: AbstractPoolingTargetSourceCreator.java,v 1.6 2004/04/21 11:54:40 jhoeller Exp $
  25. * @see org.springframework.aop.target.AbstractPoolingTargetSource
  26. * @see org.springframework.aop.target.CommonsPoolTargetSource
  27. */
  28. public abstract class AbstractPoolingTargetSourceCreator extends AbstractPrototypeBasedTargetSourceCreator {
  29. protected final AbstractPrototypeBasedTargetSource createPrototypeTargetSource(Object bean, String beanName,
  30. BeanFactory factory) {
  31. PoolingAttribute poolingAttribute = getPoolingAttribute(bean, beanName, factory);
  32. if (poolingAttribute == null) {
  33. // no pooling attribute
  34. return null;
  35. }
  36. else {
  37. return newPoolingTargetSource(poolingAttribute);
  38. }
  39. }
  40. /**
  41. * Create a new AbstractPoolingTargetSource. This implementation creates
  42. * a CommonsPoolTargetSource, but subclasses may wish to override that
  43. * behaviour. Don't need to set bean name or call setBeanFactory.
  44. * @see org.springframework.aop.target.CommonsPoolTargetSource
  45. */
  46. protected AbstractPoolingTargetSource newPoolingTargetSource(PoolingAttribute poolingAttribute) {
  47. AbstractPoolingTargetSource poolingTargetSource = new CommonsPoolTargetSource();
  48. poolingTargetSource.setMaxSize(poolingAttribute.getSize());
  49. return poolingTargetSource;
  50. }
  51. /**
  52. * Create a PoolingAttribute for the given bean, if any.
  53. * @param bean the bean to create a PoolingAttribute for
  54. * @param beanName the name of the bean
  55. * @param beanFactory the current bean factory
  56. * @return the PoolingAttribute, or null for no pooling
  57. */
  58. protected abstract PoolingAttribute getPoolingAttribute(Object bean, String beanName, BeanFactory beanFactory);
  59. }