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.  *      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. import java.util.List;

  19. import org.apache.commons.pool2.KeyedObjectPool;
  20. import org.apache.commons.pool2.UsageTracking;

  21. /**
  22.  * Create a new keyed object pool where the pooled objects are wrapped in
  23.  * proxies allowing better control of pooled objects and in particular the
  24.  * prevention of the continued use of an object by a client after that client
  25.  * returns the object to the pool.
  26.  *
  27.  * @param <K> type of the key
  28.  * @param <V> type of the pooled object
  29.  * @since 2.0
  30.  */
  31. public class ProxiedKeyedObjectPool<K, V> implements KeyedObjectPool<K, V> {

  32.     private final KeyedObjectPool<K, V> pool;
  33.     private final ProxySource<V> proxySource;

  34.     /**
  35.      * Constructs a new proxied object pool.
  36.      *
  37.      * @param pool  The object pool to wrap
  38.      * @param proxySource The source of the proxy objects
  39.      */
  40.     public ProxiedKeyedObjectPool(final KeyedObjectPool<K, V> pool,
  41.             final ProxySource<V> proxySource) {
  42.         this.pool = pool;
  43.         this.proxySource = proxySource;
  44.     }

  45.     @Override
  46.     public void addObject(final K key) throws Exception {
  47.         pool.addObject(key);
  48.     }

  49.     @SuppressWarnings("unchecked")
  50.     @Override
  51.     public V borrowObject(final K key) throws Exception {
  52.         UsageTracking<V> usageTracking = null;
  53.         if (pool instanceof UsageTracking) {
  54.             usageTracking = (UsageTracking<V>) pool;
  55.         }
  56.         return proxySource.createProxy(pool.borrowObject(key), usageTracking);
  57.     }

  58.     @Override
  59.     public void clear() throws Exception {
  60.         pool.clear();
  61.     }

  62.     @Override
  63.     public void clear(final K key) throws Exception {
  64.         pool.clear(key);
  65.     }

  66.     @Override
  67.     public void close() {
  68.         pool.close();
  69.     }

  70.     /**
  71.      * Gets a copy of the pool key list.
  72.      *
  73.      * @return a copy of the pool key list.
  74.      * @since 2.12.0
  75.      */
  76.     @Override
  77.     public List<K> getKeys() {
  78.         return pool.getKeys();
  79.     }

  80.     @Override
  81.     public int getNumActive() {
  82.         return pool.getNumActive();
  83.     }

  84.     @Override
  85.     public int getNumActive(final K key) {
  86.         return pool.getNumActive(key);
  87.     }

  88.     @Override
  89.     public int getNumIdle() {
  90.         return pool.getNumIdle();
  91.     }

  92.     @Override
  93.     public int getNumIdle(final K key) {
  94.         return pool.getNumIdle(key);
  95.     }

  96.     @Override
  97.     public void invalidateObject(final K key, final V proxy) throws Exception {
  98.         pool.invalidateObject(key, proxySource.resolveProxy(proxy));
  99.     }

  100.     @Override
  101.     public void returnObject(final K key, final V proxy) throws Exception {
  102.         pool.returnObject(key, proxySource.resolveProxy(proxy));
  103.     }

  104.     /**
  105.      * @since 2.4.3
  106.      */
  107.     @Override
  108.     public String toString() {
  109.         final StringBuilder builder = new StringBuilder();
  110.         builder.append("ProxiedKeyedObjectPool [pool=");
  111.         builder.append(pool);
  112.         builder.append(", proxySource=");
  113.         builder.append(proxySource);
  114.         builder.append("]");
  115.         return builder.toString();
  116.     }
  117. }