AbstractPoolingTargetSource.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

  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.  * @see #getTarget
  41.  * @see #releaseTarget
  42.  * @see #destroy
  43.  */
  44. public abstract class AbstractPoolingTargetSource extends AbstractPrototypeBasedTargetSource
  45. implements PoolingConfig, DisposableBean {
  46. /** The size of the pool */
  47. private int maxSize;
  48. /**
  49.  * Set the maximum size of the pool.
  50.  */
  51. public void setMaxSize(int maxSize) {
  52. this.maxSize = maxSize;
  53. }
  54. /**
  55.  * Return the maximum size of the pool.
  56.  */
  57. public int getMaxSize() {
  58. return this.maxSize;
  59. }
  60. public final void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  61. super.setBeanFactory(beanFactory);
  62. try {
  63. createPool(beanFactory);
  64. }
  65. catch (BeansException ex) {
  66. throw ex;
  67. }
  68. catch (Exception ex) {
  69. throw new BeanInitializationException("Could not create instance pool", ex);
  70. }
  71. }
  72. /**
  73.  * Create the pool.
  74.  * @param beanFactory owning BeanFactory, in case we need collaborators from it
  75.  * (normally our own properties are sufficient)
  76.  * @throws Exception to avoid placing constraints on pooling APIs
  77.  */
  78. protected abstract void createPool(BeanFactory beanFactory) throws Exception;
  79. /**
  80.  * Acquire an object from the pool.
  81.  * @return an object from the pool
  82.  * @throws Exception we may need to deal with checked exceptions from pool
  83.  * APIs, so we're forgiving with our exception signature
  84.  */
  85. public abstract Object getTarget() throws Exception;
  86. /**
  87.  * Return the given object to the pool.
  88.  * @param target object that must have been acquired from the pool
  89.  * via a call to getTarget()
  90.  * @throws Exception to allow pooling APIs to throw exception
  91.  * @see #getTarget
  92.  */
  93. public abstract void releaseTarget(Object target) throws Exception; 
  94. /**
  95.  * Return an IntroductionAdvisor that providing a mixin
  96.  * exposing statistics about the pool maintained by this object.
  97.  */
  98. public DefaultIntroductionAdvisor getPoolingConfigMixin() {
  99. DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this);
  100. return new DefaultIntroductionAdvisor(dii, PoolingConfig.class);
  101. }
  102. }