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   *
30   * @since 2.0
31   */
32  public class ProxiedObjectPool<T> implements ObjectPool<T> {
33  
34      private final ObjectPool<T> pool;
35      private final ProxySource<T> proxySource;
36  
37  
38      /**
39       * Constructs a new proxied object pool.
40       *
41       * @param pool  The object pool to wrap
42       * @param proxySource The source of the proxy objects
43       */
44      public ProxiedObjectPool(final ObjectPool<T> pool, final ProxySource<T> proxySource) {
45          this.pool = pool;
46          this.proxySource = proxySource;
47      }
48  
49      @Override
50      public void addObject() throws Exception {
51          pool.addObject();
52      }
53  
54  
55      @SuppressWarnings("unchecked")
56      @Override
57      public T borrowObject() throws Exception {
58          UsageTracking<T> usageTracking = null;
59          if (pool instanceof UsageTracking) {
60              usageTracking = (UsageTracking<T>) pool;
61          }
62          return proxySource.createProxy(pool.borrowObject(), usageTracking);
63      }
64  
65  
66      @Override
67      public void clear() throws Exception {
68          pool.clear();
69      }
70  
71  
72      @Override
73      public void close() {
74          pool.close();
75      }
76  
77  
78      @Override
79      public int getNumActive() {
80          return pool.getNumActive();
81      }
82  
83  
84      @Override
85      public int getNumIdle() {
86          return pool.getNumIdle();
87      }
88  
89  
90      @Override
91      public void invalidateObject(final T proxy) throws Exception {
92          pool.invalidateObject(proxySource.resolveProxy(proxy));
93      }
94  
95  
96      @Override
97      public void returnObject(final T proxy) throws Exception {
98          pool.returnObject(proxySource.resolveProxy(proxy));
99      }
100 
101     /**
102      * @since 2.4.3
103      */
104     @Override
105     public String toString() {
106         final StringBuilder builder = new StringBuilder();
107         builder.append("ProxiedObjectPool [pool=");
108         builder.append(pool);
109         builder.append(", proxySource=");
110         builder.append(proxySource);
111         builder.append("]");
112         return builder.toString();
113     }
114 }