001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pool.impl;
019    
020    import org.apache.commons.pool.ObjectPool;
021    import org.apache.commons.pool.ObjectPoolFactory;
022    import org.apache.commons.pool.PoolableObjectFactory;
023    
024    /**
025     * A factory for creating {@link GenericObjectPool} instances.
026     *
027     * @see GenericObjectPool
028     * @see ObjectPoolFactory
029     *
030     * @author Rodney Waldhoff
031     * @version $Revision: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $
032     * @since Pool 1.0
033     */
034    public class GenericObjectPoolFactory implements ObjectPoolFactory {
035        /**
036         * Create a new GenericObjectPoolFactory.
037         *
038         * @param factory the PoolableObjectFactory used by created pools.
039         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory)
040         */
041        public GenericObjectPoolFactory(PoolableObjectFactory factory) {
042            this(factory,GenericObjectPool.DEFAULT_MAX_ACTIVE,GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericObjectPool.DEFAULT_MAX_WAIT,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
043        }
044    
045        /**
046         * Create a new GenericObjectPoolFactory.
047         *
048         * @param factory the PoolableObjectFactory used by created pools.
049         * @param config a non-<code>null</code> GenericObjectPool.Config describing the configuration.
050         * @throws NullPointerException when config is <code>null</code>.
051         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, GenericObjectPool.Config)
052         */
053        public GenericObjectPoolFactory(PoolableObjectFactory factory, GenericObjectPool.Config config) throws NullPointerException {
054            this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.minIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle,config.softMinEvictableIdleTimeMillis, config.lifo);
055        }
056    
057        /**
058         * Create a new GenericObjectPoolFactory.
059         *
060         * @param factory the PoolableObjectFactory used by created pools.
061         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
062         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int)
063         */
064        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive) {
065            this(factory,maxActive,GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericObjectPool.DEFAULT_MAX_WAIT,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
066        }
067    
068        /**
069         * Create a new GenericObjectPoolFactory.
070         *
071         * @param factory the PoolableObjectFactory used by created pools.
072         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
073         * @param whenExhaustedAction the action to take when the pool is exhausted.
074         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
075         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long)
076         */
077        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) {
078            this(factory,maxActive,whenExhaustedAction,maxWait,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
079        }
080    
081        /**
082         * Create a new GenericObjectPoolFactory.
083         *
084         * @param factory the PoolableObjectFactory used by created pools.
085         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
086         * @param whenExhaustedAction the action to take when the pool is exhausted.
087         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
088         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
089         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
090         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, boolean, boolean)
091         */
092        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) {
093            this(factory,maxActive,whenExhaustedAction,maxWait,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
094        }
095    
096        /**
097         * Create a new GenericObjectPoolFactory.
098         *
099         * @param factory the PoolableObjectFactory used by created pools.
100         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
101         * @param whenExhaustedAction the action to take when the pool is exhausted.
102         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
103         * @param maxIdle the maximum number of idle objects in my pool.
104         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int)
105         */
106        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) {
107            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
108        }
109    
110        /**
111         * Create a new GenericObjectPoolFactory.
112         *
113         * @param factory the PoolableObjectFactory used by created pools.
114         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
115         * @param whenExhaustedAction the action to take when the pool is exhausted.
116         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
117         * @param maxIdle the maximum number of idle objects in my pool.
118         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
119         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
120         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, boolean, boolean)
121         */
122        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) {
123            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
124        }
125    
126        /**
127         * Create a new GenericObjectPoolFactory.
128         *
129         * @param factory the PoolableObjectFactory used by created pools.
130         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
131         * @param whenExhaustedAction the action to take when the pool is exhausted.
132         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
133         * @param maxIdle the maximum number of idle objects in my pool.
134         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
135         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
136         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
137         * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
138         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
139         * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
140         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, boolean, boolean, long, int, long, boolean)
141         */
142        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
143            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
144        }
145    
146        /**
147         * Create a new GenericObjectPoolFactory.
148         *
149         * @param factory the PoolableObjectFactory used by created pools.
150         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
151         * @param whenExhaustedAction the action to take when the pool is exhausted.
152         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
153         * @param maxIdle the maximum number of idle objects in my pool.
154         * @param minIdle the minimum number of idle objects in my pool.
155         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
156         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
157         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
158         * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
159         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
160         * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
161         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean)
162         */
163        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
164            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
165        }
166    
167        /**
168         * Create a new GenericObjectPoolFactory.
169         *
170         * @param factory the PoolableObjectFactory used by created pools.
171         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
172         * @param whenExhaustedAction the action to take when the pool is exhausted.
173         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
174         * @param maxIdle the maximum number of idle objects in my pool.
175         * @param minIdle the minimum number of idle objects in my pool.
176         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
177         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
178         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
179         * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
180         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
181         * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
182         * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool.
183         * @since Pool 1.3
184         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long)
185         */
186        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis) {
187            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle,softMinEvictableIdleTimeMillis, GenericObjectPool.DEFAULT_LIFO);
188        }
189    
190        /**
191         * Create a new GenericObjectPoolFactory.
192         *
193         * @param factory the PoolableObjectFactory used by created pools.
194         * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
195         * @param whenExhaustedAction the action to take when the pool is exhausted.
196         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
197         * @param maxIdle the maximum number of idle objects in my pool.
198         * @param minIdle the minimum number of idle objects in my pool.
199         * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
200         * @param testOnReturn whether to validate objects after they are returned to the returnObject.
201         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
202         * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
203         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
204         * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
205         * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool.
206         * @param lifo whether or not objects are returned in last-in-first-out order from the idle object pool.
207         * @since Pool 1.4
208         * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long, boolean)
209         */
210        public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis, boolean lifo) {
211            _maxIdle = maxIdle;
212            _minIdle = minIdle;
213            _maxActive = maxActive;
214            _maxWait = maxWait;
215            _whenExhaustedAction = whenExhaustedAction;
216            _testOnBorrow = testOnBorrow;
217            _testOnReturn = testOnReturn;
218            _testWhileIdle = testWhileIdle;
219            _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
220            _numTestsPerEvictionRun = numTestsPerEvictionRun;
221            _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
222            _softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
223            _lifo = lifo;
224            _factory = factory;
225        }
226    
227        public ObjectPool createPool() {
228            return new GenericObjectPool(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_softMinEvictableIdleTimeMillis,_lifo);
229        }
230    
231        protected int _maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
232        protected int _minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
233        protected int _maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
234        protected long _maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
235        protected byte _whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
236        protected boolean _testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW;
237        protected boolean _testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN;
238        protected boolean _testWhileIdle = GenericObjectPool.DEFAULT_TEST_WHILE_IDLE;
239        protected long _timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
240        protected int _numTestsPerEvictionRun =  GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
241        protected long _minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
242        protected long _softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
243        protected boolean _lifo = GenericObjectPool.DEFAULT_LIFO;
244        protected PoolableObjectFactory _factory = null;
245    
246    
247    }