├── .asf.yaml ├── .github ├── GH-ROBOTS.txt ├── dependabot.yml ├── pull_request_template.md └── workflows │ ├── codeql-analysis.yml │ ├── dependency-review.yml │ ├── maven.yml │ └── scorecards-analysis.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── NOTICE.txt ├── README.md ├── RELEASE-NOTES.txt ├── SECURITY.md ├── doc ├── ReaderUtil.java ├── ReaderUtilClient.java └── StringBufferFactory.java ├── pom.xml ├── pool-RC.sh ├── pool-pre-RC.sh ├── pool-release.sh └── src ├── assembly ├── bin.xml └── src.xml ├── changes ├── changes.xml └── release-notes.vm ├── conf ├── checkstyle-suppressions.xml ├── checkstyle.xml ├── findbugs-exclude-filter.xml └── pmd-ruleset.xml ├── main ├── java │ └── org │ │ └── apache │ │ └── commons │ │ └── pool3 │ │ ├── BaseKeyedPooledObjectFactory.java │ │ ├── BaseObject.java │ │ ├── BaseObjectPool.java │ │ ├── BasePooledObjectFactory.java │ │ ├── DestroyMode.java │ │ ├── KeyedObjectPool.java │ │ ├── KeyedPooledObjectFactory.java │ │ ├── ObjectPool.java │ │ ├── PoolUtils.java │ │ ├── PooledObject.java │ │ ├── PooledObjectFactory.java │ │ ├── PooledObjectState.java │ │ ├── SwallowedExceptionListener.java │ │ ├── TrackedUse.java │ │ ├── UsageTracking.java │ │ ├── impl │ │ ├── AbandonedConfig.java │ │ ├── BaseGenericObjectPool.java │ │ ├── BaseObjectPoolConfig.java │ │ ├── CallStack.java │ │ ├── CallStackUtils.java │ │ ├── DefaultEvictionPolicy.java │ │ ├── DefaultPooledObject.java │ │ ├── DefaultPooledObjectInfo.java │ │ ├── DefaultPooledObjectInfoMBean.java │ │ ├── EvictionConfig.java │ │ ├── EvictionPolicy.java │ │ ├── EvictionTimer.java │ │ ├── GenericKeyedObjectPool.java │ │ ├── GenericKeyedObjectPoolConfig.java │ │ ├── GenericKeyedObjectPoolMXBean.java │ │ ├── GenericObjectPool.java │ │ ├── GenericObjectPoolConfig.java │ │ ├── GenericObjectPoolMXBean.java │ │ ├── InterruptibleReentrantLock.java │ │ ├── LinkedBlockingDeque.java │ │ ├── NoOpCallStack.java │ │ ├── PoolImplUtils.java │ │ ├── PooledSoftReference.java │ │ ├── ResilientPooledObjectFactory.java │ │ ├── SecurityManagerCallStack.java │ │ ├── SoftReferenceObjectPool.java │ │ ├── ThrowableCallStack.java │ │ └── package-info.java │ │ ├── package-info.java │ │ └── proxy │ │ ├── AbstractProxySource.java │ │ ├── BaseProxyHandler.java │ │ ├── CglibProxyHandler.java │ │ ├── CglibProxySource.java │ │ ├── JdkProxyHandler.java │ │ ├── JdkProxySource.java │ │ ├── ProxiedKeyedObjectPool.java │ │ ├── ProxiedObjectPool.java │ │ ├── ProxySource.java │ │ └── package-info.java └── javadoc │ └── overview.html ├── site ├── resources │ ├── download_pool.cgi │ ├── images │ │ └── logo.png │ └── profile.jacoco ├── site.xml └── xdoc │ ├── download_pool.xml │ ├── examples.xml │ ├── guide │ └── index.xml │ ├── index.xml │ ├── issue-tracking.xml │ ├── mail-lists.xml │ └── releases.xml └── test ├── java └── org │ └── apache │ └── commons │ └── pool3 │ ├── AbstractTestKeyedObjectPool.java │ ├── AbstractTestObjectPool.java │ ├── MethodCall.java │ ├── MethodCallPoolableObjectFactory.java │ ├── ObjectPoolIssue326.java │ ├── PoolTest.java │ ├── PrivateException.java │ ├── TestBaseKeyedPooledObjectFactory.java │ ├── TestBaseObjectPool.java │ ├── TestBasePoolableObjectFactory.java │ ├── TestException.java │ ├── TestPoolUtils.java │ ├── TestTrackedUse.java │ ├── VisitTracker.java │ ├── VisitTrackerFactory.java │ ├── Waiter.java │ ├── WaiterFactory.java │ ├── impl │ ├── AtomicIntegerFactory.java │ ├── CallStackTest.java │ ├── DisconnectingWaiterFactory.java │ ├── NoOpCallStackTest.java │ ├── TestAbandonedKeyedObjectPool.java │ ├── TestAbandonedObjectPool.java │ ├── TestBaseGenericObjectPool.java │ ├── TestCallStackUtils.java │ ├── TestConstants.java │ ├── TestDefaultPooledObject.java │ ├── TestDefaultPooledObjectInfo.java │ ├── TestEvictionConfig.java │ ├── TestEvictionTimer.java │ ├── TestGenericKeyedObjectPool.java │ ├── TestGenericObjectPool.java │ ├── TestGenericObjectPoolClassLoaders.java │ ├── TestGenericObjectPoolFactoryCreateFailure.java │ ├── TestLinkedBlockingDeque.java │ ├── TestPoolImplUtils.java │ ├── TestPooledSoftReference.java │ ├── TestResilientPooledObjectFactory.java │ ├── TestSoftRefOutOfMemory.java │ ├── TestSoftReferenceObjectPool.java │ └── TestSynchronizedPooledObjectFactory.java │ ├── performance │ ├── PerformanceTest.java │ └── SleepingObjectFactory.java │ ├── pool407 │ ├── AbstractKeyedPool407Factory.java │ ├── AbstractPool407Factory.java │ ├── AbstractPool407Test.java │ ├── KeyedPool407.java │ ├── KeyedPool407Config.java │ ├── KeyedPool407Fixture.java │ ├── KeyedPool407NormalFactory.java │ ├── KeyedPool407NullObjectFactory.java │ ├── KeyedPool407NullPoolableObjectFactory.java │ ├── KeyedPool407Test.java │ ├── Pool407.java │ ├── Pool407Config.java │ ├── Pool407Constants.java │ ├── Pool407Fixture.java │ ├── Pool407NormalFactory.java │ ├── Pool407NullObjectFactory.java │ ├── Pool407NullPoolableObjectFactory.java │ └── Pool407Test.java │ └── proxy │ ├── AbstractTestProxiedKeyedObjectPool.java │ ├── AbstractTestProxiedObjectPool.java │ ├── TestProxiedKeyedObjectPoolWithCglibProxy.java │ ├── TestProxiedKeyedObjectPoolWithJdkProxy.java │ ├── TestProxiedObjectPoolWithCglibProxy.java │ └── TestProxiedObjectPoolWithJdkProxy.java └── resources ├── test1 └── test2 /.asf.yaml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | github: 17 | description: "Apache Commons Pool" 18 | homepage: https://commons.apache.org/pool/ 19 | 20 | notifications: 21 | commits: commits@commons.apache.org 22 | issues: issues@commons.apache.org 23 | pullrequests: issues@commons.apache.org 24 | jira_options: link label 25 | jobs: notifications@commons.apache.org 26 | issues_bot_dependabot: notifications@commons.apache.org 27 | pullrequests_bot_dependabot: notifications@commons.apache.org 28 | issues_bot_codecov-commenter: notifications@commons.apache.org 29 | pullrequests_bot_codecov-commenter: notifications@commons.apache.org 30 | -------------------------------------------------------------------------------- /.github/GH-ROBOTS.txt: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | # Keeps on creating FUD PRs in test code 17 | # Does not follow Apache disclosure policies 18 | User-agent: JLLeitschuh/security-research 19 | Disallow: * 20 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | version: 2 17 | updates: 18 | - package-ecosystem: "maven" 19 | directory: "/" 20 | schedule: 21 | interval: "weekly" 22 | day: "friday" 23 | - package-ecosystem: "github-actions" 24 | directory: "/" 25 | schedule: 26 | interval: "weekly" 27 | day: "friday" 28 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 19 | 20 | Thanks for your contribution to [Apache Commons](https://commons.apache.org/)! Your help is appreciated! 21 | 22 | Before you push a pull request, review this list: 23 | 24 | - [ ] Read the [contribution guidelines](CONTRIBUTING.md) for this project. 25 | - [ ] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself. 26 | - [ ] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible but is a best-practice. 27 | - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. 28 | - [ ] Each commit in the pull request should have a meaningful subject line and body. Note that commits might be squashed by a maintainer on merge. 29 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | name: "CodeQL" 17 | 18 | on: 19 | push: 20 | branches: [ master ] 21 | pull_request: 22 | # The branches below must be a subset of the branches above 23 | branches: [ master ] 24 | schedule: 25 | - cron: '33 9 * * 4' 26 | 27 | permissions: 28 | contents: read 29 | 30 | jobs: 31 | analyze: 32 | name: Analyze 33 | runs-on: ubuntu-latest 34 | permissions: 35 | actions: read 36 | contents: read 37 | security-events: write 38 | 39 | strategy: 40 | fail-fast: false 41 | matrix: 42 | language: [ 'java' ] 43 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 44 | # Learn more about CodeQL language support at https://git.io/codeql-language-support 45 | 46 | steps: 47 | - name: Checkout repository 48 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 49 | with: 50 | persist-credentials: false 51 | - uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 52 | with: 53 | path: ~/.m2/repository 54 | key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} 55 | restore-keys: | 56 | ${{ runner.os }}-maven- 57 | - uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 58 | with: 59 | java-version: 17 60 | distribution: 'temurin' 61 | 62 | # Initializes the CodeQL tools for scanning. 63 | - name: Initialize CodeQL 64 | uses: github/codeql-action/init@ff0a06e83cb2de871e5a09832bc6a81e7276941f # 3.28.18 65 | with: 66 | languages: ${{ matrix.language }} 67 | # If you wish to specify custom queries, you can do so here or in a config file. 68 | # By default, queries listed here will override any specified in a config file. 69 | # Prefix the list here with "+" to use these queries and those in the config file. 70 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 71 | 72 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 73 | # If this step fails, then you should remove it and run the build manually (see below) 74 | - name: Autobuild 75 | uses: github/codeql-action/autobuild@ff0a06e83cb2de871e5a09832bc6a81e7276941f # 3.28.18 76 | 77 | # ℹ️ Command-line programs to run using the OS shell. 78 | # 📚 https://git.io/JvXDl 79 | 80 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 81 | # and modify them (or add more) to build your code if your project 82 | # uses a compiled language 83 | 84 | #- run: | 85 | # make bootstrap 86 | # make release 87 | 88 | - name: Perform CodeQL Analysis 89 | uses: github/codeql-action/analyze@ff0a06e83cb2de871e5a09832bc6a81e7276941f # 3.28.18 90 | -------------------------------------------------------------------------------- /.github/workflows/dependency-review.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | name: 'Dependency Review' 19 | on: [pull_request] 20 | 21 | permissions: 22 | contents: read 23 | 24 | jobs: 25 | dependency-review: 26 | runs-on: ubuntu-latest 27 | steps: 28 | - name: 'Checkout Repository' 29 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 30 | - name: 'Dependency Review PR' 31 | uses: actions/dependency-review-action@da24556b548a50705dd671f47852072ea4c105d9 # v4.7.1 32 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | name: Java CI 17 | 18 | on: 19 | push: 20 | branches: [ master ] 21 | pull_request: 22 | branches: [ master ] 23 | 24 | permissions: 25 | contents: read 26 | 27 | jobs: 28 | build: 29 | 30 | runs-on: ubuntu-latest 31 | continue-on-error: ${{ matrix.experimental }} 32 | strategy: 33 | matrix: 34 | java: [ 17, 21, 24 ] 35 | experimental: [false] 36 | include: 37 | - java: 25-ea 38 | experimental: true 39 | 40 | steps: 41 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 42 | with: 43 | persist-credentials: false 44 | - uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 45 | with: 46 | path: ~/.m2/repository 47 | key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} 48 | restore-keys: | 49 | ${{ runner.os }}-maven- 50 | - name: Set up JDK ${{ matrix.java }} 51 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 52 | with: 53 | distribution: 'temurin' 54 | java-version: ${{ matrix.java }} 55 | - name: Build with Maven 56 | run: mvn --show-version --batch-mode --no-transfer-progress 57 | -------------------------------------------------------------------------------- /.github/workflows/scorecards-analysis.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache license, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # https://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 | name: "Scorecards supply-chain security" 17 | 18 | on: 19 | branch_protection_rule: 20 | schedule: 21 | - cron: "30 1 * * 6" # Weekly on Saturdays 22 | push: 23 | branches: [ "master" ] 24 | 25 | permissions: read-all 26 | 27 | jobs: 28 | 29 | analysis: 30 | 31 | name: "Scorecards analysis" 32 | runs-on: ubuntu-latest 33 | permissions: 34 | # Needed to upload the results to the code-scanning dashboard. 35 | security-events: write 36 | actions: read 37 | id-token: write # This is required for requesting the JWT 38 | contents: read # This is required for actions/checkout 39 | 40 | steps: 41 | 42 | - name: "Checkout code" 43 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 44 | with: 45 | persist-credentials: false 46 | 47 | - name: "Run analysis" 48 | uses: ossf/scorecard-action@f49aabe0b5af0936a0987cfb85d86b75731b0186 # 2.4.1 49 | with: 50 | results_file: results.sarif 51 | results_format: sarif 52 | # A read-only PAT token, which is sufficient for the action to function. 53 | # The relevant discussion: https://github.com/ossf/scorecard-action/issues/188 54 | repo_token: ${{ secrets.GITHUB_TOKEN }} 55 | # Publish the results for public repositories to enable scorecard badges. 56 | # For more details: https://github.com/ossf/scorecard-action#publishing-results 57 | publish_results: true 58 | 59 | - name: "Upload artifact" 60 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # 4.6.2 61 | with: 62 | name: SARIF file 63 | path: results.sarif 64 | retention-days: 5 65 | 66 | - name: "Upload to code-scanning" 67 | uses: github/codeql-action/upload-sarif@ff0a06e83cb2de871e5a09832bc6a81e7276941f # 3.28.18 68 | with: 69 | sarif_file: results.sarif 70 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | site-content 3 | /.classpath 4 | /.project 5 | /.settings/ 6 | /bin/ 7 | .idea/ 8 | .vscode/ 9 | *.iml 10 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 17 | The Apache code of conduct page is [https://www.apache.org/foundation/policies/conduct.html](https://www.apache.org/foundation/policies/conduct.html). 18 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Apache Commons Pool 2 | Copyright 2001-2025 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (https://www.apache.org/). 6 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 17 | The Apache Commons security page is [https://commons.apache.org/security.html](https://commons.apache.org/security.html). 18 | -------------------------------------------------------------------------------- /doc/ReaderUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * This file is one of the source files for the examples contained in 20 | * /src/site/xdoc/examples.xml 21 | * It is not intended to be included in a source release. 22 | */ 23 | 24 | import java.io.IOException; 25 | import java.io.Reader; 26 | 27 | import org.apache.commons.pool3.ObjectPool; 28 | 29 | /** 30 | * Maintains a pool of StringBuffers used to dump contents of Readers. 31 | */ 32 | public class ReaderUtil { 33 | 34 | private ObjectPool pool; 35 | 36 | public ReaderUtil(ObjectPool pool) { 37 | this.pool = pool; 38 | } 39 | 40 | /** 41 | * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done. 42 | */ 43 | public String readToString(Reader in) 44 | throws IOException { 45 | StringBuffer buf = null; 46 | try { 47 | buf = pool.borrowObject(); 48 | for (int c = in.read(); c != -1; c = in.read()) { 49 | buf.append((char) c); 50 | } 51 | return buf.toString(); 52 | } catch (IOException e) { 53 | throw e; 54 | } catch (Exception e) { 55 | throw new RuntimeException("Unable to borrow buffer from pool" + e.toString()); 56 | } finally { 57 | try { 58 | in.close(); 59 | } catch (Exception e) { 60 | // ignored 61 | } 62 | try { 63 | if (null != buf) { 64 | pool.returnObject(buf); 65 | } 66 | } catch (Exception e) { 67 | // ignored 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /doc/ReaderUtilClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * This file is one of the source files for the examples contained in 20 | * /src/site/xdoc/examples.xml 21 | * It is not intended to be included in a source release. 22 | */ 23 | 24 | import java.io.IOException; 25 | import java.io.Reader; 26 | import java.io.StringReader; 27 | 28 | import org.apache.commons.pool3.impl.GenericObjectPool; 29 | 30 | /** 31 | * Instantiates and uses a ReaderUtil. The GenericObjectPool supplied to the constructor will have 32 | * default configuration properties. 33 | */ 34 | public class ReaderUtilClient { 35 | 36 | public static void main(String[] args) { 37 | ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool(new StringBufferFactory())); 38 | Reader reader = new StringReader("foo"); 39 | try { 40 | System.out.println(readerUtil.readToString(reader)); 41 | } catch (IOException e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /doc/StringBufferFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * This file is one of the source files for the examples contained in 20 | * /src/site/xdoc/examples.xml 21 | * It is not intended to be included in a source release. 22 | */ 23 | 24 | import org.apache.commons.pool3.BasePooledObjectFactory; 25 | import org.apache.commons.pool3.PooledObject; 26 | import org.apache.commons.pool3.impl.DefaultPooledObject; 27 | 28 | /** 29 | * Example PooledObjectFactory for pooled StringBuffers. 30 | */ 31 | public class StringBufferFactory 32 | extends BasePooledObjectFactory { 33 | 34 | @Override 35 | public StringBuffer create() { 36 | return new StringBuffer(); 37 | } 38 | 39 | /** 40 | * Use the default PooledObject implementation. 41 | */ 42 | @Override 43 | public PooledObject wrap(StringBuffer buffer) { 44 | return new DefaultPooledObject(buffer); 45 | } 46 | 47 | /** 48 | * When an object is returned to the pool, clear the buffer. 49 | */ 50 | @Override 51 | public void passivateObject(PooledObject pooledObject) { 52 | pooledObject.getObject().setLength(0); 53 | } 54 | 55 | // for all other methods, the no-op implementation 56 | // in BasePooledObjectFactory will suffice 57 | } 58 | -------------------------------------------------------------------------------- /pool-RC.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Licensed to the Apache Software Foundation (ASF) under one or more 4 | # contributor license agreements. See the NOTICE file distributed with 5 | # this work for additional information regarding copyright ownership. 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 7 | # (the "License"); you may not use this file except in compliance with 8 | # the License. You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ----------------------------------------------------------------------------- 18 | # Generates a pool RC and publishes (a superset of) maven artifacts to Nexus. 19 | # Should be run from top-level directory of a fresh checkout of the RC tag. 20 | # 21 | # Preconditions: 22 | # 0) pool-pre-RC has been run to update the download page and release notes 23 | # and these have been checked in and included in the RC tag. 24 | # 1) Release artifacts from previous runs have been svn deleted from local 25 | # svn pub/sub dev checkout. 26 | # 2) Nexus repo from previous RC has been dropped. 27 | # 28 | # Set script variables 29 | version=2.4.3 30 | repo_path=~/.m2/repository/org/apache/commons/commons-pool2/${version} 31 | release_path=~/pool-rc #checkout of https://dist.apache.org/repos/dist/dev/commons/pool 32 | # 33 | # Delete any locally installed artifacts from previous runs 34 | rm -rf ${repo_path} 35 | echo "Cleaned maven repo." 36 | # 37 | # Generate site and release artifacts, deploy locally and upload to Nexus 38 | mvn clean site 39 | mvn deploy -Prelease 40 | # 41 | # Copy the zips/tarballs and release notes to the local svn pub path 42 | cp ${repo_path}/*bin.zip* ${release_path}/binaries 43 | cp ${repo_path}/*bin.tar.gz* ${release_path}/binaries 44 | cp ${repo_path}/*src.zip* ${release_path}/source 45 | cp ${repo_path}/*src.tar.gz* ${release_path}/source 46 | cp RELEASE-NOTES.txt ${release_path} 47 | 48 | echo "Release candidate complete." 49 | echo "svn add the generated artifacts and commit after inspection." 50 | echo "log in to repository.apache.org, manually (sic) drop the cruft and close the repo." 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /pool-pre-RC.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Licensed to the Apache Software Foundation (ASF) under one or more 4 | # contributor license agreements. See the NOTICE file distributed with 5 | # this work for additional information regarding copyright ownership. 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 7 | # (the "License"); you may not use this file except in compliance with 8 | # the License. You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ----------------------------------------------------------------------------- 18 | # 19 | # Shell script to update download page and release notes prior 20 | # to preparing a commons pool release candidate. 21 | # 22 | # Note: RELEASE-NOTES.txt may need a little reformatting prior 23 | # to checkin. Both RELEASE-NOTES.txt and the generated download 24 | # page need to be checked in after review. 25 | # 26 | # ---------------------------------------------------------------------------- 27 | version=2.4.3 28 | mvn changes:announcement-generate -Prelease-notes -Dchanges.version=${version} 29 | mvn commons:download-page -Dcommons.componentid=pool -Dcommons.release.version=${version} 30 | -------------------------------------------------------------------------------- /pool-release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Licensed to the Apache Software Foundation (ASF) under one or more 4 | # contributor license agreements. See the NOTICE file distributed with 5 | # this work for additional information regarding copyright ownership. 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 7 | # (the "License"); you may not use this file except in compliance with 8 | # the License. You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ----------------------------------------------------------------------------- 18 | # Performs the local svn steps necessary to publish a pool release. 19 | # 20 | # Preconditions: 21 | # 0) Successful release VOTE has completed, based on artifacts in rc_path 22 | # (checkout of https://dist.apache.org/repos/dist/dev/commons/pool) 23 | # 1) release_path points to a local checkout of 24 | # https://dist.apache.org/repos/dist/release/commons/pool 25 | # 2) RELEASE-NOTES.txt for the new release is in top level of rc_path 26 | # 27 | # NOTE: This script does not do any of the following: 28 | # 0) Commit the local changes to actually publish the artifacts 29 | # 1) Cleanup old versions in dist 30 | # 31 | # ----------------------------------------------------------------------------- 32 | # Set script variables 33 | version=2.4.3 # version being released 34 | last_version=2.4.2 # previous version, will be replaced in README.html 35 | rc_path=~/pool-rc # checkout of https://dist.apache.org/repos/dist/dev/commons/pool 36 | release_path=~/pool-release #https://dist.apache.org/repos/dist/release/commons/pool 37 | # 38 | # Move release notes 39 | cp $rc_path/RELEASE-NOTES.txt $release_path 40 | svn rm $rc_path/RELEASE-NOTES.txt 41 | # 42 | # Update README.html 43 | sed -i "" "s/$last_version/$version/g" $release_path/README.html 44 | # OSX ^^ required suffix 45 | # 46 | cp $release_path/README.html $release_path/source 47 | cp $release_path/README.html $release_path/binaries 48 | # ^^^^^^^^^^ Maybe we can toss these? ^^^^^^^ 49 | # 50 | # Move release artifacts 51 | svn mv $rc_path/source/* $release_path/source 52 | svn mv $rc_path/binaries/* $release_path/binaries 53 | # 54 | echo "Local svn changes complete." 55 | echo "Inspect the files in $release_path and commit to publish the release." 56 | echo "Also remember to commit $rc_path to drop RC artifacts and svn rm" 57 | echo "obsolete artifacts from $release_path." 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/assembly/bin.xml: -------------------------------------------------------------------------------- 1 | 17 | 19 | bin 20 | 21 | tar.gz 22 | zip 23 | 24 | false 25 | 26 | 27 | 28 | LICENSE.txt 29 | NOTICE.txt 30 | RELEASE-NOTES.txt 31 | 32 | 33 | 34 | target 35 | 36 | 37 | *.jar 38 | 39 | 644 40 | 41 | 42 | target/site/apidocs 43 | apidocs 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/assembly/src.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | src 23 | 24 | tar.gz 25 | zip 26 | 27 | ${artifactId}-${version}-src 28 | 29 | 30 | 31 | BUILDING.txt 32 | CODE_OF_CONDUCT.md 33 | CONTRIBUTING.md 34 | LICENSE.txt 35 | NOTICE.txt 36 | pom.xml 37 | README.md 38 | RELEASE-NOTES.txt 39 | SECURITY.md 40 | 41 | 42 | 43 | src 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/conf/checkstyle-suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/conf/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /src/conf/pmd-ruleset.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 20 | 21 | This ruleset checks the code for discouraged programming constructs. 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/BaseObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * A base class for common functionality. 21 | * 22 | * @since 2.4.3 23 | */ 24 | public abstract class BaseObject { 25 | 26 | /** 27 | * Constructs a new instance. 28 | */ 29 | public BaseObject() { 30 | // empty 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | final StringBuilder builder = new StringBuilder(); 36 | builder.append(getClass().getSimpleName()); 37 | builder.append(" ["); 38 | toStringAppendFields(builder); 39 | builder.append("]"); 40 | return builder.toString(); 41 | } 42 | 43 | /** 44 | * Used by sub-classes to include the fields defined by the sub-class in the 45 | * {@link #toString()} output. 46 | * 47 | * @param builder Field names and values are appended to this object 48 | */ 49 | protected void toStringAppendFields(final StringBuilder builder) { 50 | // do nothing by default, needed for b/w compatibility. 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/BaseObjectPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * A simple base implementation of {@link ObjectPool}. 21 | * Optional operations are implemented to either do nothing, return a value 22 | * indicating it is unsupported or throw {@link UnsupportedOperationException}. 23 | *

