KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.pool.ObjectPool;
20 import org.apache.commons.pool.PoolableObjectFactory;
21 import org.apache.commons.pool.impl.GenericObjectPool;
22
23 import org.springframework.beans.BeansException;
24 import org.springframework.core.Constants;
25
26 /**
27  * TargetSource implementation that holds objects in a configurable
28  * Jakarta Commons Pool.
29  *
30  * <p>By default, an instance of <code>GenericObjectPool</code> is created.
31  * Subclasses may change the type of <code>ObjectPool</code> used by
32  * overriding the <code>createObjectPool()</code> method.
33  *
34  * <p>Provides many configuration properties mirroring those of the Commons Pool
35  * <code>GenericObjectPool</code> class; these properties are passed to the
36  * <code>GenericObjectPool</code> during construction. If creating a subclass of this
37  * class to change the <code>ObjectPool</code> implementation type, pass in the values
38  * of configuration properties that are relevant to your chosen implementation.
39  *
40  * <p>The <code>testOnBorrow</code>, <code>testOnReturn</code> and <code>testWhileIdle</code>
41  * properties are explictly not mirrored because the implementation of
42  * <code>PoolableObjectFactory</code> used by this class does not implement
43  * meaningful validation. All exposed Commons Pool properties use the corresponding
44  * Commons Pool defaults: for example,
45  *
46  * @author Rod Johnson
47  * @author Rob Harrop
48  * @author Juergen Hoeller
49  * @see GenericObjectPool
50  * @see #createObjectPool()
51  * @see #setMaxSize
52  * @see #setMaxIdle
53  * @see #setMinIdle
54  * @see #setMaxWait
55  * @see #setTimeBetweenEvictionRunsMillis
56  * @see #setMinEvictableIdleTimeMillis
57  */

58 public class CommonsPoolTargetSource extends AbstractPoolingTargetSource
59         implements PoolableObjectFactory {
60
61     private static final Constants constants = new Constants(GenericObjectPool.class);
62
63
64     private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
65
66     private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
67
68     private long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
69
70     private long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
71
72     private long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
73
74     private byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
75
76     /**
77      * The Jakarta Commons <code>ObjectPool</code> used to pool target objects
78      */

79     private ObjectPool pool;
80
81
82     /**
83      * Create a CommonsPoolTargetSource with default settings.
84      * Default maximum size of the pool is 8.
85      * @see #setMaxSize
86      * @see GenericObjectPool#setMaxActive
87      */

88     public CommonsPoolTargetSource() {
89         setMaxSize(GenericObjectPool.DEFAULT_MAX_ACTIVE);
90     }
91
92     /**
93      * Set the maximum number of idle objects in the pool.
94      * Default is 8.
95      * @see GenericObjectPool#setMaxIdle
96      */

97     public void setMaxIdle(int maxIdle) {
98         this.maxIdle = maxIdle;
99     }
100
101     /**
102      * Return the maximum number of idle objects in the pool.
103      */

104     public int getMaxIdle() {
105         return this.maxIdle;
106     }
107
108     /**
109      * Set the minimum number of idle objects in the pool.
110      * Default is 0.
111      * @see GenericObjectPool#setMinIdle
112      */

113     public void setMinIdle(int minIdle) {
114         this.minIdle = minIdle;
115     }
116
117     /**
118      * Return the minimum number of idle objects in the pool.
119      */

120     public int getMinIdle() {
121         return this.minIdle;
122     }
123
124     /**
125      * Set the maximum waiting time for fetching an object from the pool.
126      * Default is -1, waiting forever.
127      * @see GenericObjectPool#setMaxWait
128      */

129     public void setMaxWait(long maxWait) {
130         this.maxWait = maxWait;
131     }
132
133     /**
134      * Return the maximum waiting time for fetching an object from the pool.
135      */

136     public long getMaxWait() {
137         return this.maxWait;
138     }
139
140     /**
141      * Set the time between eviction runs that check idle objects whether
142      * they have been idle for too long or have become invalid.
143      * Default is -1, not performing any eviction.
144      * @see GenericObjectPool#setTimeBetweenEvictionRunsMillis
145      */

146     public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
147         this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
148     }
149
150     /**
151      * Return the time between eviction runs that check idle objects.
152      */

153     public long getTimeBetweenEvictionRunsMillis() {
154         return this.timeBetweenEvictionRunsMillis;
155     }
156
157     /**
158      * Set the minimum time that an idle object can sit in the pool before
159      * it becomes subject to eviction. Default is 1800000 (30 minutes).
160      * <p>Note that eviction runs need to be performed to take this
161      * setting into effect.
162      * @see #setTimeBetweenEvictionRunsMillis
163      * @see GenericObjectPool#setMinEvictableIdleTimeMillis
164      */

