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   *
33   * @since 2.0
34   */
35  public class ProxiedKeyedObjectPool<K, V> implements KeyedObjectPool<K, V> {
36  
37      private final KeyedObjectPool<K, V> pool;
38      private final ProxySource<V> proxySource;
39  
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<K, V> pool,
48              final ProxySource<V> proxySource) {
49          this.pool = pool;
50          this.proxySource = proxySource;
51      }
52  
53  
54      @Override
55      public void addObject(final K key) throws Exception {
56          pool.addObject(key);
57      }
58  
59      @SuppressWarnings("unchecked")
60      @Override
61      public V borrowObject(final K key) throws Exception {
62          UsageTracking<V> usageTracking = null;
63          if (pool instanceof UsageTracking) {
64              usageTracking = (UsageTracking<V>) pool;
65          }
66          return proxySource.createProxy(pool.borrowObject(key), usageTracking);
67      }
68  
69      @Override
70      public void clear() throws Exception {
71          pool.clear();
72      }
73  
74      @Override
75      public void clear(final K key) throws Exception {
76          pool.clear(key);
77      }
78  
79      @Override
80      public void close() {
81          pool.close();
82      }
83  
84      /**
85       * Gets a copy of the pool key list.
86       *
87       * @return a copy of the pool key list.
88       * @since 2.12.0
89       */
90      @Override
91      public List<K> getKeys() {
92          return pool.getKeys();
93      }
94  
95      @Override
96      public int getNumActive() {
97          return pool.getNumActive();
98      }
99  
100     @Override
101     public int getNumActive(final K key) {
102         return pool.getNumActive(key);
103     }
104 
105     @Override
106     public int getNumIdle() {
107         return pool.getNumIdle();
108     }
109 
110     @Override
111     public int getNumIdle(final K key) {
112         return pool.getNumIdle(key);
113     }
114 
115     @Override
116     public void invalidateObject(final K key, final V proxy) throws Exception {
117         pool.invalidateObject(key, proxySource.resolveProxy(proxy));
118     }
119 
120     @Override
121     public void returnObject(final K key, final V proxy) throws Exception {
122         pool.returnObject(key, proxySource.resolveProxy(proxy));
123     }
124 
125     /**
126      * @since 2.4.3
127      */
128     @Override
129     public String toString() {
130         final StringBuilder builder = new StringBuilder();
131         builder.append("ProxiedKeyedObjectPool [pool=");
132         builder.append(pool);
133         builder.append(", proxySource=");
134         builder.append(proxySource);
135         builder.append("]");
136         return builder.toString();
137     }
138 }