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 */
017package org.apache.commons.pool2.impl;
018
019/**
020 * Provides the implementation for the common attributes shared by the
021 * sub-classes. New instances of this class will be created using the defaults
022 * defined by the public constants.
023 * <p>
024 * This class is not thread-safe.
025 *
026 * @version $Revision: $
027 *
028 * @since 2.0
029 */
030public abstract class BaseObjectPoolConfig implements Cloneable {
031
032    /**
033     * The default value for the {@code lifo} configuration attribute.
034     * @see GenericObjectPool#getLifo()
035     * @see GenericKeyedObjectPool#getLifo()
036     */
037    public static final boolean DEFAULT_LIFO = true;
038
039    /**
040     * The default value for the {@code maxWait} configuration attribute.
041     * @see GenericObjectPool#getMaxWaitMillis()
042     * @see GenericKeyedObjectPool#getMaxWaitMillis()
043     */
044    public static final long DEFAULT_MAX_WAIT_MILLIS = -1L;
045
046    /**
047     * The default value for the {@code minEvictableIdleTimeMillis}
048     * configuration attribute.
049     * @see GenericObjectPool#getMinEvictableIdleTimeMillis()
050     * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()
051     */
052    public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS =
053            1000L * 60L * 30L;
054
055    /**
056     * The default value for the {@code softMinEvictableIdleTimeMillis}
057     * configuration attribute.
058     * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
059     * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis()
060     */
061    public static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1;
062
063    /**
064     * The default value for the {@code numTestsPerEvictionRun} configuration
065     * attribute.
066     * @see GenericObjectPool#getNumTestsPerEvictionRun()
067     * @see GenericKeyedObjectPool#getNumTestsPerEvictionRun()
068     */
069    public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3;
070
071    /**
072     * The default value for the {@code testOnBorrow} configuration attribute.
073     * @see GenericObjectPool#getTestOnBorrow()
074     * @see GenericKeyedObjectPool#getTestOnBorrow()
075     */
076    public static final boolean DEFAULT_TEST_ON_BORROW = false;
077
078    /**
079     * The default value for the {@code testOnReturn} configuration attribute.
080     * @see GenericObjectPool#getTestOnReturn()
081     * @see GenericKeyedObjectPool#getTestOnReturn()
082     */
083    public static final boolean DEFAULT_TEST_ON_RETURN = false;
084
085    /**
086     * The default value for the {@code testWhileIdle} configuration attribute.
087     * @see GenericObjectPool#getTestWhileIdle()
088     * @see GenericKeyedObjectPool#getTestWhileIdle()
089     */
090    public static final boolean DEFAULT_TEST_WHILE_IDLE = false;
091
092    /**
093     * The default value for the {@code timeBetweenEvictionRunsMillis}
094     * configuration attribute.
095     * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis()
096     * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis()
097     */
098    public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;
099
100    /**
101     * The default value for the {@code blockWhenExhausted} configuration
102     * attribute.
103     * @see GenericObjectPool#getBlockWhenExhausted()
104     * @see GenericKeyedObjectPool#getBlockWhenExhausted()
105     */
106    public static final boolean DEFAULT_BLOCK_WHEN_EXHAUSTED = true;
107
108    /**
109     * The default value for enabling JMX for pools created with a configuration
110     * instance.
111     */
112    public static final boolean DEFAULT_JMX_ENABLE = true;
113
114    /**
115     * The default value for the prefix used to name JMX enabled pools created
116     * with a configuration instance.
117     * @see GenericObjectPool#getJmxName()
118     * @see GenericKeyedObjectPool#getJmxName()
119     */
120    public static final String DEFAULT_JMX_NAME_PREFIX = "pool";
121
122    /**
123     * The default value for the base name to use to name JMX enabled pools
124     * created with a configuration instance. The default is <code>null</code>
125     * which means the pool will provide the base name to use.
126     * @see GenericObjectPool#getJmxName()
127     * @see GenericKeyedObjectPool#getJmxName()
128     */
129    public static final String DEFAULT_JMX_NAME_BASE = null;
130
131    /**
132     * The default value for the {@code evictionPolicyClassName} configuration
133     * attribute.
134     * @see GenericObjectPool#getEvictionPolicyClassName()
135     * @see GenericKeyedObjectPool#getEvictionPolicyClassName()
136     */
137    public static final String DEFAULT_EVICTION_POLICY_CLASS_NAME =
138            "org.apache.commons.pool2.impl.DefaultEvictionPolicy";
139
140
141    private boolean lifo = DEFAULT_LIFO;
142
143    private long maxWaitMillis = DEFAULT_MAX_WAIT_MILLIS;
144
145    private long minEvictableIdleTimeMillis =
146        DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
147
148    private long softMinEvictableIdleTimeMillis =
149            DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
150
151    private int numTestsPerEvictionRun =
152        DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
153
154    private String evictionPolicyClassName = DEFAULT_EVICTION_POLICY_CLASS_NAME;
155
156    private boolean testOnBorrow = DEFAULT_TEST_ON_BORROW;
157
158    private boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
159
160    private boolean testWhileIdle = DEFAULT_TEST_WHILE_IDLE;
161
162    private long timeBetweenEvictionRunsMillis =
163        DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
164
165    private boolean blockWhenExhausted = DEFAULT_BLOCK_WHEN_EXHAUSTED;
166
167    private boolean jmxEnabled = DEFAULT_JMX_ENABLE;
168
169    // TODO Consider changing this to a single property for 3.x
170    private String jmxNamePrefix = DEFAULT_JMX_NAME_PREFIX;
171
172    private String jmxNameBase = DEFAULT_JMX_NAME_PREFIX;
173
174
175    /**
176     * Get the value for the {@code lifo} configuration attribute for pools
177     * created with this configuration instance.
178     *
179     * @return  The current setting of {@code lifo} for this configuration
180     *          instance
181     *
182     * @see GenericObjectPool#getLifo()
183     * @see GenericKeyedObjectPool#getLifo()
184     */
185    public boolean getLifo() {
186        return lifo;
187    }
188
189    /**
190     * Set the value for the {@code lifo} configuration attribute for pools
191     * created with this configuration instance.
192     *
193     * @param lifo The new setting of {@code lifo}
194     *        for this configuration instance
195     *
196     * @see GenericObjectPool#getLifo()
197     * @see GenericKeyedObjectPool#getLifo()
198     */
199    public void setLifo(boolean lifo) {
200        this.lifo = lifo;
201    }
202
203    /**
204     * Get the value for the {@code maxWait} configuration attribute for pools
205     * created with this configuration instance.
206     *
207     * @return  The current setting of {@code maxWait} for this
208     *          configuration instance
209     *
210     * @see GenericObjectPool#getMaxWaitMillis()
211     * @see GenericKeyedObjectPool#getMaxWaitMillis()
212     */
213    public long getMaxWaitMillis() {
214        return maxWaitMillis;
215    }
216
217    /**
218     * Set the value for the {@code maxWait} configuration attribute for pools
219     * created with this configuration instance.
220     *
221     * @param maxWaitMillis The new setting of {@code maxWaitMillis}
222     *        for this configuration instance
223     *
224     * @see GenericObjectPool#getMaxWaitMillis()
225     * @see GenericKeyedObjectPool#getMaxWaitMillis()
226     */
227    public void setMaxWaitMillis(long maxWaitMillis) {
228        this.maxWaitMillis = maxWaitMillis;
229    }
230
231    /**
232     * Get the value for the {@code minEvictableIdleTimeMillis} configuration
233     * attribute for pools created with this configuration instance.
234     *
235     * @return  The current setting of {@code minEvictableIdleTimeMillis} for
236     *          this configuration instance
237     *
238     * @see GenericObjectPool#getMinEvictableIdleTimeMillis()
239     * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()
240     */
241    public long getMinEvictableIdleTimeMillis() {
242        return minEvictableIdleTimeMillis;
243    }
244
245    /**
246     * Set the value for the {@code minEvictableIdleTimeMillis} configuration
247     * attribute for pools created with this configuration instance.
248     *
249     * @param minEvictableIdleTimeMillis The new setting of
250     *        {@code minEvictableIdleTimeMillis} for this configuration instance
251     *
252     * @see GenericObjectPool#getMinEvictableIdleTimeMillis()
253     * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()
254     */
255    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
256        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
257    }
258
259    /**
260     * Get the value for the {@code softMinEvictableIdleTimeMillis}
261     * configuration attribute for pools created with this configuration
262     * instance.
263     *
264     * @return  The current setting of {@code softMinEvictableIdleTimeMillis}
265     *          for this configuration instance
266     *
267     * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
268     * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis()
269     */
270    public long getSoftMinEvictableIdleTimeMillis() {
271        return softMinEvictableIdleTimeMillis;
272    }
273
274    /**
275     * Set the value for the {@code softMinEvictableIdleTimeMillis}
276     * configuration attribute for pools created with this configuration
277     * instance.
278     *
279     * @param softMinEvictableIdleTimeMillis The new setting of
280     *        {@code softMinEvictableIdleTimeMillis} for this configuration
281     *        instance
282     *
283     * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
284     * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis()
285     */
286    public void setSoftMinEvictableIdleTimeMillis(
287            long softMinEvictableIdleTimeMillis) {
288        this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
289    }
290
291    /**
292     * Get the value for the {@code numTestsPerEvictionRun} configuration
293     * attribute for pools created with this configuration instance.
294     *
295     * @return  The current setting of {@code numTestsPerEvictionRun} for this
296     *          configuration instance
297     *
298     * @see GenericObjectPool#getNumTestsPerEvictionRun()
299     * @see GenericKeyedObjectPool#getNumTestsPerEvictionRun()
300     */
301    public int getNumTestsPerEvictionRun() {
302        return numTestsPerEvictionRun;
303    }
304
305    /**
306     * Set the value for the {@code numTestsPerEvictionRun} configuration
307     * attribute for pools created with this configuration instance.
308     *
309     * @param numTestsPerEvictionRun The new setting of
310     *        {@code numTestsPerEvictionRun} for this configuration instance
311     *
312     * @see GenericObjectPool#getNumTestsPerEvictionRun()
313     * @see GenericKeyedObjectPool#getNumTestsPerEvictionRun()
314     */
315    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
316        this.numTestsPerEvictionRun = numTestsPerEvictionRun;
317    }
318
319    /**
320     * Get the value for the {@code testOnBorrow} configuration attribute for
321     * pools created with this configuration instance.
322     *
323     * @return  The current setting of {@code testOnBorrow} for this
324     *          configuration instance
325     *
326     * @see GenericObjectPool#getTestOnBorrow()
327     * @see GenericKeyedObjectPool#getTestOnBorrow()
328     */
329    public boolean getTestOnBorrow() {
330        return testOnBorrow;
331    }
332
333    /**
334     * Set the value for the {@code testOnBorrow} configuration attribute for
335     * pools created with this configuration instance.
336     *
337     * @param testOnBorrow The new setting of {@code testOnBorrow}
338     *        for this configuration instance
339     *
340     * @see GenericObjectPool#getTestOnBorrow()
341     * @see GenericKeyedObjectPool#getTestOnBorrow()
342     */
343    public void setTestOnBorrow(boolean testOnBorrow) {
344        this.testOnBorrow = testOnBorrow;
345    }
346
347    /**
348     * Get the value for the {@code testOnReturn} configuration attribute for
349     * pools created with this configuration instance.
350     *
351     * @return  The current setting of {@code testOnReturn} for this
352     *          configuration instance
353     *
354     * @see GenericObjectPool#getTestOnReturn()
355     * @see GenericKeyedObjectPool#getTestOnReturn()
356     */
357    public boolean getTestOnReturn() {
358        return testOnReturn;
359    }
360
361    /**
362     * Set the value for the {@code testOnReturn} configuration attribute for
363     * pools created with this configuration instance.
364     *
365     * @param testOnReturn The new setting of {@code testOnReturn}
366     *        for this configuration instance
367     *
368     * @see GenericObjectPool#getTestOnReturn()
369     * @see GenericKeyedObjectPool#getTestOnReturn()
370     */
371    public void setTestOnReturn(boolean testOnReturn) {
372        this.testOnReturn = testOnReturn;
373    }
374
375    /**
376     * Get the value for the {@code testWhileIdle} configuration attribute for
377     * pools created with this configuration instance.
378     *
379     * @return  The current setting of {@code testWhileIdle} for this
380     *          configuration instance
381     *
382     * @see GenericObjectPool#getTestWhileIdle()
383     * @see GenericKeyedObjectPool#getTestWhileIdle()
384     */
385    public boolean getTestWhileIdle() {
386        return testWhileIdle;
387    }
388
389    /**
390     * Set the value for the {@code testWhileIdle} configuration attribute for
391     * pools created with this configuration instance.
392     *
393     * @param testWhileIdle The new setting of {@code testWhileIdle}
394     *        for this configuration instance
395     *
396     * @see GenericObjectPool#getTestWhileIdle()
397     * @see GenericKeyedObjectPool#getTestWhileIdle()
398     */
399    public void setTestWhileIdle(boolean testWhileIdle) {
400        this.testWhileIdle = testWhileIdle;
401    }
402
403    /**
404     * Get the value for the {@code timeBetweenEvictionRunsMillis} configuration
405     * attribute for pools created with this configuration instance.
406     *
407     * @return  The current setting of {@code timeBetweenEvictionRunsMillis} for
408     *          this configuration instance
409     *
410     * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis()
411     * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis()
412     */
413    public long getTimeBetweenEvictionRunsMillis() {
414        return timeBetweenEvictionRunsMillis;
415    }
416
417    /**
418     * Set the value for the {@code timeBetweenEvictionRunsMillis} configuration
419     * attribute for pools created with this configuration instance.
420     *
421     * @param timeBetweenEvictionRunsMillis The new setting of
422     *        {@code timeBetweenEvictionRunsMillis} for this configuration
423     *        instance
424     *
425     * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis()
426     * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis()
427     */
428    public void setTimeBetweenEvictionRunsMillis(
429            long timeBetweenEvictionRunsMillis) {
430        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
431    }
432
433    /**
434     * Get the value for the {@code evictionPolicyClassName} configuration
435     * attribute for pools created with this configuration instance.
436     *
437     * @return  The current setting of {@code evictionPolicyClassName} for this
438     *          configuration instance
439     *
440     * @see GenericObjectPool#getEvictionPolicyClassName()
441     * @see GenericKeyedObjectPool#getEvictionPolicyClassName()
442     */
443    public String getEvictionPolicyClassName() {
444        return evictionPolicyClassName;
445    }
446
447    /**
448     * Set the value for the {@code evictionPolicyClassName} configuration
449     * attribute for pools created with this configuration instance.
450     *
451     * @param evictionPolicyClassName The new setting of
452     *        {@code evictionPolicyClassName} for this configuration instance
453     *
454     * @see GenericObjectPool#getEvictionPolicyClassName()
455     * @see GenericKeyedObjectPool#getEvictionPolicyClassName()
456     */
457    public void setEvictionPolicyClassName(String evictionPolicyClassName) {
458        this.evictionPolicyClassName = evictionPolicyClassName;
459    }
460
461    /**
462     * Get the value for the {@code blockWhenExhausted} configuration attribute
463     * for pools created with this configuration instance.
464     *
465     * @return  The current setting of {@code blockWhenExhausted} for this
466     *          configuration instance
467     *
468     * @see GenericObjectPool#getBlockWhenExhausted()
469     * @see GenericKeyedObjectPool#getBlockWhenExhausted()
470     */
471    public boolean getBlockWhenExhausted() {
472        return blockWhenExhausted;
473    }
474
475    /**
476     * Set the value for the {@code blockWhenExhausted} configuration attribute
477     * for pools created with this configuration instance.
478     *
479     * @param blockWhenExhausted The new setting of {@code blockWhenExhausted}
480     *        for this configuration instance
481     *
482     * @see GenericObjectPool#getBlockWhenExhausted()
483     * @see GenericKeyedObjectPool#getBlockWhenExhausted()
484     */
485    public void setBlockWhenExhausted(boolean blockWhenExhausted) {
486        this.blockWhenExhausted = blockWhenExhausted;
487    }
488
489    /**
490     * Gets the value of the flag that determines if JMX will be enabled for
491     * pools created with this configuration instance.
492     *
493     * @return  The current setting of {@code jmxEnabled} for this configuration
494     *          instance
495     */
496    public boolean getJmxEnabled() {
497        return jmxEnabled;
498    }
499
500    /**
501     * Sets the value of the flag that determines if JMX will be enabled for
502     * pools created with this configuration instance.
503     *
504     * @param jmxEnabled The new setting of {@code jmxEnabled}
505     *        for this configuration instance
506     */
507    public void setJmxEnabled(boolean jmxEnabled) {
508        this.jmxEnabled = jmxEnabled;
509    }
510
511    /**
512     * Gets the value of the JMX name base that will be used as part of the
513     * name assigned to JMX enabled pools created with this configuration
514     * instance. A value of <code>null</code> means that the pool will define
515     * the JMX name base.
516     *
517     * @return  The current setting of {@code jmxNameBase} for this
518     *          configuration instance
519     */
520    public String getJmxNameBase() {
521        return jmxNameBase;
522    }
523
524    /**
525     * Sets the value of the JMX name base that will be used as part of the
526     * name assigned to JMX enabled pools created with this configuration
527     * instance. A value of <code>null</code> means that the pool will define
528     * the JMX name base.
529     *
530     * @param jmxNameBase The new setting of {@code jmxNameBase}
531     *        for this configuration instance
532     */
533    public void setJmxNameBase(String jmxNameBase) {
534        this.jmxNameBase = jmxNameBase;
535    }
536
537    /**
538     * Gets the value of the JMX name prefix that will be used as part of the
539     * name assigned to JMX enabled pools created with this configuration
540     * instance.
541     *
542     * @return  The current setting of {@code jmxNamePrefix} for this
543     *          configuration instance
544     */
545    public String getJmxNamePrefix() {
546        return jmxNamePrefix;
547    }
548
549    /**
550     * Sets the value of the JMX name prefix that will be used as part of the
551     * name assigned to JMX enabled pools created with this configuration
552     * instance.
553     *
554     * @param jmxNamePrefix The new setting of {@code jmxNamePrefix}
555     *        for this configuration instance
556     */
557    public void setJmxNamePrefix(String jmxNamePrefix) {
558        this.jmxNamePrefix = jmxNamePrefix;
559    }
560}