KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > AbstractPoolingTargetSource


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.aop.target;
18
19 import org.springframework.aop.support.DefaultIntroductionAdvisor;
20 import org.springframework.aop.support.DelegatingIntroductionInterceptor;
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanInitializationException;
24 import org.springframework.beans.factory.DisposableBean;
25
26 /**
27  * Abstract base class for pooling {@link org.springframework.aop.TargetSource}
28  * implementations which maintain a pool of target instances, acquiring and
29  * releasing a target object from the pool for each method invocation.
30  * This abstract base class is independent of concrete pooling technology;
31  * see the subclass {@link CommonsPoolTargetSource} for a concrete example.
32  *
33  * <p>Subclasses must implement the {@link #getTarget} and
34  * {@link #releaseTarget} methods based on their chosen object pool.
35  * The {@link #newPrototypeInstance()} method inherited from
36  * {@link AbstractPrototypeBasedTargetSource} can be used to create objects
37  * in order to put them into the pool.
38  *
39  * <p>Subclasses must also implement some of the monitoring methods from the
40  * {@link PoolingConfig} interface. The {@link #getPoolingConfigMixin()} method
41  * makes these stats available on proxied objects through an IntroductionAdvisor.
42  *
43  * <p>This class implements the {@link org.springframework.beans.factory.DisposableBean}
44  * interface in order to force subclasses to implement a {@link #destroy()}
45  * method, closing down their object pool.
46  *
47  * @author Rod Johnson
48  * @author Juergen Hoeller
49  * @see #getTarget
50  * @see #releaseTarget
51  * @see #destroy
52  */

53 public abstract class AbstractPoolingTargetSource extends AbstractPrototypeBasedTargetSource
54         implements PoolingConfig, DisposableBean {
55
56     /** The maximum size of the pool */
57     private int maxSize = -1;
58
59
60     /**
61      * Set the maximum size of the pool.
62      * Default is -1, indicating no size limit.
63      */

64     public void setMaxSize(int maxSize) {
65         this.maxSize = maxSize;
66     }
67
68     /**
69      * Return the maximum size of the pool.
70      */

71     public int getMaxSize() {
72         return this.maxSize;
73     }
74
75
76     public final void setBeanFactory(BeanFactory beanFactory) throws BeansException {
77         super.setBeanFactory(beanFactory);
78         try {
79             createPool();
80         }
81         catch (Throwable JavaDoc ex) {
82             throw new BeanInitializationException("Could not create instance pool for TargetSource", ex);
83         }
84     }
85
86
87     /**
88      * Create the pool.
89      * @throws Exception to avoid placing constraints on pooling APIs
90      */

91     protected abstract void createPool() throws Exception JavaDoc;
92     
93     /**
94      * Acquire an object from the pool.
95      * @return an object from the pool
96      * @throws Exception we may need to deal with checked exceptions from pool
97      * APIs, so we're forgiving with our exception signature
98      */

99     public abstract Object JavaDoc getTarget() throws Exception JavaDoc;
100     
101     /**
102      * Return the given object to the pool.
103      * @param target object that must have been acquired from the pool
104      * via a call to <code>getTarget()</code>
105      * @throws Exception to allow pooling APIs to throw exception
106      * @see #getTarget
107      */

108     public abstract void releaseTarget(Object JavaDoc target) throws Exception JavaDoc;
109
110
111     /**
112      * Return an IntroductionAdvisor that providing a mixin
113      * exposing statistics about the pool maintained by this object.
114      */

115     public DefaultIntroductionAdvisor getPoolingConfigMixin() {
116         DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this);
117         return new DefaultIntroductionAdvisor(dii, PoolingConfig.class);
118     }
119
120 }
121
Popular Tags