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.KeyedObjectPool;
021    import org.apache.commons.pool.KeyedObjectPoolFactory;
022    import org.apache.commons.pool.KeyedPoolableObjectFactory;
023    
024    /**
025     * A factory for creating {@link GenericKeyedObjectPool} instances.
026     *
027     * @see GenericKeyedObjectPool
028     * @see KeyedObjectPoolFactory
029     *
030     * @author Rodney Waldhoff
031     * @author Dirk Verbeeck
032     * @version $Revision: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $
033     * @since Pool 1.0
034     */
035    public class GenericKeyedObjectPoolFactory implements KeyedObjectPoolFactory {
036        /**
037         * Create a new GenericKeyedObjectPoolFactory.
038         *
039         * @param factory the KeyedPoolableObjectFactory to used by created pools.
040         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory)
041         */
042        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory) {
043            this(factory,GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE,GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericKeyedObjectPool.DEFAULT_MAX_WAIT,GenericKeyedObjectPool.DEFAULT_MAX_IDLE,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
044        }
045    
046        /**
047         * Create a new GenericKeyedObjectPoolFactory.
048         *
049         * @param factory the KeyedPoolableObjectFactory to used by created pools.
050         * @param config a non-null GenericKeyedObjectPool.Config describing the configuration.
051         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, GenericKeyedObjectPool.Config)
052         * @throws NullPointerException when config is <code>null</code>.
053         */
054        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, GenericKeyedObjectPool.Config config) throws NullPointerException {
055            this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.maxTotal,config.minIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle,config.lifo);
056        }
057    
058        /**
059         * Create a new GenericKeyedObjectPoolFactory.
060         *
061         * @param factory the KeyedPoolableObjectFactory to used by created pools.
062         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
063         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int)
064         */
065        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive) {
066            this(factory,maxActive,GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericKeyedObjectPool.DEFAULT_MAX_WAIT,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
067        }
068    
069        /**
070         * Create a new GenericKeyedObjectPoolFactory.
071         *
072         * @param factory the KeyedPoolableObjectFactory to used by created pools.
073         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
074         * @param whenExhaustedAction the action to take when the pool is exhausted.
075         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
076         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long)
077         */
078        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) {
079            this(factory,maxActive,whenExhaustedAction,maxWait,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
080        }
081    
082        /**
083         * Create a new GenericKeyedObjectPoolFactory.
084         *
085         * @param factory the KeyedPoolableObjectFactory to used by created pools.
086         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
087         * @param whenExhaustedAction the action to take when the pool is exhausted.
088         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
089         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
090         * @param testOnReturn whether to validate objects after they are returned to returnObject.
091         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, boolean, boolean)
092         */
093        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) {
094            this(factory,maxActive,whenExhaustedAction,maxWait,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,testOnBorrow,testOnReturn,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
095        }
096    
097        /**
098         * Create a new GenericKeyedObjectPoolFactory.
099         *
100         * @param factory the KeyedPoolableObjectFactory to used by created pools.
101         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
102         * @param whenExhaustedAction the action to take when the pool is exhausted.
103         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
104         * @param maxIdle the maximum number of idle objects in the pools.
105         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int)
106         */
107        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) {
108            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
109        }
110    
111        /**
112         * Create a new GenericKeyedObjectPoolFactory.
113         *
114         * @param factory the KeyedPoolableObjectFactory to used by created pools.
115         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
116         * @param whenExhaustedAction the action to take when the pool is exhausted.
117         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
118         * @param maxIdle the maximum number of idle objects in the pools.
119         * @param maxTotal the maximum number of objects that can exists at one time.
120         */
121        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal) {
122            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, maxTotal, GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
123        }
124    
125        /**
126         * Create a new GenericKeyedObjectPoolFactory.
127         *
128         * @param factory the KeyedPoolableObjectFactory to used by created pools.
129         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
130         * @param whenExhaustedAction the action to take when the pool is exhausted.
131         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
132         * @param maxIdle the maximum number of idle objects in the pools.
133         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
134         * @param testOnReturn whether to validate objects after they are returned to returnObject.
135         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, boolean, boolean)
136         */
137        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) {
138            this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,testOnBorrow,testOnReturn,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
139        }
140    
141        /**
142         * Create a new GenericKeyedObjectPoolFactory.
143         *
144         * @param factory the KeyedPoolableObjectFactory to used by created pools.
145         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
146         * @param whenExhaustedAction the action to take when the pool is exhausted.
147         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
148         * @param maxIdle the maximum number of idle objects in the pools.
149         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
150         * @param testOnReturn whether to validate objects after they are returned to returnObject.
151         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
152         * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor.
153         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
154         * @param testWhileIdle whether to validate objects in the idle object eviction thread.
155         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, boolean, boolean, long, int, long, boolean)
156         */
157        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
158            this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle);
159        }
160    
161        /**
162         * Create a new GenericKeyedObjectPoolFactory.
163         *
164         * @param factory the KeyedPoolableObjectFactory to used by created pools.
165         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
166         * @param whenExhaustedAction the action to take when the pool is exhausted.
167         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
168         * @param maxIdle the maximum number of idle objects in the pools.
169         * @param maxTotal the maximum number of objects that can exists at one time.
170         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
171         * @param testOnReturn whether to validate objects after they are returned to returnObject.
172         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
173         * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor.
174         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
175         * @param testWhileIdle whether to validate objects in the idle object eviction thread.
176         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean)
177         */
178        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
179            this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, GenericKeyedObjectPool.DEFAULT_MIN_IDLE , testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle);
180        }
181    
182        /**
183         * Create a new GenericKeyedObjectPoolFactory.
184         *
185         * @param factory the KeyedPoolableObjectFactory to used by created pools.
186         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
187         * @param whenExhaustedAction the action to take when the pool is exhausted.
188         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
189         * @param maxIdle the maximum number of idle objects in the pools.
190         * @param maxTotal the maximum number of objects that can exists at one time.
191         * @param minIdle the minimum number of idle objects to have in the pool at any one time.
192         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
193         * @param testOnReturn whether to validate objects after they are returned to returnObject.
194         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
195         * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor.
196         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
197         * @param testWhileIdle whether to validate objects in the idle object eviction thread.
198         * @since Pool 1.3
199         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, int, boolean, boolean, long, int, long, boolean)
200         */
201        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
202            this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, GenericKeyedObjectPool.DEFAULT_LIFO);
203        }
204    
205        /**
206         * Create a new GenericKeyedObjectPoolFactory.
207         *
208         * @param factory the KeyedPoolableObjectFactory to used by created pools.
209         * @param maxActive the maximum number of objects that can be borrowed from pools at one time.
210         * @param whenExhaustedAction the action to take when the pool is exhausted.
211         * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
212         * @param maxIdle the maximum number of idle objects in the pools.
213         * @param maxTotal the maximum number of objects that can exists at one time.
214         * @param minIdle the minimum number of idle objects to have in the pool at any one time.
215         * @param testOnBorrow whether to validate objects before they are returned by borrowObject.
216         * @param testOnReturn whether to validate objects after they are returned to returnObject.
217         * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
218         * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor.
219         * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
220         * @param testWhileIdle whether to validate objects in the idle object eviction thread.
221         * @param lifo whether or not objects are returned in last-in-first-out order from the idle object pool.
222         * @since Pool 1.4
223         * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, int, boolean, boolean, long, int, long, boolean, boolean)
224         */
225        public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, boolean lifo) {
226            _maxIdle = maxIdle;
227            _maxActive = maxActive;
228            _maxTotal = maxTotal;
229            _minIdle = minIdle;
230            _maxWait = maxWait;
231            _whenExhaustedAction = whenExhaustedAction;
232            _testOnBorrow = testOnBorrow;
233            _testOnReturn = testOnReturn;
234            _testWhileIdle = testWhileIdle;
235            _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
236            _numTestsPerEvictionRun = numTestsPerEvictionRun;
237            _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
238            _factory = factory;
239            _lifo = lifo;
240        }
241    
242        public KeyedObjectPool createPool() {
243            return new GenericKeyedObjectPool(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_maxTotal,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_lifo);
244        }
245    
246        //--- protected attributes ---------------------------------------
247    
248        protected int _maxIdle = GenericKeyedObjectPool.DEFAULT_MAX_IDLE;
249        protected int _maxActive = GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE;
250        protected int _maxTotal = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL;
251        protected int _minIdle = GenericKeyedObjectPool.DEFAULT_MIN_IDLE;
252        protected long _maxWait = GenericKeyedObjectPool.DEFAULT_MAX_WAIT;
253        protected byte _whenExhaustedAction = GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
254        protected boolean _testOnBorrow = GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW;
255        protected boolean _testOnReturn = GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN;
256        protected boolean _testWhileIdle = GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE;
257        protected long _timeBetweenEvictionRunsMillis = GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
258        protected int _numTestsPerEvictionRun =  GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
259        protected long _minEvictableIdleTimeMillis = GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
260        protected KeyedPoolableObjectFactory _factory = null;
261        protected boolean _lifo = GenericKeyedObjectPool.DEFAULT_LIFO;
262    
263    }