View Javadoc
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    *      http://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.pool2.proxy;
18  
19  import org.apache.commons.pool2.ObjectPool;
20  import org.apache.commons.pool2.UsageTracking;
21  
22  /**
23   * Create a new object pool where the pooled objects are wrapped in proxies
24   * allowing better control of pooled objects and in particular the prevention
25   * of the continued use of an object by a client after that client returns the
26   * object to the pool.
27   *
28   * @param <T> type of the pooled object
29   * @since 2.0
30   */
31  public class ProxiedObjectPool<T> implements ObjectPool<T> {
32  
33      private final ObjectPool<T> pool;
34      private final ProxySource<T> proxySource;
35  
36      /**
37       * Constructs a new proxied object pool.
38       *
39       * @param pool  The object pool to wrap
40       * @param proxySource The source of the proxy objects
41       */
42      public ProxiedObjectPool(final ObjectPool<T> pool, final ProxySource<T> proxySource) {
43          this.pool = pool;
44          this.proxySource = proxySource;
45      }
46  
47      @Override
48      public void addObject() throws Exception {
49          pool.addObject();
50      }
51  
52      @SuppressWarnings("unchecked")
53      @Override
54      public T borrowObject() throws Exception {
55          UsageTracking<T> usageTracking = null;
56          if (pool instanceof UsageTracking) {
57              usageTracking = (UsageTracking<T>) pool;
58          }
59          return proxySource.createProxy(pool.borrowObject(), usageTracking);
60      }
61  
62      @Override
63      public void clear() throws Exception {
64          pool.clear();
65      }
66  
67      @Override
68      public void close() {
69          pool.close();
70      }
71  
72      @Override
73      public int getNumActive() {
74          return pool.getNumActive();
75      }
76  
77      @Override
78      public int getNumIdle() {
79          return pool.getNumIdle();
80      }
81  
82      @Override
83      public void invalidateObject(final T proxy) throws Exception {
84          pool.invalidateObject(proxySource.resolveProxy(proxy));
85      }
86  
87      @Override
88      public void returnObject(final T proxy) throws Exception {
89          pool.returnObject(proxySource.resolveProxy(proxy));
90      }
91  
92      /**
93       * @since 2.4.3
94       */
95      @Override
96      public String toString() {
97          final StringBuilder builder = new StringBuilder();
98          builder.append("ProxiedObjectPool [pool=");
99          builder.append(pool);
100         builder.append(", proxySource=");
101         builder.append(proxySource);
102         builder.append("]");
103         return builder.toString();
104     }
105 }