24 | * This class is intended to be thread-safe. 25 | *

26 | * 27 | * @param Type of element pooled in this pool. 28 | * @param Type of exception thrown by this pool. 29 | * @since 2.0 30 | */ 31 | public abstract class BaseObjectPool extends BaseObject implements ObjectPool { 32 | 33 | private volatile boolean closed; 34 | 35 | /** 36 | * Constructs a new instance. 37 | */ 38 | public BaseObjectPool() { 39 | // empty 40 | } 41 | 42 | /** 43 | * Not supported in this base implementation. Subclasses should override 44 | * this behavior. 45 | * 46 | * @throws UnsupportedOperationException if the pool does not implement this 47 | * method 48 | */ 49 | @Override 50 | public void addObject() throws E, UnsupportedOperationException { 51 | throw new UnsupportedOperationException(); 52 | } 53 | 54 | /** 55 | * Throws an {@code IllegalStateException} when this pool has been 56 | * closed. 57 | * 58 | * @throws IllegalStateException when this pool has been closed. 59 | * @see #isClosed() 60 | */ 61 | protected final void assertOpen() throws IllegalStateException { 62 | if (isClosed()) { 63 | throw new IllegalStateException("Pool not open"); 64 | } 65 | } 66 | 67 | @Override 68 | public abstract T borrowObject() throws E; 69 | 70 | /** 71 | * Not supported in this base implementation. 72 | * 73 | * @throws UnsupportedOperationException if the pool does not implement this 74 | * method 75 | */ 76 | @Override 77 | public void clear() throws E, UnsupportedOperationException { 78 | throw new UnsupportedOperationException(); 79 | } 80 | 81 | /** 82 | * {@inheritDoc} 83 | *

84 | * This affects the behavior of {@code isClosed} and 85 | * {@code assertOpen}. 86 | *

87 | */ 88 | @Override 89 | public void close() { 90 | closed = true; 91 | } 92 | 93 | /** 94 | * Not supported in this base implementation. 95 | * 96 | * @return a negative value. 97 | */ 98 | @Override 99 | public int getNumActive() { 100 | return -1; 101 | } 102 | 103 | /** 104 | * Not supported in this base implementation. 105 | * 106 | * @return a negative value. 107 | */ 108 | @Override 109 | public int getNumIdle() { 110 | return -1; 111 | } 112 | 113 | @Override 114 | public abstract void invalidateObject(T obj) throws E; 115 | 116 | /** 117 | * Has this pool instance been closed. 118 | * 119 | * @return {@code true} when this pool has been closed. 120 | */ 121 | public final boolean isClosed() { 122 | return closed; 123 | } 124 | 125 | @Override 126 | public abstract void returnObject(T obj) throws E; 127 | 128 | @Override 129 | protected void toStringAppendFields(final StringBuilder builder) { 130 | builder.append("closed="); 131 | builder.append(closed); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/BasePooledObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | import java.util.Objects; 20 | 21 | /** 22 | * A base implementation of {@code PoolableObjectFactory}. 23 | *

24 | * All operations defined here are essentially no-op's. 25 | *

26 | * This class is immutable, and therefore thread-safe 27 | * 28 | * @param Type of element managed in this factory. 29 | * @param Type of exception thrown in this factory. 30 | * @see PooledObjectFactory 31 | * @see BaseKeyedPooledObjectFactory 32 | * @since 2.0 33 | */ 34 | public abstract class BasePooledObjectFactory extends BaseObject implements PooledObjectFactory { 35 | 36 | /** 37 | * Constructs a new instance. 38 | */ 39 | public BasePooledObjectFactory() { 40 | // empty 41 | } 42 | 43 | /** 44 | * No-op. 45 | * 46 | * @param p ignored 47 | */ 48 | @Override 49 | public void activateObject(final PooledObject p) throws E { 50 | // The default implementation is a no-op. 51 | } 52 | 53 | /** 54 | * Creates an object instance, to be wrapped in a {@link PooledObject}. 55 | *

This method must support concurrent, multi-threaded 56 | * invocation.

57 | * 58 | * @return an instance to be served by the pool, not null. 59 | * @throws E if there is a problem creating a new instance, 60 | * this will be propagated to the code requesting an object. 61 | */ 62 | public abstract T create() throws E; 63 | 64 | /** 65 | * No-op. 66 | * 67 | * @param p ignored 68 | */ 69 | @Override 70 | public void destroyObject(final PooledObject p) throws E { 71 | // The default implementation is a no-op. 72 | } 73 | 74 | @Override 75 | public PooledObject makeObject() throws E { 76 | return wrap(Objects.requireNonNull(create(), () -> String.format("BasePooledObjectFactory(%s).create() = null", getClass().getName()))); 77 | } 78 | 79 | /** 80 | * No-op. 81 | * 82 | * @param p ignored 83 | */ 84 | @Override 85 | public void passivateObject(final PooledObject p) throws E { 86 | // The default implementation is a no-op. 87 | } 88 | 89 | /** 90 | * Always returns {@code true}. 91 | * 92 | * @param p ignored 93 | * @return {@code true} 94 | */ 95 | @Override 96 | public boolean validateObject(final PooledObject p) { 97 | return true; 98 | } 99 | 100 | /** 101 | * Wraps the provided instance with an implementation of 102 | * {@link PooledObject}. 103 | * 104 | * @param obj the instance to wrap, should not be null. 105 | * @return The provided instance, wrapped by a {@link PooledObject} 106 | */ 107 | public abstract PooledObject wrap(T obj); 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/DestroyMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * Destroy context provided to object factories via {@code destroyObject} and {@code invalidateObject} methods. Values 21 | * provide information about why the pool is asking for a pooled object to be destroyed. 22 | * 23 | * @since 2.9.0 24 | */ 25 | public enum DestroyMode { 26 | 27 | /** Normal destroy. */ 28 | NORMAL, 29 | 30 | /** Destroy abandoned object. */ 31 | ABANDONED 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/PooledObjectState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * Provides all possible states of a {@link PooledObject}. 21 | * 22 | * @since 2.0 23 | */ 24 | public enum PooledObjectState { 25 | 26 | /** 27 | * In the queue, not in use. 28 | */ 29 | IDLE, 30 | 31 | /** 32 | * In use. 33 | */ 34 | ALLOCATED, 35 | 36 | /** 37 | * In the queue, currently being tested for possible eviction. 38 | */ 39 | EVICTION, 40 | 41 | /** 42 | * Not in the queue, currently being tested for possible eviction. An attempt to borrow the object was made while 43 | * being tested which removed it from the queue. It should be returned to the head of the queue once eviction 44 | * testing completes. 45 | *

46 | * TODO: Consider allocating object and ignoring the result of the eviction test. 47 | *

48 | */ 49 | EVICTION_RETURN_TO_HEAD, 50 | 51 | /** 52 | * In the queue, currently being validated. 53 | */ 54 | VALIDATION, 55 | 56 | /** 57 | * Not in queue, currently being validated. The object was borrowed while being validated and since testOnBorrow was 58 | * configured, it was removed from the queue and pre-allocated. It should be allocated once validation completes. 59 | */ 60 | VALIDATION_PREALLOCATED, 61 | 62 | /** 63 | * Not in queue, currently being validated. An attempt to borrow the object was made while previously being tested 64 | * for eviction which removed it from the queue. It should be returned to the head of the queue once validation 65 | * completes. 66 | */ 67 | VALIDATION_RETURN_TO_HEAD, 68 | 69 | /** 70 | * Failed maintenance (e.g. eviction test or validation) and will be / has been destroyed 71 | */ 72 | INVALID, 73 | 74 | /** 75 | * Deemed abandoned, to be invalidated. 76 | */ 77 | ABANDONED, 78 | 79 | /** 80 | * Returning to the pool. 81 | */ 82 | RETURNING 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/SwallowedExceptionListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * Pools that unavoidably swallow exceptions may be configured with an instance 21 | * of this listener so the user may receive notification of when this happens. 22 | * The listener should not throw an exception when called but pools calling 23 | * listeners should protect themselves against exceptions anyway. 24 | * 25 | * @since 2.0 26 | */ 27 | public interface SwallowedExceptionListener { 28 | 29 | /** 30 | * Notifies this instance every time the implementation unavoidably swallows 31 | * an exception. 32 | * 33 | * @param e The exception that was swallowed 34 | */ 35 | void onSwallowException(Exception e); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/TrackedUse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | import java.time.Instant; 20 | 21 | /** 22 | * Allows pooled objects to make information available about when and how they were used available to the object pool. 23 | * The object pool may, but is not required, to use this information to make more informed decisions when determining 24 | * the state of a pooled object - for instance whether or not the object has been abandoned. 25 | * 26 | * @since 2.0 27 | */ 28 | public interface TrackedUse { 29 | 30 | /** 31 | * Gets the last Instant this object was used. 32 | *

33 | * Starting with Java 9, the JRE {@code SystemClock} precision is increased usually down to microseconds, or tenth 34 | * of microseconds, depending on the OS, Hardware, and JVM implementation. 35 | *

36 | * 37 | * @return the last Instant this object was used. 38 | * @since 2.11.0 39 | */ 40 | Instant getLastUsedInstant(); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/UsageTracking.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * This interface may be implemented by an object pool to enable clients (primarily those clients that wrap pools to 21 | * provide pools with extended features) to provide additional information to the pool relating to object using allowing 22 | * more informed decisions and reporting to be made regarding abandoned objects. 23 | * 24 | * @param The type of object provided by the pool. 25 | * @since 2.0 26 | */ 27 | public interface UsageTracking { 28 | 29 | /** 30 | * Called every time a pooled object is used to enable the pool to better track borrowed objects. 31 | * 32 | * @param pooledObject The object that is being used. 33 | */ 34 | void use(T pooledObject); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/CallStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.io.PrintWriter; 20 | 21 | import org.apache.commons.pool3.PooledObject; 22 | import org.apache.commons.pool3.UsageTracking; 23 | 24 | /** 25 | * Strategy for obtaining and printing the current call stack. This is primarily useful for 26 | * {@linkplain UsageTracking usage tracking} so that different JVMs and configurations can use more efficient strategies 27 | * for obtaining the current call stack depending on metadata needs. 28 | * 29 | * @see CallStackUtils 30 | * @since 2.4.3 31 | */ 32 | public interface CallStack { 33 | 34 | /** 35 | * Clears the current stack trace snapshot. Subsequent calls to {@link #printStackTrace(PrintWriter)} will be 36 | * no-ops until another call to {@link #fillInStackTrace()}. 37 | */ 38 | void clear(); 39 | 40 | /** 41 | * Takes a snapshot of the current call stack. Subsequent calls to {@link #printStackTrace(PrintWriter)} will print 42 | * out that stack trace until it is {@linkplain #clear() cleared}. 43 | */ 44 | void fillInStackTrace(); 45 | 46 | /** 47 | * Prints the current stack trace if available to a PrintWriter. The format is undefined and is primarily useful 48 | * for debugging issues with {@link PooledObject} usage in user code. 49 | * 50 | * @param writer a PrintWriter to write the current stack trace to if available 51 | * @return true if a stack trace was available to print or false if nothing was printed 52 | */ 53 | boolean printStackTrace(PrintWriter writer); 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/CallStackUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.security.AccessControlException; 20 | 21 | /** 22 | * Utility methods for {@link CallStack}. 23 | * 24 | * @since 2.4.3 25 | */ 26 | public final class CallStackUtils { 27 | 28 | /** 29 | * Tests whether the caller can create a security manager in the current environment. 30 | * 31 | * @return {@code true} if it is able to create a security manager in the current environment, {@code false} 32 | * otherwise. 33 | */ 34 | private static boolean canCreateSecurityManager() { 35 | final SecurityManager manager = System.getSecurityManager(); 36 | if (manager == null) { 37 | return true; 38 | } 39 | try { 40 | manager.checkPermission(new RuntimePermission("createSecurityManager")); 41 | return true; 42 | } catch (final AccessControlException ignored) { 43 | return false; 44 | } 45 | } 46 | 47 | /** 48 | * Constructs a new {@link CallStack} using the fasted allowed strategy. 49 | * 50 | * @param messageFormat message (or format) to print first in stack traces 51 | * @param useTimestamp if true, interpret message as a SimpleDateFormat and print the created timestamp; 52 | * otherwise, print message format literally 53 | * @param requireFullStackTrace if true, forces the use of a stack walking mechanism that includes full stack trace 54 | * information; otherwise, uses a faster implementation if possible 55 | * @return a new CallStack 56 | * @since 2.5 57 | */ 58 | public static CallStack newCallStack(final String messageFormat, 59 | final boolean useTimestamp, 60 | final boolean requireFullStackTrace) { 61 | return canCreateSecurityManager() && !requireFullStackTrace ? 62 | new SecurityManagerCallStack(messageFormat, useTimestamp) : 63 | new ThrowableCallStack(messageFormat, useTimestamp); 64 | } 65 | 66 | /** 67 | * Hidden constructor. 68 | */ 69 | private CallStackUtils() { 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/DefaultEvictionPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import org.apache.commons.pool3.PooledObject; 20 | 21 | /** 22 | * Provides the default implementation of {@link EvictionPolicy} used by the pools. 23 | *

24 | * Objects will be evicted if the following conditions are met: 25 | *

26 | *
    27 | *
  • The object has been idle longer than 28 | * {@link GenericObjectPool#getMinEvictableIdleDuration()} / 29 | * {@link GenericKeyedObjectPool#getMinEvictableIdleDuration()}
  • 30 | *
  • There are more than {@link GenericObjectPool#getMinIdle()} / 31 | * {@link GenericKeyedObjectPoolConfig#getMinIdlePerKey()} idle objects in 32 | * the pool and the object has been idle for longer than 33 | * {@link GenericObjectPool#getSoftMinEvictableIdleDuration()} / 34 | * {@link GenericKeyedObjectPool#getSoftMinEvictableIdleDuration()} 35 | *
36 | *

37 | * This class is immutable and thread-safe. 38 | *

39 | * 40 | * @param the type of objects in the pool. 41 | * @since 2.0 42 | */ 43 | public class DefaultEvictionPolicy implements EvictionPolicy { 44 | 45 | /** 46 | * Constructs a new instance. 47 | */ 48 | public DefaultEvictionPolicy() { 49 | // empty 50 | } 51 | 52 | @Override 53 | public boolean evict(final EvictionConfig config, final PooledObject underTest, final int idleCount) { 54 | // @formatter:off 55 | return config.getIdleSoftEvictDuration().compareTo(underTest.getIdleDuration()) < 0 && 56 | config.getMinIdle() < idleCount || 57 | config.getIdleEvictDuration().compareTo(underTest.getIdleDuration()) < 0; 58 | // @formatter:on 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/DefaultPooledObjectInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.io.PrintWriter; 20 | import java.io.StringWriter; 21 | import java.text.SimpleDateFormat; 22 | import java.util.Objects; 23 | 24 | import org.apache.commons.pool3.PooledObject; 25 | 26 | /** 27 | * Implements providing information on pooled objects via JMX. 28 | * 29 | * @since 2.0 30 | */ 31 | public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { 32 | 33 | private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z"; 34 | 35 | private final PooledObject pooledObject; 36 | 37 | /** 38 | * Constructs a new instance for the given pooled object. 39 | * 40 | * @param pooledObject The pooled object that this instance will represent 41 | * @throws NullPointerException if {@code obj} is {@code null} 42 | */ 43 | public DefaultPooledObjectInfo(final PooledObject pooledObject) { 44 | this.pooledObject = Objects.requireNonNull(pooledObject, "pooledObject"); 45 | } 46 | 47 | @Override 48 | public long getBorrowedCount() { 49 | return pooledObject.getBorrowedCount(); 50 | } 51 | 52 | @Override 53 | public long getCreateTime() { 54 | return pooledObject.getCreateInstant().toEpochMilli(); 55 | } 56 | 57 | @Override 58 | public String getCreateTimeFormatted() { 59 | return getTimeMillisFormatted(getCreateTime()); 60 | } 61 | 62 | @Override 63 | public long getLastBorrowTime() { 64 | return pooledObject.getLastBorrowInstant().toEpochMilli(); 65 | } 66 | 67 | @Override 68 | public String getLastBorrowTimeFormatted() { 69 | return getTimeMillisFormatted(getLastBorrowTime()); 70 | } 71 | 72 | @Override 73 | public String getLastBorrowTrace() { 74 | final StringWriter sw = new StringWriter(); 75 | pooledObject.printStackTrace(new PrintWriter(sw)); 76 | return sw.toString(); 77 | } 78 | 79 | @Override 80 | public long getLastReturnTime() { 81 | return pooledObject.getLastReturnInstant().toEpochMilli(); 82 | } 83 | 84 | @Override 85 | public String getLastReturnTimeFormatted() { 86 | return getTimeMillisFormatted(getLastReturnTime()); 87 | } 88 | 89 | @Override 90 | public String getPooledObjectToString() { 91 | return Objects.toString(pooledObject.getObject(), null); 92 | } 93 | 94 | @Override 95 | public String getPooledObjectType() { 96 | final Object object = pooledObject.getObject(); 97 | return object != null ? object.getClass().getName() : null; 98 | } 99 | 100 | private String getTimeMillisFormatted(final long millis) { 101 | return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis)); 102 | } 103 | 104 | /** 105 | * @since 2.4.3 106 | */ 107 | @Override 108 | public String toString() { 109 | final StringBuilder builder = new StringBuilder(); 110 | builder.append("DefaultPooledObjectInfo [pooledObject="); 111 | builder.append(pooledObject); 112 | builder.append("]"); 113 | return builder.toString(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/DefaultPooledObjectInfoMBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | /** 20 | * The interface that defines the information about pooled objects that will be exposed via JMX. 21 | *

Note

22 | *

23 | * This interface exists only to define those attributes and methods that will be made available via JMX. It must not be implemented by clients as it is subject 24 | * to change between major, minor and patch version releases of commons pool. Clients that implement this interface may not, therefore, be able to upgrade to a 25 | * new minor or patch release without requiring code changes. 26 | *

27 | * 28 | * @since 2.0 29 | */ 30 | public interface DefaultPooledObjectInfoMBean { 31 | 32 | /** 33 | * Gets the number of times this object has been borrowed. 34 | * 35 | * @return The number of times this object has been borrowed. 36 | * @since 2.1 37 | */ 38 | long getBorrowedCount(); 39 | 40 | /** 41 | * Gets the time (using the same basis as {@link java.time.Clock#instant()}) that pooled object was created. 42 | * 43 | * @return The creation time for the pooled object. 44 | */ 45 | long getCreateTime(); 46 | 47 | /** 48 | * Gets the time that pooled object was created. 49 | * 50 | * @return The creation time for the pooled object formatted as {@code yyyy-MM-dd HH:mm:ss Z}. 51 | */ 52 | String getCreateTimeFormatted(); 53 | 54 | /** 55 | * Gets the time (using the same basis as {@link java.time.Clock#instant()}) the polled object was last borrowed. 56 | * 57 | * @return The time the pooled object was last borrowed. 58 | */ 59 | long getLastBorrowTime(); 60 | 61 | /** 62 | * Gets the time that pooled object was last borrowed. 63 | * 64 | * @return The last borrowed time for the pooled object formatted as {@code yyyy-MM-dd HH:mm:ss Z}. 65 | */ 66 | String getLastBorrowTimeFormatted(); 67 | 68 | /** 69 | * Gets the stack trace recorded when the pooled object was last borrowed. 70 | * 71 | * @return The stack trace showing which code last borrowed the pooled object. 72 | */ 73 | String getLastBorrowTrace(); 74 | 75 | /** 76 | * Gets the time (using the same basis as {@link java.time.Clock#instant()})the wrapped object was last returned. 77 | * 78 | * @return The time the object was last returned. 79 | */ 80 | long getLastReturnTime(); 81 | 82 | /** 83 | * Gets the time that pooled object was last returned. 84 | * 85 | * @return The last returned time for the pooled object formatted as {@code yyyy-MM-dd HH:mm:ss Z}. 86 | */ 87 | String getLastReturnTimeFormatted(); 88 | 89 | /** 90 | * Gets a String form of the wrapper for debug purposes. The format is not fixed and may change at any time. 91 | * 92 | * @return A string representation of the pooled object. 93 | * @see Object#toString() 94 | */ 95 | String getPooledObjectToString(); 96 | 97 | /** 98 | * Gets the name of the class of the pooled object. 99 | * 100 | * @return The pooled object's class name. 101 | * @see Class#getName() 102 | */ 103 | String getPooledObjectType(); 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/EvictionConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.time.Duration; 20 | 21 | /** 22 | * This class is used by pool implementations to pass configuration information 23 | * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have 24 | * its own specific configuration attributes. 25 | *

26 | * This class is immutable and thread-safe. 27 | *

28 | * 29 | * @since 2.0 30 | */ 31 | public class EvictionConfig { 32 | 33 | private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE); 34 | private final Duration idleEvictDuration; 35 | private final Duration idleSoftEvictDuration; 36 | private final int minIdle; 37 | 38 | /** 39 | * Creates a new eviction configuration with the specified parameters. 40 | * Instances are immutable. 41 | * 42 | * @param idleEvictDuration Expected to be provided by 43 | * {@link BaseGenericObjectPool#getMinEvictableIdleDuration()} 44 | * @param idleSoftEvictDuration Expected to be provided by 45 | * {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()} 46 | * @param minIdle Expected to be provided by 47 | * {@link GenericObjectPool#getMinIdle()} or 48 | * {@link GenericKeyedObjectPool#getMinIdlePerKey()} 49 | * @since 2.10.0 50 | */ 51 | public EvictionConfig(final Duration idleEvictDuration, final Duration idleSoftEvictDuration, final int minIdle) { 52 | this.idleEvictDuration = PoolImplUtils.isPositive(idleEvictDuration) ? idleEvictDuration : MAX_DURATION; 53 | this.idleSoftEvictDuration = PoolImplUtils.isPositive(idleSoftEvictDuration) ? idleSoftEvictDuration : MAX_DURATION; 54 | this.minIdle = minIdle; 55 | } 56 | 57 | /** 58 | * Gets the {@code idleEvictTime} for this eviction configuration 59 | * instance. 60 | *

61 | * How the evictor behaves based on this value will be determined by the 62 | * configured {@link EvictionPolicy}. 63 | *

64 | * 65 | * @return The {@code idleEvictTime}. 66 | * @since 2.11.0 67 | */ 68 | public Duration getIdleEvictDuration() { 69 | return idleEvictDuration; 70 | } 71 | 72 | /** 73 | * Gets the {@code idleSoftEvictTime} for this eviction configuration 74 | * instance. 75 | *

76 | * How the evictor behaves based on this value will be determined by the 77 | * configured {@link EvictionPolicy}. 78 | *

79 | * 80 | * @return The (@code idleSoftEvictTime} in milliseconds 81 | * @since 2.11.0 82 | */ 83 | public Duration getIdleSoftEvictDuration() { 84 | return idleSoftEvictDuration; 85 | } 86 | 87 | /** 88 | * Gets the {@code minIdle} for this eviction configuration instance. 89 | *

90 | * How the evictor behaves based on this value will be determined by the 91 | * configured {@link EvictionPolicy}. 92 | *

93 | * 94 | * @return The {@code minIdle} 95 | */ 96 | public int getMinIdle() { 97 | return minIdle; 98 | } 99 | 100 | /** 101 | * @since 2.4 102 | */ 103 | @Override 104 | public String toString() { 105 | final StringBuilder builder = new StringBuilder(); 106 | builder.append("EvictionConfig [idleEvictDuration="); 107 | builder.append(idleEvictDuration); 108 | builder.append(", idleSoftEvictDuration="); 109 | builder.append(idleSoftEvictDuration); 110 | builder.append(", minIdle="); 111 | builder.append(minIdle); 112 | builder.append("]"); 113 | return builder.toString(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/EvictionPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import org.apache.commons.pool3.PooledObject; 20 | 21 | /** 22 | * To provide a custom eviction policy (i.e. something other than {@link 23 | * DefaultEvictionPolicy} for a pool, users must provide an implementation of 24 | * this interface that provides the required eviction policy. 25 | * 26 | * @param the type of objects in the pool 27 | * @since 2.0 28 | */ 29 | public interface EvictionPolicy { 30 | 31 | /** 32 | * Tests if an idle object in the pool should be evicted or not. 33 | * 34 | * @param config The pool configuration settings related to eviction 35 | * @param underTest The pooled object being tested for eviction 36 | * @param idleCount The current number of idle objects in the pool including 37 | * the object under test 38 | * @return {@code true} if the object should be evicted, otherwise 39 | * {@code false} 40 | */ 41 | boolean evict(EvictionConfig config, PooledObject underTest, int idleCount); 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/InterruptibleReentrantLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.util.concurrent.locks.Condition; 20 | import java.util.concurrent.locks.ReentrantLock; 21 | 22 | /** 23 | * This sub-class was created to expose the waiting threads so that they can be 24 | * interrupted when the pool using the queue that uses this lock is closed. The 25 | * class is intended for internal use only. 26 | *

27 | * This class is intended to be thread-safe. 28 | *

29 | * 30 | * @since 2.0 31 | */ 32 | final class InterruptibleReentrantLock extends ReentrantLock { 33 | 34 | private static final long serialVersionUID = 1L; 35 | 36 | /** 37 | * Constructs a new InterruptibleReentrantLock with the given fairness policy. 38 | * 39 | * @param fairness true means threads should acquire contended locks as if 40 | * waiting in a FIFO queue 41 | */ 42 | InterruptibleReentrantLock(final boolean fairness) { 43 | super(fairness); 44 | } 45 | 46 | /** 47 | * Interrupts the threads that are waiting on a specific condition 48 | * 49 | * @param condition the condition on which the threads are waiting. 50 | */ 51 | void interruptWaiters(final Condition condition) { 52 | getWaitingThreads(condition).forEach(Thread::interrupt); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/NoOpCallStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.io.PrintWriter; 20 | 21 | /** 22 | * CallStack strategy using no-op implementations of all functionality. Can be used by default when abandoned object 23 | * logging is disabled. 24 | * 25 | * @since 2.5 26 | */ 27 | public class NoOpCallStack implements CallStack { 28 | 29 | /** 30 | * Singleton instance. 31 | */ 32 | public static final CallStack INSTANCE = new NoOpCallStack(); 33 | 34 | /** 35 | * Constructs the singleton instance. 36 | */ 37 | private NoOpCallStack() { 38 | } 39 | 40 | @Override 41 | public void clear() { 42 | // no-op 43 | } 44 | 45 | @Override 46 | public void fillInStackTrace() { 47 | // no-op 48 | } 49 | 50 | @Override 51 | public boolean printStackTrace(final PrintWriter writer) { 52 | return false; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/PooledSoftReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.lang.ref.SoftReference; 20 | 21 | /** 22 | * Extension of {@link DefaultPooledObject} to wrap pooled soft references. 23 | * 24 | *

This class is intended to be thread-safe.

25 | * 26 | * @param the type of the underlying object that the wrapped SoftReference 27 | * refers to. 28 | * 29 | * @since 2.0 30 | */ 31 | public class PooledSoftReference extends DefaultPooledObject { 32 | 33 | /** SoftReference wrapped by this object */ 34 | private volatile SoftReference reference; 35 | 36 | /** 37 | * Creates a new PooledSoftReference wrapping the provided reference. 38 | * 39 | * @param reference SoftReference to be managed by the pool 40 | */ 41 | public PooledSoftReference(final SoftReference reference) { 42 | super(null); // Null the hard reference in the parent 43 | this.reference = reference; 44 | } 45 | 46 | /** 47 | * Gets the object that the wrapped SoftReference refers to. 48 | *

49 | * Note that if the reference has been cleared, this method will return 50 | * null. 51 | *

52 | * 53 | * @return Object referred to by the SoftReference 54 | */ 55 | @Override 56 | public T getObject() { 57 | return reference.get(); 58 | } 59 | 60 | /** 61 | * Gets the SoftReference wrapped by this object. 62 | * 63 | * @return underlying SoftReference 64 | */ 65 | public synchronized SoftReference getReference() { 66 | return reference; 67 | } 68 | 69 | /** 70 | * Sets the wrapped reference. 71 | * 72 | *

This method exists to allow a new, non-registered reference to be 73 | * held by the pool to track objects that have been checked out of the pool. 74 | * The actual parameter should be a reference to the same 75 | * object that {@link #getObject()} returns before calling this method.

76 | * 77 | * @param reference new reference 78 | */ 79 | public synchronized void setReference(final SoftReference reference) { 80 | this.reference = reference; 81 | } 82 | 83 | /** 84 | * {@inheritDoc} 85 | */ 86 | @Override 87 | public String toString() { 88 | final StringBuilder result = new StringBuilder(); 89 | result.append("Referenced Object: "); 90 | result.append(getObject().toString()); 91 | result.append(", State: "); 92 | synchronized (this) { 93 | result.append(getState().toString()); 94 | } 95 | return result.toString(); 96 | // TODO add other attributes 97 | // TODO encapsulate state and other attribute display in parent 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/ThrowableCallStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.io.PrintWriter; 20 | import java.text.DateFormat; 21 | import java.text.SimpleDateFormat; 22 | 23 | /** 24 | * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the 25 | * SecurityManager implementation, provides call stack method names and other metadata in addition to the call stack 26 | * of classes. 27 | * 28 | * @see Throwable#fillInStackTrace() 29 | * @since 2.4.3 30 | */ 31 | public class ThrowableCallStack implements CallStack { 32 | 33 | /** 34 | * A snapshot of a throwable. 35 | */ 36 | private static final class Snapshot extends Throwable { 37 | private static final long serialVersionUID = 1L; 38 | private final long timestampMillis = System.currentTimeMillis(); 39 | } 40 | 41 | private final String messageFormat; 42 | 43 | //@GuardedBy("dateFormat") 44 | private final DateFormat dateFormat; 45 | 46 | private volatile Snapshot snapshot; 47 | 48 | /** 49 | * Creates a new instance. 50 | * 51 | * @param messageFormat message format 52 | * @param useTimestamp whether to format the dates in the output message or not 53 | */ 54 | public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) { 55 | this.messageFormat = messageFormat; 56 | this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null; 57 | } 58 | 59 | @Override 60 | public void clear() { 61 | snapshot = null; 62 | } 63 | 64 | @Override 65 | public void fillInStackTrace() { 66 | snapshot = new Snapshot(); 67 | } 68 | 69 | @Override 70 | public synchronized boolean printStackTrace(final PrintWriter writer) { 71 | final Snapshot snapshotRef = this.snapshot; 72 | if (snapshotRef == null) { 73 | return false; 74 | } 75 | final String message; 76 | if (dateFormat == null) { 77 | message = messageFormat; 78 | } else { 79 | synchronized (dateFormat) { 80 | message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis)); 81 | } 82 | } 83 | writer.println(message); 84 | snapshotRef.printStackTrace(writer); 85 | return true; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/impl/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Object pooling API implementations. 20 | *

21 | * {@link org.apache.commons.pool3.impl.GenericObjectPool GenericObjectPool} ({@link org.apache.commons.pool3.impl.GenericKeyedObjectPool 22 | * GenericKeyedObjectPool}) provides a more robust (but also more complicated) implementation of {@link org.apache.commons.pool3.ObjectPool ObjectPool} 23 | * ({@link org.apache.commons.pool3.KeyedObjectPool KeyedObjectPool}). 24 | *

25 | *

26 | * {@link org.apache.commons.pool3.impl.SoftReferenceObjectPool SoftReferenceObjectPool} provides a {@link java.lang.ref.SoftReference SoftReference} based 27 | * {@link org.apache.commons.pool3.ObjectPool ObjectPool}. 28 | *

29 | *

30 | * See also the {@link org.apache.commons.pool3} package. 31 | *

32 | */ 33 | package org.apache.commons.pool3.impl; 34 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Object pooling API. 20 | *

21 | * The org.apache.commons.pool3 package defines a simple interface for a pool of object instances, and a handful of base classes that may be useful 22 | * when creating pool implementations. 23 | *

24 | *

25 | * The pool package itself doesn't define a specific object pooling implementation, but rather a contract that implementations may support in order 26 | * to be fully interchangeable. 27 | *

28 | *

29 | * The pool package separates the way in which instances are pooled from the way in which they are created, resulting in a pair of interfaces: 30 | *

31 | *
32 | *
{@link org.apache.commons.pool3.ObjectPool ObjectPool}
33 | *
defines a simple object pooling interface, with methods for borrowing instances from and returning them to the pool.
34 | *
{@link org.apache.commons.pool3.PooledObjectFactory PooledObjectFactory}
35 | *
defines lifecycle methods for object instances contained within a pool. By associating a factory with a pool, the pool can create new object instances as 36 | * needed.
37 | *
38 | *

39 | * The pool package also provides a keyed pool interface, which pools instances of multiple types, accessed according to an arbitrary key. See 40 | * {@link org.apache.commons.pool3.KeyedObjectPool KeyedObjectPool} and {@link org.apache.commons.pool3.KeyedPooledObjectFactory KeyedPooledObjectFactory}. 41 | *

42 | */ 43 | package org.apache.commons.pool3; 44 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/AbstractProxySource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.proxy; 19 | 20 | import java.lang.reflect.InvocationTargetException; 21 | import java.util.function.Supplier; 22 | 23 | /** 24 | * Abstracts a proxy source implementation. 25 | * 26 | * @param type of the pooled object to be proxied. 27 | * @since 3.0.0 28 | */ 29 | public abstract class AbstractProxySource implements ProxySource { 30 | 31 | /** 32 | * Abstract a builder implementations. 33 | * 34 | * @param type of the pooled object to be proxied. 35 | * @param

the AbstractProxySource subclass 36 | * @param the builder subclass. 37 | */ 38 | public abstract static class AbstractBuilder> implements Supplier

{ 39 | 40 | /** 41 | * Whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}. 42 | */ 43 | protected boolean unwrapInvocationTargetException; 44 | 45 | /** 46 | * Constructs a new instance for a subclass. 47 | */ 48 | public AbstractBuilder() { 49 | // empty 50 | } 51 | 52 | /** 53 | * Returns {@code this} instance typed as a subclass. 54 | * 55 | * @return {@code this} instance typed as a subclass. 56 | */ 57 | @SuppressWarnings("unchecked") 58 | B asThis() { 59 | return (B) this; 60 | } 61 | 62 | /** 63 | * Sets whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}. 64 | * 65 | * @param unwrapInvocationTargetException whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of 66 | * {@link InvocationTargetException}. 67 | * @return {@code this} instance. 68 | */ 69 | public B setUnwrapInvocationTargetException(final boolean unwrapInvocationTargetException) { 70 | this.unwrapInvocationTargetException = unwrapInvocationTargetException; 71 | return asThis(); 72 | } 73 | } 74 | 75 | /** 76 | * Whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}. 77 | */ 78 | private final boolean unwrapInvocationTargetException; 79 | 80 | /** 81 | * Constructs a new instance. 82 | * 83 | * @param builder Information used to build the new instance. 84 | */ 85 | protected AbstractProxySource(final AbstractBuilder builder) { 86 | this.unwrapInvocationTargetException = builder.unwrapInvocationTargetException; 87 | } 88 | 89 | /** 90 | * Tests whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}. 91 | * 92 | * @return whether the proxy throws {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}. 93 | */ 94 | protected boolean isUnwrapInvocationTargetException() { 95 | return unwrapInvocationTargetException; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/CglibProxyHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.proxy; 19 | 20 | import java.lang.reflect.InvocationTargetException; 21 | import java.lang.reflect.Method; 22 | 23 | import org.apache.commons.pool3.UsageTracking; 24 | 25 | import net.sf.cglib.proxy.MethodInterceptor; 26 | import net.sf.cglib.proxy.MethodProxy; 27 | 28 | /** 29 | * cglib is unmaintained and does not work well (or possibly at all?) in newer JDKs, particularly JDK17+; see https://github.com/cglib/cglib 30 | *

31 | * CGLib implementation of the proxy handler. 32 | *

33 | * 34 | * @param type of the wrapped pooled object 35 | * @since 2.0 36 | */ 37 | final class CglibProxyHandler extends BaseProxyHandler implements MethodInterceptor { 38 | 39 | /** 40 | * Constructs a CGLib proxy instance. 41 | * 42 | * @param pooledObject The object to wrap 43 | * @param usageTracking The instance, if any (usually the object pool) to be provided with usage tracking information for this wrapped 44 | * object 45 | * @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of 46 | * {@link InvocationTargetException} 47 | */ 48 | CglibProxyHandler(final T pooledObject, final UsageTracking usageTracking, final boolean unwrapInvocationTargetException) { 49 | super(pooledObject, usageTracking, unwrapInvocationTargetException); 50 | } 51 | 52 | @Override 53 | public Object intercept(final Object object, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable { 54 | return doInvoke(method, args); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/JdkProxyHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.proxy; 19 | 20 | import java.lang.reflect.InvocationHandler; 21 | import java.lang.reflect.InvocationTargetException; 22 | import java.lang.reflect.Method; 23 | 24 | import org.apache.commons.pool3.UsageTracking; 25 | 26 | /** 27 | * Java reflection implementation of the proxy handler. 28 | * 29 | * @param type of the wrapped pooled object 30 | * @since 2.0 31 | */ 32 | final class JdkProxyHandler extends BaseProxyHandler implements InvocationHandler { 33 | 34 | /** 35 | * Constructs a Java reflection proxy instance. 36 | * 37 | * @param pooledObject The object to wrap 38 | * @param usageTracking The instance, if any (usually the object pool) to be provided with usage tracking information for this wrapped 39 | * object 40 | * @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of 41 | * {@link InvocationTargetException} 42 | */ 43 | JdkProxyHandler(final T pooledObject, final UsageTracking usageTracking, final boolean unwrapInvocationTargetException) { 44 | super(pooledObject, usageTracking, unwrapInvocationTargetException); 45 | } 46 | 47 | @Override 48 | public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { 49 | return doInvoke(method, args); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/ProxiedKeyedObjectPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.proxy; 18 | 19 | import java.util.List; 20 | import java.util.NoSuchElementException; 21 | 22 | import org.apache.commons.pool3.KeyedObjectPool; 23 | import org.apache.commons.pool3.UsageTracking; 24 | 25 | /** 26 | * Create a new keyed object pool where the pooled objects are wrapped in 27 | * proxies allowing better control of pooled objects and in particular the 28 | * prevention of the continued use of an object by a client after that client 29 | * returns the object to the pool. 30 | * 31 | * @param type of the key 32 | * @param type of the pooled object 33 | * @param type of exception thrown by this pool 34 | * @since 2.0 35 | */ 36 | public class ProxiedKeyedObjectPool implements KeyedObjectPool { 37 | 38 | private final KeyedObjectPool pool; 39 | private final ProxySource proxySource; 40 | 41 | /** 42 | * Constructs a new proxied object pool. 43 | * 44 | * @param pool The object pool to wrap 45 | * @param proxySource The source of the proxy objects 46 | */ 47 | public ProxiedKeyedObjectPool(final KeyedObjectPool pool, 48 | final ProxySource proxySource) { 49 | this.pool = pool; 50 | this.proxySource = proxySource; 51 | } 52 | 53 | @Override 54 | public void addObject(final K key) throws E, IllegalStateException, 55 | UnsupportedOperationException { 56 | pool.addObject(key); 57 | } 58 | 59 | @SuppressWarnings("unchecked") 60 | @Override 61 | public V borrowObject(final K key) throws E, NoSuchElementException, 62 | IllegalStateException { 63 | UsageTracking usageTracking = null; 64 | if (pool instanceof UsageTracking) { 65 | usageTracking = (UsageTracking) pool; 66 | } 67 | return proxySource.createProxy(pool.borrowObject(key), usageTracking); 68 | } 69 | 70 | @Override 71 | public void clear() throws E, UnsupportedOperationException { 72 | pool.clear(); 73 | } 74 | 75 | @Override 76 | public void clear(final K key) throws E, UnsupportedOperationException { 77 | pool.clear(key); 78 | } 79 | 80 | @Override 81 | public void close() { 82 | pool.close(); 83 | } 84 | 85 | @Override 86 | public List getKeys() { 87 | return pool.getKeys(); 88 | } 89 | 90 | @Override 91 | public int getNumActive() { 92 | return pool.getNumActive(); 93 | } 94 | 95 | @Override 96 | public int getNumActive(final K key) { 97 | return pool.getNumActive(key); 98 | } 99 | 100 | @Override 101 | public int getNumIdle() { 102 | return pool.getNumIdle(); 103 | } 104 | 105 | @Override 106 | public int getNumIdle(final K key) { 107 | return pool.getNumIdle(key); 108 | } 109 | 110 | @Override 111 | public void invalidateObject(final K key, final V proxy) throws E { 112 | pool.invalidateObject(key, proxySource.resolveProxy(proxy)); 113 | } 114 | 115 | @Override 116 | public void returnObject(final K key, final V proxy) throws E { 117 | pool.returnObject(key, proxySource.resolveProxy(proxy)); 118 | } 119 | 120 | /** 121 | * @since 2.4.3 122 | */ 123 | @Override 124 | public String toString() { 125 | final StringBuilder builder = new StringBuilder(); 126 | builder.append("ProxiedKeyedObjectPool [pool="); 127 | builder.append(pool); 128 | builder.append(", proxySource="); 129 | builder.append(proxySource); 130 | builder.append("]"); 131 | return builder.toString(); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/ProxiedObjectPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.proxy; 18 | 19 | import java.util.NoSuchElementException; 20 | 21 | import org.apache.commons.pool3.ObjectPool; 22 | import org.apache.commons.pool3.UsageTracking; 23 | 24 | /** 25 | * Create a new object pool where the pooled objects are wrapped in proxies 26 | * allowing better control of pooled objects and in particular the prevention 27 | * of the continued use of an object by a client after that client returns the 28 | * object to the pool. 29 | * 30 | * @param type of the pooled object 31 | * @param type of the exception 32 | * @since 2.0 33 | */ 34 | public class ProxiedObjectPool implements ObjectPool { 35 | 36 | private final ObjectPool pool; 37 | private final ProxySource proxySource; 38 | 39 | /** 40 | * Constructs a new proxied object pool. 41 | * 42 | * @param pool The object pool to wrap 43 | * @param proxySource The source of the proxy objects 44 | */ 45 | public ProxiedObjectPool(final ObjectPool pool, final ProxySource proxySource) { 46 | this.pool = pool; 47 | this.proxySource = proxySource; 48 | } 49 | 50 | @Override 51 | public void addObject() throws E, IllegalStateException, UnsupportedOperationException { 52 | pool.addObject(); 53 | } 54 | 55 | @SuppressWarnings("unchecked") 56 | @Override 57 | public T borrowObject() throws E, NoSuchElementException, IllegalStateException { 58 | UsageTracking usageTracking = null; 59 | if (pool instanceof UsageTracking) { 60 | usageTracking = (UsageTracking) pool; 61 | } 62 | return proxySource.createProxy(pool.borrowObject(), usageTracking); 63 | } 64 | 65 | @Override 66 | public void clear() throws E, UnsupportedOperationException { 67 | pool.clear(); 68 | } 69 | 70 | @Override 71 | public void close() { 72 | pool.close(); 73 | } 74 | 75 | @Override 76 | public int getNumActive() { 77 | return pool.getNumActive(); 78 | } 79 | 80 | @Override 81 | public int getNumIdle() { 82 | return pool.getNumIdle(); 83 | } 84 | 85 | @Override 86 | public void invalidateObject(final T proxy) throws E { 87 | pool.invalidateObject(proxySource.resolveProxy(proxy)); 88 | } 89 | 90 | @Override 91 | public void returnObject(final T proxy) throws E { 92 | pool.returnObject(proxySource.resolveProxy(proxy)); 93 | } 94 | 95 | /** 96 | * @since 2.4.3 97 | */ 98 | @Override 99 | public String toString() { 100 | final StringBuilder builder = new StringBuilder(); 101 | builder.append("ProxiedObjectPool [pool="); 102 | builder.append(pool); 103 | builder.append(", proxySource="); 104 | builder.append(proxySource); 105 | builder.append("]"); 106 | return builder.toString(); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/ProxySource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.proxy; 18 | 19 | import org.apache.commons.pool3.UsageTracking; 20 | 21 | /** 22 | * The interface that any provider of proxy instances must implement to allow the 23 | * {@link ProxiedObjectPool} to create proxies as required. 24 | * 25 | * @param type of the pooled object to be proxied 26 | * @since 2.0 27 | */ 28 | interface ProxySource { 29 | 30 | /** 31 | * Creates a new proxy object, wrapping the given pooled object. 32 | * 33 | * @param pooledObject The object to wrap 34 | * @param usageTracking The instance, if any (usually the object pool) to 35 | * be provided with usage tracking information for this 36 | * wrapped object 37 | * 38 | * @return the new proxy object 39 | */ 40 | T createProxy(T pooledObject, UsageTracking usageTracking); 41 | 42 | /** 43 | * Resolves the wrapped object from the given proxy. 44 | * 45 | * @param proxy The proxy object 46 | * @return The pooled object wrapped by the given proxy 47 | */ 48 | T resolveProxy(T proxy); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/pool3/proxy/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /** 19 | * Object pooling proxy implementation. 20 | *

21 | * The org.apache.commons.pool3.proxy package defines a 22 | * object pool that wraps all objects returned to clients. This allows it 23 | * to disable those proxies when the objects are returned thereby enabling 24 | * the continued use of those objects by clients to be detected. 25 | *

26 | *

27 | * Support is provided for java.lang.reflect.Proxy and for 28 | * net.sf.cglib.proxy based proxies. The latter, requires the 29 | * additional of the optional Code Generation Library (GCLib). 30 | *

31 | *

32 | * cglib is unmaintained and does not work well (or possibly at all?) in newer JDKs, particularly JDK17+; see https://github.com/cglib/cglib 33 | *

34 | */ 35 | package org.apache.commons.pool3.proxy; 36 | -------------------------------------------------------------------------------- /src/main/javadoc/overview.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | Overview of the org.apache.commons.pool3 component 20 | 21 | 22 |

23 | Generic Object pooling API with several implementations. 24 |

25 |

26 | The org.apache.commons.pool3 package defines a simple 27 | interface for a pool of object instances, and a handful of base 28 | classes that may be useful when creating pool implementations. 29 | The API supports pooling of unique objects which can be requested 30 | via a key as well as pools where all objects are equivalent. 31 |

32 |

33 | The org.apache.commons.pool3.impl package contains 34 | several pool implementations. 35 | {@link org.apache.commons.pool3.impl.GenericObjectPool 36 | GenericObjectPool} has many configuration options and can support 37 | a limited set of objects such as would be useful in a database 38 | connection pool. 39 | {@link org.apache.commons.pool3.impl.SoftReferenceObjectPool 40 | SoftReferenceObjectPool} has no limit on the number of objects in the 41 | pool, but the garbage collector can remove idle objects from the pool 42 | as needed. There is also a keyed version of 43 | {@link org.apache.commons.pool3.impl.GenericObjectPool 44 | GenericObjectPool}, 45 | {@link org.apache.commons.pool3.impl.GenericKeyedObjectPool 46 | GenericKeyedObjectPool} 47 |

48 | 49 | 50 | -------------------------------------------------------------------------------- /src/site/resources/download_pool.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ----------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one or more 4 | # contributor license agreements. See the NOTICE file distributed with 5 | # this work for additional information regarding copyright ownership. 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 7 | # (the "License"); you may not use this file except in compliance with 8 | # the License. You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ----------------------------------------------------------------------------- 18 | # 19 | # Just call the standard mirrors.cgi script. It will use download.html 20 | # as the input template. 21 | exec /www/www.apache.org/dyn/mirrors/mirrors.cgi $* -------------------------------------------------------------------------------- /src/site/resources/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/commons-pool/1475f3c3ec86fb1140687e387e34ebde3e7d734a/src/site/resources/images/logo.png -------------------------------------------------------------------------------- /src/site/resources/profile.jacoco: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // https://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, 12 | // software distributed under the License is distributed on an 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | // KIND, either express or implied. See the License for the 15 | // specific language governing permissions and limitations 16 | // under the License. 17 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/site/xdoc/guide/index.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 22 | 23 | Developers Guide 24 | Apache Commons Documentation Team 25 | 26 | 27 | 28 | 29 |
30 |

31 | ObjectPool 32 | defines a simple pooling interface. 33 |

34 |
    35 |
  • 36 | GenericObjectPool: 37 | ObjectPool implementation with configurable LIFO/FIFO behavior. 38 | The default behavior is for the pool to act as a LIFO queue. What this 39 | means is that when there are idle objects available in the pool, 40 | borrowObject returns the most recently returned ("last in") instance. 41 | If the lifo the property of the pool false, instances are 42 | returned in the opposite order - first-in, first-out. 43 |
  • 44 |
  • 45 | SoftReferenceObjectPool: 46 | ObjectPool implementation with a LIFO (Last In First Out) behavior. Additionally this pool wraps 47 | each object in a SoftReference allowing the garbage collector to remove them in response to 48 | memory demand. 49 |
  • 50 |
51 |
52 | 53 |
54 |

55 | A KeyedObjectPool 56 | pools instances of multiple types. Each type may be accessed using an arbitrary key. 57 |

58 |
    59 |
  • 60 | GenericKeyedObjectPool: 61 | ObjectPool implementation with configurable LIFO/FIFO behavior. 62 | The default behavior is for the pool to act as a LIFO queue. What this 63 | means is that when there are idle objects available in the pool, 64 | borrowObject returns the most recently returned ("last in") instance. 65 | If the lifo the property of the pool false, instances are 66 | returned in the opposite order - first-in, first-out. 67 |
  • 68 |
69 |
70 | 71 | 72 |
73 | -------------------------------------------------------------------------------- /src/site/xdoc/releases.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 21 | 22 | Downloads 23 | Apache Commons Documentation Team 24 | 25 | 26 | 27 |
28 |

29 | The latest release binary and source releases are always available on the 30 | 31 | Apache Commons Pool Downloads page. 32 |

33 |
    34 |
  • 35 | Version 2 is a major refactoring. The primary change is in the 36 | underlying implementation which uses significantly less locking 37 | and is therefore much better suited to high concurrency 38 | environments. The API has also been made much more self-consistent 39 | with a number of attributes changing name to clarify their 40 | purpose. 41 |
  • 42 |
43 |

44 |

45 |

46 | Older releases are retained by the Apache Software Foundation but are 47 | moved into a 48 | 49 | special archive area. 50 |

51 |

52 | Access to the source tree to see the latest and greatest code is possible 53 | through anonymous access. 54 |

55 |
56 | 57 | 58 |
59 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/MethodCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | import java.util.Arrays; 21 | import java.util.Collections; 22 | import java.util.List; 23 | import java.util.Objects; 24 | 25 | /** 26 | * Holds method names, parameters, and return values for tracing method calls. 27 | */ 28 | public class MethodCall { 29 | private final String name; 30 | private final List params; 31 | private Object returned; 32 | 33 | public MethodCall(final String name) { 34 | this(name, null); 35 | } 36 | 37 | public MethodCall(final String name, final List params) { 38 | if (name == null) { 39 | throw new IllegalArgumentException("name must not be null."); 40 | } 41 | this.name = name; 42 | if (params != null) { 43 | this.params = params; 44 | } else { 45 | this.params = Collections.emptyList(); 46 | } 47 | } 48 | 49 | public MethodCall(final String name, final Object param) { 50 | this(name, Collections.singletonList(param)); 51 | } 52 | 53 | public MethodCall(final String name, final Object param1, final Object param2) { 54 | this(name, Arrays.asList(param1, param2)); 55 | } 56 | 57 | @Override 58 | public boolean equals(final Object o) { 59 | if (this == o) { 60 | return true; 61 | } 62 | if (o == null || getClass() != o.getClass()) { 63 | return false; 64 | } 65 | final MethodCall that = (MethodCall) o; 66 | if (!Objects.equals(name, that.name)) { 67 | return false; 68 | } 69 | if (!Objects.equals(params, that.params)) { 70 | return false; 71 | } 72 | return Objects.equals(returned, that.returned); 73 | } 74 | 75 | public String getName() { 76 | return name; 77 | } 78 | 79 | public List getParams() { 80 | return params; 81 | } 82 | 83 | public Object getReturned() { 84 | return returned; 85 | } 86 | 87 | @Override 88 | public int hashCode() { 89 | int result; 90 | result = name != null ? name.hashCode() : 0; 91 | result = 29 * result + (params != null ? params.hashCode() : 0); 92 | return 29 * result + (returned != null ? returned.hashCode() : 0); 93 | } 94 | 95 | public MethodCall returned(final Object obj) { 96 | setReturned(obj); 97 | return this; 98 | } 99 | 100 | public void setReturned(final Object returned) { 101 | this.returned = returned; 102 | } 103 | 104 | @Override 105 | public String toString() { 106 | final StringBuilder sb = new StringBuilder(); 107 | sb.append("MethodCall"); 108 | sb.append("{name='").append(name).append('\''); 109 | if (!params.isEmpty()) { 110 | sb.append(", params=").append(params); 111 | } 112 | if (returned != null) { 113 | sb.append(", returned=").append(returned); 114 | } 115 | sb.append('}'); 116 | return sb.toString(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/PoolTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; 21 | import static org.junit.jupiter.api.Assertions.assertFalse; 22 | 23 | import java.time.Duration; 24 | 25 | import org.apache.commons.pool3.impl.DefaultPooledObject; 26 | import org.apache.commons.pool3.impl.GenericObjectPool; 27 | import org.apache.commons.pool3.impl.GenericObjectPoolConfig; 28 | import org.junit.jupiter.api.Disabled; 29 | import org.junit.jupiter.api.Test; 30 | 31 | @Disabled 32 | public class PoolTest { 33 | 34 | private static final class Foo { 35 | } 36 | 37 | private static final class PooledFooFactory implements PooledObjectFactory { 38 | private static final long VALIDATION_WAIT_IN_MILLIS = 1000; 39 | 40 | @Override 41 | public void activateObject(final PooledObject pooledObject) { 42 | } 43 | 44 | @Override 45 | public void destroyObject(final PooledObject pooledObject) { 46 | } 47 | 48 | @Override 49 | public PooledObject makeObject() { 50 | return new DefaultPooledObject<>(new Foo()); 51 | } 52 | 53 | @Override 54 | public void passivateObject(final PooledObject pooledObject) { 55 | } 56 | 57 | @Override 58 | public boolean validateObject(final PooledObject pooledObject) { 59 | try { 60 | Thread.sleep(VALIDATION_WAIT_IN_MILLIS); 61 | } catch (final InterruptedException e) { 62 | Thread.interrupted(); 63 | } 64 | return false; 65 | } 66 | } 67 | 68 | private static final CharSequence COMMONS_POOL_EVICTIONS_TIMER_THREAD_NAME = "commons-pool-EvictionTimer"; 69 | 70 | private static final long EVICTION_PERIOD_IN_MILLIS = 100; 71 | 72 | private static final Duration EVICTION_DURATION = Duration.ofMillis(100); 73 | 74 | @Test 75 | public void testPool() { 76 | final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); 77 | poolConfig.setTestWhileIdle(true /* testWhileIdle */); 78 | final PooledFooFactory pooledFooFactory = new PooledFooFactory(); 79 | try (GenericObjectPool pool = new GenericObjectPool<>(pooledFooFactory, poolConfig)) { 80 | pool.setDurationBetweenEvictionRuns(EVICTION_DURATION); 81 | assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis()); 82 | assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis()); 83 | pool.setDurationBetweenEvictionRuns(Duration.ofMillis(EVICTION_PERIOD_IN_MILLIS)); 84 | assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis()); 85 | pool.addObject(); 86 | try { 87 | Thread.sleep(EVICTION_PERIOD_IN_MILLIS); 88 | } catch (final InterruptedException e) { 89 | Thread.interrupted(); 90 | } 91 | } 92 | final Thread[] threads = new Thread[Thread.activeCount()]; 93 | Thread.enumerate(threads); 94 | for (final Thread thread : threads) { 95 | if (thread == null) { 96 | continue; 97 | } 98 | final String name = thread.getName(); 99 | assertFalse(name.contains(COMMONS_POOL_EVICTIONS_TIMER_THREAD_NAME), name); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/PrivateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | /** 21 | * An exception that only is thrown by these tests. 22 | */ 23 | public class PrivateException extends RuntimeException { 24 | private static final long serialVersionUID = 1L; 25 | 26 | public PrivateException(final String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/TestBaseKeyedPooledObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertTrue; 20 | 21 | import org.apache.commons.pool3.impl.DefaultPooledObject; 22 | import org.junit.jupiter.api.Test; 23 | 24 | /** 25 | */ 26 | public class TestBaseKeyedPooledObjectFactory { 27 | 28 | private static final class TestFactory extends BaseKeyedPooledObjectFactory { 29 | @Override 30 | public Object create(final Object key) { 31 | return null; 32 | } 33 | 34 | @Override 35 | public PooledObject wrap(final Object value) { 36 | return new DefaultPooledObject<>(value); 37 | } 38 | } 39 | 40 | @Test 41 | public void testDefaultMethods() { 42 | final KeyedPooledObjectFactory factory = new TestFactory(); 43 | 44 | factory.activateObject("key", null); // a no-op 45 | factory.passivateObject("key", null); // a no-op 46 | factory.destroyObject("key", null); // a no-op 47 | assertTrue(factory.validateObject("key", null)); // constant true 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/TestBasePoolableObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertTrue; 21 | 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | 24 | import org.apache.commons.pool3.impl.DefaultPooledObject; 25 | import org.junit.jupiter.api.Test; 26 | 27 | /** 28 | */ 29 | public class TestBasePoolableObjectFactory { 30 | 31 | private static final class TestFactory extends BasePooledObjectFactory { 32 | @Override 33 | public AtomicInteger create() { 34 | return new AtomicInteger(); 35 | } 36 | 37 | @Override 38 | public void destroyObject(final PooledObject p, final DestroyMode destroyMode) { 39 | if (destroyMode.equals(DestroyMode.ABANDONED)) { 40 | p.getObject().incrementAndGet(); 41 | } 42 | } 43 | 44 | @Override 45 | public PooledObject wrap(final AtomicInteger value) { 46 | return new DefaultPooledObject<>(value); 47 | } 48 | } 49 | 50 | @Test 51 | public void testDefaultMethods() { 52 | final PooledObjectFactory factory = new TestFactory(); 53 | 54 | factory.activateObject(null); // a no-op 55 | factory.passivateObject(null); // a no-op 56 | factory.destroyObject(null); // a no-op 57 | assertTrue(factory.validateObject(null)); // constant true 58 | } 59 | 60 | /** 61 | * Default destroy does nothing to underlying AtomicInt, ABANDONED mode increments the value. Verify that destroy with no mode does default, destroy with 62 | * ABANDONED mode increments. 63 | * 64 | * @throws RuntimeException May occur in some failure modes 65 | */ 66 | @Test 67 | public void testDestroyModes() { 68 | final PooledObjectFactory factory = new TestFactory(); 69 | final PooledObject pooledObj = factory.makeObject(); 70 | final AtomicInteger obj = pooledObj.getObject(); 71 | factory.destroyObject(pooledObj); 72 | assertEquals(0, obj.get()); 73 | factory.destroyObject(pooledObj, DestroyMode.ABANDONED); 74 | assertEquals(1, obj.get()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/TestException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | /** 21 | * A custom exception for unit tests. 22 | */ 23 | public class TestException extends Exception { 24 | 25 | private static final long serialVersionUID = 1L; 26 | 27 | public TestException() { 28 | // empty 29 | } 30 | 31 | public TestException(final String message) { 32 | super(message); 33 | } 34 | 35 | public TestException(final String message, final Throwable cause) { 36 | super(message, cause); 37 | } 38 | 39 | public TestException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) { 40 | super(message, cause, enableSuppression, writableStackTrace); 41 | } 42 | 43 | public TestException(final Throwable cause) { 44 | super(cause); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/TestTrackedUse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; 21 | 22 | import java.time.Instant; 23 | 24 | import org.junit.jupiter.api.Test; 25 | 26 | public class TestTrackedUse { 27 | 28 | final class DefaultTrackedUse implements TrackedUse { 29 | 30 | @Override 31 | public Instant getLastUsedInstant() { 32 | return Instant.ofEpochMilli(1); 33 | } 34 | 35 | } 36 | 37 | @Test 38 | public void testDefaultGetLastUsedInstant() { 39 | assertEquals(Instant.ofEpochMilli(1), new DefaultTrackedUse().getLastUsedInstant()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/VisitTracker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3; 18 | 19 | /** 20 | * Test pooled object class. Keeps track of how many times it has been validated, activated, passivated. 21 | * 22 | * @param The key type. 23 | */ 24 | public class VisitTracker { 25 | 26 | private int validateCount; 27 | private int activateCount; 28 | private int passivateCount; 29 | private boolean destroyed; 30 | private int id; 31 | private K key; 32 | 33 | public VisitTracker() { 34 | reset(); 35 | } 36 | 37 | public VisitTracker(final int id) { 38 | this.id = id; 39 | reset(); 40 | } 41 | 42 | public VisitTracker(final int id, final K key) { 43 | this.id = id; 44 | this.key = key; 45 | reset(); 46 | } 47 | 48 | public void activate() { 49 | if (destroyed) { 50 | fail("attempted to activate a destroyed object"); 51 | } 52 | activateCount++; 53 | } 54 | 55 | public void destroy() { 56 | destroyed = true; 57 | } 58 | 59 | private void fail(final String message) { 60 | throw new IllegalStateException(message); 61 | } 62 | 63 | public int getActivateCount() { 64 | return activateCount; 65 | } 66 | 67 | public int getId() { 68 | return id; 69 | } 70 | 71 | public K getKey() { 72 | return key; 73 | } 74 | 75 | public int getPassivateCount() { 76 | return passivateCount; 77 | } 78 | 79 | public int getValidateCount() { 80 | return validateCount; 81 | } 82 | 83 | public boolean isDestroyed() { 84 | return destroyed; 85 | } 86 | 87 | public void passivate() { 88 | if (destroyed) { 89 | fail("attempted to passivate a destroyed object"); 90 | } 91 | passivateCount++; 92 | } 93 | 94 | public void reset() { 95 | validateCount = 0; 96 | activateCount = 0; 97 | passivateCount = 0; 98 | destroyed = false; 99 | } 100 | 101 | @Override 102 | public String toString() { 103 | return "Key: " + key + " id: " + id; 104 | } 105 | 106 | public boolean validate() { 107 | if (destroyed) { 108 | fail("attempted to validate a destroyed object"); 109 | } 110 | validateCount++; 111 | return true; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/VisitTrackerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3; 19 | 20 | import org.apache.commons.pool3.impl.DefaultPooledObject; 21 | 22 | /** 23 | * Factory that creates VisitTracker instances. Used to test Evictor runs. 24 | * 25 | * @param The VisitTracker key type. 26 | */ 27 | public class VisitTrackerFactory 28 | implements PooledObjectFactory, RuntimeException>, KeyedPooledObjectFactory, RuntimeException> { 29 | 30 | private int nextId; 31 | 32 | @Override 33 | public void activateObject(final K key, final PooledObject> ref) { 34 | ref.getObject().activate(); 35 | } 36 | 37 | @Override 38 | public void activateObject(final PooledObject> ref) { 39 | ref.getObject().activate(); 40 | } 41 | 42 | @Override 43 | public void destroyObject(final K key, final PooledObject> ref) { 44 | ref.getObject().destroy(); 45 | } 46 | 47 | @Override 48 | public void destroyObject(final PooledObject> ref) { 49 | ref.getObject().destroy(); 50 | } 51 | 52 | @Override 53 | public PooledObject> makeObject() { 54 | return new DefaultPooledObject<>(new VisitTracker<>(nextId++)); 55 | } 56 | 57 | @Override 58 | public PooledObject> makeObject(final K key) { 59 | return new DefaultPooledObject<>(new VisitTracker<>(nextId++, key)); 60 | } 61 | 62 | @Override 63 | public void passivateObject(final K key, final PooledObject> ref) { 64 | ref.getObject().passivate(); 65 | } 66 | 67 | @Override 68 | public void passivateObject(final PooledObject> ref) { 69 | ref.getObject().passivate(); 70 | } 71 | 72 | public void resetId() { 73 | nextId = 0; 74 | } 75 | 76 | @Override 77 | public boolean validateObject(final K key, final PooledObject> ref) { 78 | return ref.getObject().validate(); 79 | } 80 | 81 | @Override 82 | public boolean validateObject(final PooledObject> ref) { 83 | return ref.getObject().validate(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/AtomicIntegerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import java.util.concurrent.atomic.AtomicInteger; 20 | 21 | import org.apache.commons.pool3.BasePooledObjectFactory; 22 | import org.apache.commons.pool3.PooledObject; 23 | import org.apache.commons.pool3.Waiter; 24 | 25 | /** 26 | * Factory that sources PooledObjects that wrap AtomicIntegers. 27 | * {@link #create()} creates an AtomicInteger with value 0, activate increments 28 | * the value of the wrapped AtomicInteger and passivate decrements it. Latency 29 | * of factory methods is configurable. 30 | */ 31 | public class AtomicIntegerFactory 32 | extends BasePooledObjectFactory { 33 | 34 | private long activateLatency; 35 | private long passivateLatency; 36 | private long createLatency; 37 | private long destroyLatency; 38 | private long validateLatency; 39 | 40 | @Override 41 | public void activateObject(final PooledObject p) { 42 | p.getObject().incrementAndGet(); 43 | Waiter.sleepQuietly(activateLatency); 44 | } 45 | 46 | @Override 47 | public AtomicInteger create() { 48 | Waiter.sleepQuietly(createLatency); 49 | return new AtomicInteger(); 50 | } 51 | 52 | @Override 53 | public void destroyObject(final PooledObject p) { 54 | Waiter.sleepQuietly(destroyLatency); 55 | } 56 | 57 | @Override 58 | public void passivateObject(final PooledObject p) { 59 | p.getObject().decrementAndGet(); 60 | Waiter.sleepQuietly(passivateLatency); 61 | } 62 | 63 | /** 64 | * @param activateLatency the activateLatency to set 65 | */ 66 | public void setActivateLatency(final long activateLatency) { 67 | this.activateLatency = activateLatency; 68 | } 69 | 70 | /** 71 | * @param createLatency the createLatency to set 72 | */ 73 | public void setCreateLatency(final long createLatency) { 74 | this.createLatency = createLatency; 75 | } 76 | 77 | /** 78 | * @param destroyLatency the destroyLatency to set 79 | */ 80 | public void setDestroyLatency(final long destroyLatency) { 81 | this.destroyLatency = destroyLatency; 82 | } 83 | 84 | /** 85 | * @param passivateLatency the passivateLatency to set 86 | */ 87 | public void setPassivateLatency(final long passivateLatency) { 88 | this.passivateLatency = passivateLatency; 89 | } 90 | 91 | /** 92 | * @param validateLatency the validateLatency to set 93 | */ 94 | public void setValidateLatency(final long validateLatency) { 95 | this.validateLatency = validateLatency; 96 | } 97 | 98 | @Override 99 | public boolean validateObject(final PooledObject instance) { 100 | Waiter.sleepQuietly(validateLatency); 101 | return instance.getObject().intValue() == 1; 102 | } 103 | 104 | @Override 105 | public PooledObject wrap(final AtomicInteger integer) { 106 | return new DefaultPooledObject<>(integer); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/CallStackTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache license, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the license for the specific language governing permissions and 15 | * limitations under the license. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertTrue; 21 | 22 | import java.io.PrintWriter; 23 | import java.io.StringWriter; 24 | import java.util.stream.Stream; 25 | 26 | import org.junit.jupiter.params.ParameterizedTest; 27 | import org.junit.jupiter.params.provider.Arguments; 28 | import org.junit.jupiter.params.provider.MethodSource; 29 | 30 | public class CallStackTest { 31 | 32 | public static Stream data() { 33 | // @formatter:off 34 | return Stream.of( 35 | Arguments.arguments(new ThrowableCallStack("Test", false)), 36 | Arguments.arguments(new ThrowableCallStack("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", true)), 37 | Arguments.arguments(new SecurityManagerCallStack("Test", false)), 38 | Arguments.arguments(new SecurityManagerCallStack("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", true)) 39 | ); 40 | // @formatter:on 41 | } 42 | 43 | private final StringWriter writer = new StringWriter(); 44 | 45 | @ParameterizedTest 46 | @MethodSource("data") 47 | public void testPrintClearedStackTraceIsNoOp(final CallStack stack) { 48 | stack.fillInStackTrace(); 49 | stack.clear(); 50 | stack.printStackTrace(new PrintWriter(writer)); 51 | final String stackTrace = writer.toString(); 52 | assertEquals("", stackTrace); 53 | } 54 | 55 | @ParameterizedTest 56 | @MethodSource("data") 57 | public void testPrintFilledStackTrace(final CallStack stack) { 58 | stack.fillInStackTrace(); 59 | stack.printStackTrace(new PrintWriter(writer)); 60 | final String stackTrace = writer.toString(); 61 | assertTrue(stackTrace.contains(getClass().getName())); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/NoOpCallStackTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | 21 | import java.io.PrintWriter; 22 | import java.io.StringWriter; 23 | 24 | import org.junit.jupiter.api.Test; 25 | 26 | public class NoOpCallStackTest { 27 | @Test 28 | public void testPrintStackTraceIsNoOp() { 29 | final CallStack stack = NoOpCallStack.INSTANCE; 30 | stack.fillInStackTrace(); 31 | final StringWriter writer = new StringWriter(); 32 | stack.printStackTrace(new PrintWriter(writer)); 33 | assertEquals("", writer.toString()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestCallStackUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.impl; 19 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; 21 | import static org.junit.jupiter.api.Assertions.assertTrue; 22 | 23 | import java.io.PrintWriter; 24 | import java.io.StringWriter; 25 | 26 | import org.junit.jupiter.api.Test; 27 | 28 | /** 29 | * Tests {@link CallStackUtils}. 30 | */ 31 | public class TestCallStackUtils { 32 | 33 | private static final String MESSAGE_FORMAT = "'Timestamp:' yyyy-MM-dd HH:mm:ss Z"; 34 | 35 | private void assertNewCallStack(final CallStack callStack) { 36 | callStack.fillInStackTrace(); 37 | final StringWriter out = new StringWriter(); 38 | callStack.printStackTrace(new PrintWriter(out)); 39 | assertFalse(out.toString().isEmpty()); 40 | callStack.clear(); 41 | out.getBuffer().setLength(0); 42 | callStack.printStackTrace(new PrintWriter(out)); 43 | assertTrue(out.toString().isEmpty()); 44 | } 45 | 46 | @Test 47 | public void testNewCallStack3() { 48 | assertNewCallStack(CallStackUtils.newCallStack(MESSAGE_FORMAT, false, false)); 49 | assertNewCallStack(CallStackUtils.newCallStack(MESSAGE_FORMAT, false, true)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.impl; 19 | 20 | import java.time.Duration; 21 | 22 | /** 23 | * Constants used in tests. 24 | */ 25 | public class TestConstants { 26 | 27 | /** 28 | * A duration of one second. 29 | */ 30 | public static final Duration ONE_SECOND_DURATION = Duration.ofSeconds(1); 31 | 32 | /** 33 | * A duration of one minute. 34 | */ 35 | public static final Duration ONE_MINUTE_DURATION = Duration.ofMinutes(1); 36 | 37 | /** 38 | * A duration of one millisecond. 39 | */ 40 | public static final Duration ONE_MILLISECOND_DURATION = Duration.ofMillis(1); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestEvictionConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertFalse; 21 | 22 | import java.time.Duration; 23 | 24 | import org.junit.jupiter.api.Test; 25 | 26 | /** 27 | * Tests for {@link EvictionConfig}. 28 | */ 29 | public class TestEvictionConfig { 30 | 31 | @Test 32 | public void testConstructor1s() { 33 | final EvictionConfig config = new EvictionConfig(Duration.ofMillis(1), Duration.ofMillis(1), 1); 34 | assertEquals(1, config.getIdleEvictDuration().toMillis()); 35 | assertEquals(1, config.getIdleSoftEvictDuration().toMillis()); 36 | assertEquals(1, config.getMinIdle()); 37 | // toString() should never throw 38 | assertFalse(config.toString().isEmpty()); 39 | } 40 | 41 | @Test 42 | public void testConstructorZerosDurations() { 43 | final EvictionConfig config = new EvictionConfig(Duration.ZERO, Duration.ZERO, 0); 44 | assertEquals(Long.MAX_VALUE, config.getIdleEvictDuration().toMillis()); 45 | assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictDuration().toMillis()); 46 | assertEquals(0, config.getMinIdle()); 47 | // toString() should never throw 48 | assertFalse(config.toString().isEmpty()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestPooledSoftReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | 21 | import java.lang.ref.SoftReference; 22 | 23 | import org.junit.jupiter.api.BeforeEach; 24 | import org.junit.jupiter.api.Test; 25 | 26 | /** 27 | * Tests for PooledSoftReference. 28 | */ 29 | public class TestPooledSoftReference { 30 | 31 | private static final String REFERENT = "test"; 32 | private static final String REFERENT2 = "test2"; 33 | PooledSoftReference ref; 34 | 35 | @BeforeEach 36 | public void setUp() { 37 | final SoftReference softRef = new SoftReference<>(REFERENT); 38 | ref = new PooledSoftReference<>(softRef); 39 | } 40 | 41 | @Test 42 | public void testPooledSoftReference() { 43 | assertEquals(REFERENT, ref.getObject()); 44 | 45 | SoftReference softRef = ref.getReference(); 46 | assertEquals(REFERENT, softRef.get()); 47 | softRef.clear(); 48 | 49 | softRef = new SoftReference<>(REFERENT2); 50 | ref.setReference(softRef); 51 | 52 | assertEquals(REFERENT2, ref.getObject()); 53 | 54 | softRef = ref.getReference(); 55 | assertEquals(REFERENT2, softRef.get()); 56 | softRef.clear(); 57 | } 58 | 59 | @Test 60 | public void testToString() { 61 | final String expected = "Referenced Object: test, State: IDLE"; 62 | assertEquals(expected, ref.toString()); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestSoftReferenceObjectPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.impl; 18 | 19 | import org.apache.commons.pool3.BasePooledObjectFactory; 20 | import org.apache.commons.pool3.ObjectPool; 21 | import org.apache.commons.pool3.PooledObject; 22 | import org.apache.commons.pool3.PooledObjectFactory; 23 | import org.apache.commons.pool3.TestBaseObjectPool; 24 | 25 | /** 26 | */ 27 | public class TestSoftReferenceObjectPool extends TestBaseObjectPool { 28 | 29 | private static final class SimpleFactory extends BasePooledObjectFactory { 30 | 31 | int counter; 32 | 33 | @Override 34 | public String create() { 35 | return String.valueOf(counter++); 36 | } 37 | 38 | @Override 39 | public PooledObject wrap(final String value) { 40 | return new DefaultPooledObject<>(value); 41 | } 42 | } 43 | 44 | @Override 45 | protected Object getNthObject(final int n) { 46 | return String.valueOf(n); 47 | } 48 | 49 | @Override 50 | protected boolean isFifo() { 51 | return false; 52 | } 53 | 54 | @Override 55 | protected boolean isLifo() { 56 | return false; 57 | } 58 | 59 | @Override 60 | protected ObjectPool makeEmptyPool(final int cap) { 61 | return (ObjectPool) new SoftReferenceObjectPool<>(new SimpleFactory()); 62 | } 63 | 64 | @Override 65 | protected ObjectPool makeEmptyPool(final PooledObjectFactory factory) { 66 | return new SoftReferenceObjectPool<>(factory); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/impl/TestSynchronizedPooledObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.impl; 19 | 20 | import java.util.concurrent.locks.ReentrantReadWriteLock; 21 | import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; 22 | 23 | import org.apache.commons.pool3.PooledObject; 24 | import org.apache.commons.pool3.PooledObjectFactory; 25 | 26 | /** 27 | * Copies PoolUtil's private static final class SynchronizedPooledObjectFactory. 28 | * 29 | * A fully synchronized PooledObjectFactory that wraps a PooledObjectFactory and synchronizes access to the wrapped factory methods. 30 | *

31 | * Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons 32 | * Pool library. 33 | *

34 | * 35 | * @param Type of element managed in this factory. 36 | * @param Type of exception thrown in this factory. 37 | */ 38 | final class TestSynchronizedPooledObjectFactory implements PooledObjectFactory { 39 | 40 | /** Synchronization lock */ 41 | private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock(); 42 | 43 | /** Wrapped factory */ 44 | private final PooledObjectFactory factory; 45 | 46 | /** 47 | * Constructs a SynchronizedPoolableObjectFactory wrapping the given factory. 48 | * 49 | * @param factory underlying factory to wrap 50 | * @throws IllegalArgumentException if the factory is null 51 | */ 52 | TestSynchronizedPooledObjectFactory(final PooledObjectFactory factory) throws IllegalArgumentException { 53 | if (factory == null) { 54 | throw new IllegalArgumentException("factory must not be null."); 55 | } 56 | this.factory = factory; 57 | } 58 | 59 | /** 60 | * {@inheritDoc} 61 | */ 62 | @Override 63 | public void activateObject(final PooledObject p) throws E { 64 | writeLock.lock(); 65 | try { 66 | factory.activateObject(p); 67 | } finally { 68 | writeLock.unlock(); 69 | } 70 | } 71 | 72 | /** 73 | * {@inheritDoc} 74 | */ 75 | @Override 76 | public void destroyObject(final PooledObject p) throws E { 77 | writeLock.lock(); 78 | try { 79 | factory.destroyObject(p); 80 | } finally { 81 | writeLock.unlock(); 82 | } 83 | } 84 | 85 | /** 86 | * {@inheritDoc} 87 | */ 88 | @Override 89 | public PooledObject makeObject() throws E { 90 | writeLock.lock(); 91 | try { 92 | return factory.makeObject(); 93 | } finally { 94 | writeLock.unlock(); 95 | } 96 | } 97 | 98 | /** 99 | * {@inheritDoc} 100 | */ 101 | @Override 102 | public void passivateObject(final PooledObject p) throws E { 103 | writeLock.lock(); 104 | try { 105 | factory.passivateObject(p); 106 | } finally { 107 | writeLock.unlock(); 108 | } 109 | } 110 | 111 | /** 112 | * {@inheritDoc} 113 | */ 114 | @Override 115 | public String toString() { 116 | final StringBuilder sb = new StringBuilder(); 117 | sb.append("SynchronizedPoolableObjectFactory"); 118 | sb.append("{factory=").append(factory); 119 | sb.append('}'); 120 | return sb.toString(); 121 | } 122 | 123 | /** 124 | * {@inheritDoc} 125 | */ 126 | @Override 127 | public boolean validateObject(final PooledObject p) { 128 | writeLock.lock(); 129 | try { 130 | return factory.validateObject(p); 131 | } finally { 132 | writeLock.unlock(); 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/performance/SleepingObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.performance; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | import org.apache.commons.pool3.PooledObjectFactory; 22 | import org.apache.commons.pool3.Waiter; 23 | import org.apache.commons.pool3.impl.DefaultPooledObject; 24 | 25 | /** 26 | * Sleepy ObjectFactory (everything takes a while longer) 27 | */ 28 | public class SleepingObjectFactory implements PooledObjectFactory { 29 | 30 | private int counter; 31 | private boolean debug; 32 | 33 | @Override 34 | public void activateObject(final PooledObject obj) { 35 | debug("activateObject", obj); 36 | Waiter.sleepQuietly(10); 37 | } 38 | 39 | private void debug(final String method, final Object obj) { 40 | if (debug) { 41 | final String thread = "thread" + Thread.currentThread().getName(); 42 | System.out.println(thread + ": " + method + " " + obj); 43 | } 44 | } 45 | 46 | @Override 47 | public void destroyObject(final PooledObject obj) { 48 | debug("destroyObject", obj); 49 | Waiter.sleepQuietly(250); 50 | } 51 | 52 | public boolean isDebug() { 53 | return debug; 54 | } 55 | 56 | @Override 57 | public PooledObject makeObject() { 58 | // Deliberate choice to create a new object in case future unit tests 59 | // check for a specific object. 60 | final Integer obj = Integer.valueOf(counter++); 61 | debug("makeObject", obj); 62 | Waiter.sleepQuietly(500); 63 | return new DefaultPooledObject<>(obj); 64 | } 65 | 66 | @Override 67 | public void passivateObject(final PooledObject obj) { 68 | debug("passivateObject", obj); 69 | Waiter.sleepQuietly(10); 70 | } 71 | 72 | public void setDebug(final boolean b) { 73 | debug = b; 74 | } 75 | 76 | @Override 77 | public boolean validateObject(final PooledObject obj) { 78 | debug("validateObject", obj); 79 | Waiter.sleepQuietly(30); 80 | return true; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/AbstractKeyedPool407Factory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.BaseKeyedPooledObjectFactory; 21 | import org.apache.commons.pool3.PooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public abstract class AbstractKeyedPool407Factory extends BaseKeyedPooledObjectFactory { 27 | 28 | abstract boolean isDefaultMakeObject(); 29 | 30 | @Override 31 | public boolean validateObject(final String key, final PooledObject p) { 32 | // TODO Should this be enough even if wrap() does throw and returns a DefaultPooledObject wrapping a null? 33 | return !PooledObject.isNull(p); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/AbstractPool407Factory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.BasePooledObjectFactory; 21 | import org.apache.commons.pool3.PooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public abstract class AbstractPool407Factory extends BasePooledObjectFactory { 27 | 28 | /** 29 | * Tests whether the subclass relies on the Pool's implementation of makeObject(). If the subclass returns false, then it implements makeObject(), in which 30 | * case makeObject() returns a bad object like null or a null wrapper. 31 | * 32 | * @return whether the subclass relies on the Pool's implementation of makeObject(). 33 | */ 34 | abstract boolean isDefaultMakeObject(); 35 | 36 | /** 37 | * Tests whether this instance makes null or null wrappers. 38 | * 39 | * @return whether this instance makes null or null wrappers. 40 | */ 41 | abstract boolean isNullFactory(); 42 | 43 | @Override 44 | public boolean validateObject(final PooledObject p) { 45 | // TODO Should this be enough even if wrap() does throw and returns a DefaultPooledObject wrapping a null? 46 | return !PooledObject.isNull(p); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/AbstractPool407Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; 21 | import static org.junit.jupiter.api.Assertions.assertTrue; 22 | 23 | import java.time.Duration; 24 | 25 | import org.apache.commons.pool3.PooledObject; 26 | 27 | /** 28 | * Tests POOL-407. 29 | */ 30 | public class AbstractPool407Test { 31 | 32 | protected void assertShutdown(final boolean termination, final Duration poolConfigMaxWait, final T obj, final PooledObject pooledObject) { 33 | if (pooledObject != null) { 34 | // The factory makes non-null objects and non-null PooledObjects, 35 | // therefore the ExecutorService should terminate when requested, without delay. 36 | assertTrue(termination); 37 | } else { 38 | // The factory makes null objects or null PooledObjects, 39 | // therefore the ExecutorService should keep trying to create objects as configured in the pool's config object. 40 | if (poolConfigMaxWait.equals(Pool407Constants.WAIT_FOREVER)) { 41 | // If poolConfigMaxWait is maxed out, then the ExecutorService will not shutdown without delay. 42 | if (obj == null) { 43 | // create() returned null, so wrap() was not even called, and borrowObject() fails fast. 44 | assertTrue(termination); 45 | } else { 46 | // The ExecutorService fails to terminate when requested because 47 | assertFalse(true); 48 | } 49 | } else { 50 | // If poolConfigMaxWait is short, then the ExecutorService should usually shutdown without delay. 51 | assertTrue(termination); 52 | } 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import java.time.Duration; 21 | 22 | import org.apache.commons.pool3.BaseKeyedPooledObjectFactory; 23 | import org.apache.commons.pool3.impl.BaseObjectPoolConfig; 24 | import org.apache.commons.pool3.impl.GenericKeyedObjectPool; 25 | 26 | public final class KeyedPool407 extends GenericKeyedObjectPool { 27 | 28 | public KeyedPool407(final BaseKeyedPooledObjectFactory factory, final Duration maxWait) { 29 | super(factory, new KeyedPool407Config(BaseObjectPoolConfig.DEFAULT_MAX_WAIT)); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import java.time.Duration; 21 | 22 | import org.apache.commons.pool3.impl.GenericKeyedObjectPoolConfig; 23 | 24 | public final class KeyedPool407Config extends GenericKeyedObjectPoolConfig { 25 | 26 | public KeyedPool407Config(final Duration poolConfigMaxWait) { 27 | setBlockWhenExhausted(Pool407Constants.BLOCK_WHEN_EXHAUSTED); 28 | setMaxTotalPerKey(Pool407Constants.MAX_TOTAL); 29 | setMaxWait(poolConfigMaxWait); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407Fixture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | public final class KeyedPool407Fixture { 21 | // empty 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407NormalFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | import org.apache.commons.pool3.impl.DefaultPooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public final class KeyedPool407NormalFactory extends AbstractKeyedPool407Factory { 27 | 28 | private final KeyedPool407Fixture fixture; 29 | 30 | KeyedPool407NormalFactory(final KeyedPool407Fixture fixture) { 31 | this.fixture = fixture; 32 | } 33 | 34 | @Override 35 | public KeyedPool407Fixture create(final String key) { 36 | // This is key to the test, creation failed and returns null for instance see 37 | // https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.io.transport.modbus/ 38 | // src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java#L163 39 | // the test passes when this returns new Pool407Fixture(); 40 | return fixture; 41 | } 42 | 43 | @Override 44 | boolean isDefaultMakeObject() { 45 | return true; 46 | } 47 | 48 | @Override 49 | public PooledObject wrap(final KeyedPool407Fixture value) { 50 | // value will never be null here. 51 | return new DefaultPooledObject<>(value); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407NullObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | import org.apache.commons.pool3.impl.DefaultPooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public final class KeyedPool407NullObjectFactory extends AbstractKeyedPool407Factory { 27 | 28 | @Override 29 | public KeyedPool407Fixture create(final String key) { 30 | return null; 31 | } 32 | 33 | @Override 34 | boolean isDefaultMakeObject() { 35 | return false; 36 | } 37 | 38 | @Override 39 | public PooledObject makeObject(final String key) throws RuntimeException { 40 | return new DefaultPooledObject<>(null); 41 | } 42 | 43 | @Override 44 | public PooledObject wrap(final KeyedPool407Fixture value) { 45 | return new DefaultPooledObject<>(null); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/KeyedPool407NullPoolableObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | 22 | /** 23 | * Tests POOL-407. 24 | */ 25 | public final class KeyedPool407NullPoolableObjectFactory extends AbstractKeyedPool407Factory { 26 | 27 | @Override 28 | public KeyedPool407Fixture create(final String key) { 29 | return null; 30 | } 31 | 32 | @Override 33 | boolean isDefaultMakeObject() { 34 | return false; 35 | } 36 | 37 | @Override 38 | public PooledObject makeObject(final String key) throws RuntimeException { 39 | return null; 40 | } 41 | 42 | @Override 43 | public PooledObject wrap(final KeyedPool407Fixture value) { 44 | return null; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import java.time.Duration; 21 | 22 | import org.apache.commons.pool3.BasePooledObjectFactory; 23 | import org.apache.commons.pool3.impl.GenericObjectPool; 24 | 25 | /** 26 | * Tests POOL-407. 27 | */ 28 | public final class Pool407 extends GenericObjectPool { 29 | 30 | public Pool407(final BasePooledObjectFactory factory, final Duration poolConfigMaxWait) { 31 | super(factory, new Pool407Config(poolConfigMaxWait)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import java.time.Duration; 21 | 22 | import org.apache.commons.pool3.impl.GenericObjectPoolConfig; 23 | 24 | /** 25 | * Tests POOL-407. 26 | */ 27 | public final class Pool407Config extends GenericObjectPoolConfig { 28 | 29 | public Pool407Config(final Duration poolConfigMaxWait) { 30 | setBlockWhenExhausted(Pool407Constants.BLOCK_WHEN_EXHAUSTED); 31 | setMaxTotal(Pool407Constants.MAX_TOTAL); 32 | setMaxWait(poolConfigMaxWait); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import java.time.Duration; 21 | 22 | import org.apache.commons.pool3.impl.BaseObjectPoolConfig; 23 | 24 | /** 25 | * Tests POOL-407. 26 | */ 27 | final class Pool407Constants { 28 | 29 | static final int AWAIT_TERMINATION_SECONDS = 10; 30 | static final boolean BLOCK_WHEN_EXHAUSTED = true; 31 | static final int MAX_TOTAL = 1; 32 | static final int POOL_SIZE = 3; 33 | static final Duration WAIT_FOREVER = BaseObjectPoolConfig.DEFAULT_MAX_WAIT; 34 | static final Duration WAIT_SHORT = Duration.ofSeconds(1); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407Fixture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | public final class Pool407Fixture { 21 | // empty 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407NormalFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | import org.apache.commons.pool3.impl.DefaultPooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public final class Pool407NormalFactory extends AbstractPool407Factory { 27 | 28 | private final Pool407Fixture fixture; 29 | 30 | Pool407NormalFactory(final Pool407Fixture fixture) { 31 | this.fixture = fixture; 32 | } 33 | 34 | @Override 35 | public Pool407Fixture create() { 36 | // When this returns null, we fail-fast internally and borrowsObject() throws an exception. 37 | // 38 | // Old note: 39 | // This is key to the test, creation failed and returns null for instance see 40 | // https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.io.transport.modbus/ 41 | // src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java#L163 42 | // the test passes when this returns new Pool407Fixture(); 43 | return fixture; 44 | } 45 | 46 | @Override 47 | boolean isDefaultMakeObject() { 48 | return true; 49 | } 50 | 51 | @Override 52 | boolean isNullFactory() { 53 | return fixture == null; 54 | } 55 | 56 | @Override 57 | public PooledObject wrap(final Pool407Fixture value) { 58 | // value will never be null here. 59 | return new DefaultPooledObject<>(value); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407NullObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | import org.apache.commons.pool3.impl.DefaultPooledObject; 22 | 23 | /** 24 | * Tests POOL-407. 25 | */ 26 | public final class Pool407NullObjectFactory extends AbstractPool407Factory { 27 | 28 | @Override 29 | public Pool407Fixture create() { 30 | // Never called because this class calls makeObject() and wrap(). 31 | return null; 32 | } 33 | 34 | @Override 35 | boolean isDefaultMakeObject() { 36 | return false; 37 | } 38 | 39 | @Override 40 | boolean isNullFactory() { 41 | return true; 42 | } 43 | 44 | @Override 45 | public PooledObject makeObject() throws RuntimeException { 46 | return new DefaultPooledObject<>(null); 47 | } 48 | 49 | @Override 50 | public PooledObject wrap(final Pool407Fixture value) { 51 | return new DefaultPooledObject<>(null); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/pool407/Pool407NullPoolableObjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.pool407; 19 | 20 | import org.apache.commons.pool3.PooledObject; 21 | 22 | /** 23 | * Tests POOL-407. 24 | */ 25 | public final class Pool407NullPoolableObjectFactory extends AbstractPool407Factory { 26 | 27 | @Override 28 | public Pool407Fixture create() { 29 | return null; 30 | } 31 | 32 | @Override 33 | boolean isDefaultMakeObject() { 34 | return false; 35 | } 36 | 37 | @Override 38 | boolean isNullFactory() { 39 | return true; 40 | } 41 | 42 | @Override 43 | public PooledObject makeObject() throws RuntimeException { 44 | return null; 45 | } 46 | 47 | @Override 48 | public PooledObject wrap(final Pool407Fixture value) { 49 | return null; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.proxy; 18 | 19 | public class TestProxiedKeyedObjectPoolWithCglibProxy extends 20 | AbstractTestProxiedKeyedObjectPool { 21 | 22 | @Override 23 | protected ProxySource getProxySource() { 24 | return new CglibProxySource<>(TestObject.class); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.commons.pool3.proxy; 18 | 19 | public class TestProxiedKeyedObjectPoolWithJdkProxy 20 | extends AbstractTestProxiedKeyedObjectPool { 21 | 22 | @Override 23 | protected ProxySource getProxySource() { 24 | return new JdkProxySource<>(this.getClass().getClassLoader(), 25 | new Class[] { TestObject.class }); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/proxy/TestProxiedObjectPoolWithCglibProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.proxy; 19 | 20 | import java.lang.reflect.InvocationTargetException; 21 | 22 | public class TestProxiedObjectPoolWithCglibProxy extends AbstractTestProxiedObjectPool { 23 | 24 | @Override 25 | protected ProxySource getProxySource(boolean unwrapInvocationTargetException) { 26 | // @formatter:off 27 | return CglibProxySource.builder() 28 | .setSuperclass(TestObject.class) 29 | .setUnwrapInvocationTargetException(unwrapInvocationTargetException) 30 | .get(); 31 | // @formatter:on 32 | } 33 | 34 | @Override 35 | protected Class getInvocationTargetExceptionType() { 36 | return InvocationTargetException.class; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/apache/commons/pool3/proxy/TestProxiedObjectPoolWithJdkProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * https://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.pool3.proxy; 19 | 20 | import java.lang.reflect.UndeclaredThrowableException; 21 | 22 | public class TestProxiedObjectPoolWithJdkProxy extends AbstractTestProxiedObjectPool { 23 | 24 | @Override 25 | protected ProxySource getProxySource(boolean unwrapInvocationTargetException) { 26 | // @formatter:off 27 | return JdkProxySource.builder() 28 | .setClassLoader(getClass().getClassLoader()) 29 | .setInterfaces(TestObject.class) 30 | .setUnwrapInvocationTargetException(unwrapInvocationTargetException) 31 | .get(); 32 | // @formatter:on 33 | } 34 | 35 | @Override 36 | protected Class getInvocationTargetExceptionType() { 37 | return UndeclaredThrowableException.class; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/resources/test1: -------------------------------------------------------------------------------- 1 | Hello world ! -------------------------------------------------------------------------------- /src/test/resources/test2: -------------------------------------------------------------------------------- 1 | Hello world ! --------------------------------------------------------------------------------