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