165     public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
166         this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
167     }
168
169     /**
170      * Return the minimum time that an idle object can sit in the pool.
171      */

172     public long getMinEvictableIdleTimeMillis() {
173         return this.minEvictableIdleTimeMillis;
174     }
175
176     /**
177      * Set the action to take when the pool is exhausted. Uses the
178      * constant names defined in Commons Pool's GenericObjectPool class:
179      * "WHEN_EXHAUSTED_BLOCK", "WHEN_EXHAUSTED_FAIL", "WHEN_EXHAUSTED_GROW".
180      * @see #setWhenExhaustedAction(byte)
181      */

182     public void setWhenExhaustedActionName(String JavaDoc whenExhaustedActionName) {
183         setWhenExhaustedAction(constants.asNumber(whenExhaustedActionName).byteValue());
184     }
185
186     /**
187      * Set the action to take when the pool is exhausted. Uses the
188      * constant values defined in Commons Pool's GenericObjectPool class.
189      * @see GenericObjectPool#setWhenExhaustedAction(byte)
190      * @see GenericObjectPool#WHEN_EXHAUSTED_BLOCK
191      * @see GenericObjectPool#WHEN_EXHAUSTED_FAIL
192      * @see GenericObjectPool#WHEN_EXHAUSTED_GROW
193      */

194     public void setWhenExhaustedAction(byte whenExhaustedAction) {
195         this.whenExhaustedAction = whenExhaustedAction;
196     }
197
198     /**
199      * Return the action to take when the pool is exhausted.
200      */

201     public byte getWhenExhaustedAction() {
202         return whenExhaustedAction;
203     }
204
205
206     /**
207      * Creates and holds an ObjectPool instance.
208      * @see #createObjectPool()
209      */

210     protected final void createPool() {
211         logger.debug("Creating Commons object pool");
212         this.pool = createObjectPool();
213     }
214
215     /**
216      * Subclasses can override this if they want to return a specific Commons pool.
217      * They should apply any configuration properties to the pool here.
218      * <p>Default is a GenericObjectPool instance with the given pool size.
219      * @return an empty Commons <code>ObjectPool</code>.
220      * @see org.apache.commons.pool.impl.GenericObjectPool
221      * @see #setMaxSize
222      */

223     protected ObjectPool createObjectPool() {
224         GenericObjectPool gop = new GenericObjectPool(this);
225         gop.setMaxActive(getMaxSize());
226         gop.setMaxIdle(getMaxIdle());
227         gop.setMinIdle(getMinIdle());
228         gop.setMaxWait(getMaxWait());
229         gop.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
230         gop.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
231         gop.setWhenExhaustedAction(getWhenExhaustedAction());
232         return gop;
233     }
234
235
236     /**
237      * Borrow an object from the <code>ObjectPool</code>.
238      */

239     public Object JavaDoc getTarget() throws Exception JavaDoc {
240         return this.pool.borrowObject();
241     }
242
243     /**
244      * Returns the specified object to the underlying <code>ObjectPool</code>.
245      */

246     public void releaseTarget(Object JavaDoc target) throws Exception JavaDoc {
247         this.pool.returnObject(target);
248     }
249
250     public int getActiveCount() throws UnsupportedOperationException JavaDoc {
251         return this.pool.getNumActive();
252     }
253
254     public int getIdleCount() throws UnsupportedOperationException JavaDoc {
255         return this.pool.getNumIdle();
256     }
257
258
259     /**
260      * Closes the underlying <code>ObjectPool</code> when destroying this object.
261      */

262     public void destroy() throws Exception JavaDoc {
263         logger.debug("Closing Commons ObjectPool");
264         this.pool.close();
265     }
266
267
268     //----------------------------------------------------------------------------
269
// Implementation of org.apache.commons.pool.PoolableObjectFactory interface
270
//----------------------------------------------------------------------------
271

272     public Object JavaDoc makeObject() throws BeansException {
273         return newPrototypeInstance();
274     }
275
276     public void destroyObject(Object JavaDoc obj) throws Exception JavaDoc {
277         destroyPrototypeInstance(obj);
278     }
279
280     public boolean validateObject(Object JavaDoc obj) {
281         return true;
282     }
283
284     public void activateObject(Object JavaDoc obj) {
285     }
286
287     public void passivateObject(Object JavaDoc obj) {
288     }
289
290 }
291
Popular Tags