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 java.util.List;
20  
21  import org.apache.commons.pool2.KeyedObjectPool;
22  import org.apache.commons.pool2.UsageTracking;
23  
24  /**
25   * Create a new keyed object pool where the pooled objects are wrapped in
26   * proxies allowing better control of pooled objects and in particular the
27   * prevention of the continued use of an object by a client after that client
28   * returns the object to the pool.
29   *
30   * @param <K> type of the key
31   * @param <V> type of the pooled object
32   * @since 2.0
33   */
34  public class ProxiedKeyedObjectPool<K, V> implements KeyedObjectPool<K, V> {
35  
36      private final KeyedObjectPool<K, V> pool;
37      private final ProxySource<V> 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 ProxiedKeyedObjectPool(final KeyedObjectPool<K, V> pool,
46              final ProxySource<V> proxySource) {
47          this.pool = pool;
48          this.proxySource = proxySource;
49      }
50  
51      @Override
52      public void addObject(final K key) throws Exception {
53          pool.addObject(key);
54      }
55  
56      @SuppressWarnings("unchecked")
57      @Override
58      public V borrowObject(final K key) throws Exception {
59          UsageTracking<V> usageTracking = null;
60          if (pool instanceof UsageTracking) {
61              usageTracking = (UsageTracking<V>) pool;
62          }
63          return proxySource.createProxy(pool.borrowObject(key), usageTracking);
64      }
65  
66      @Override
67      public void clear() throws Exception {
68          pool.clear();
69      }
70  
71      @Override
72      public void clear(final K key) throws Exception {
73          pool.clear(key);
74      }
75  
76      @Override
77      public void close() {
78          pool.close();
79      }
80  
81      /**
82       * Gets a copy of the pool key list.
83       *
84       * @return a copy of the pool key list.
85       * @since 2.12.0
86       */
87      @Override
88      public List<K> getKeys() {
89          return pool.getKeys();
90      }
91  
92      @Override
93      public int getNumActive() {
94          return pool.getNumActive();
95      }
96  
97      @Override
98      public int getNumActive(final K key) {
99          return pool.getNumActive(key);
100     }
101 
102     @Override
103     public int getNumIdle() {
104         return pool.getNumIdle();
105     }
106 
107     @Override
108     public int getNumIdle(final K key) {
109         return pool.getNumIdle(key);
110     }
111 
112     @Override
113     public void invalidateObject(final K key, final V proxy) throws Exception {
114         pool.invalidateObject(key, proxySource.resolveProxy(proxy));
115     }
116 
117     @Override
118     public void returnObject(final K key, final V proxy) throws Exception {
119         pool.returnObject(key, proxySource.resolveProxy(proxy));
120     }
121 
122     /**
123      * @since 2.4.3
124      */
125     @Override
126     public String toString() {
127         final StringBuilder builder = new StringBuilder();
128         builder.append("ProxiedKeyedObjectPool [pool=");
129         builder.append(pool);
130         builder.append(", proxySource=");
131         builder.append(proxySource);
132         builder.append("]");
133         return builder.toString();
134     }
135 }