ProxiedObjectPool.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 org.apache.commons.pool2.ObjectPool;
  19. import org.apache.commons.pool2.UsageTracking;

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

  30.     private final ObjectPool<T> pool;
  31.     private final ProxySource<T> proxySource;

  32.     /**
  33.      * Constructs a new proxied object pool.
  34.      *
  35.      * @param pool  The object pool to wrap
  36.      * @param proxySource The source of the proxy objects
  37.      */
  38.     public ProxiedObjectPool(final ObjectPool<T> pool, final ProxySource<T> proxySource) {
  39.         this.pool = pool;
  40.         this.proxySource = proxySource;
  41.     }

  42.     @Override
  43.     public void addObject() throws Exception {
  44.         pool.addObject();
  45.     }

  46.     @SuppressWarnings("unchecked")
  47.     @Override
  48.     public T borrowObject() throws Exception {
  49.         UsageTracking<T> usageTracking = null;
  50.         if (pool instanceof UsageTracking) {
  51.             usageTracking = (UsageTracking<T>) pool;
  52.         }
  53.         return proxySource.createProxy(pool.borrowObject(), usageTracking);
  54.     }

  55.     @Override
  56.     public void clear() throws Exception {
  57.         pool.clear();
  58.     }

  59.     @Override
  60.     public void close() {
  61.         pool.close();
  62.     }

  63.     @Override
  64.     public int getNumActive() {
  65.         return pool.getNumActive();
  66.     }

  67.     @Override
  68.     public int getNumIdle() {
  69.         return pool.getNumIdle();
  70.     }

  71.     @Override
  72.     public void invalidateObject(final T proxy) throws Exception {
  73.         pool.invalidateObject(proxySource.resolveProxy(proxy));
  74.     }

  75.     @Override
  76.     public void returnObject(final T proxy) throws Exception {
  77.         pool.returnObject(proxySource.resolveProxy(proxy));
  78.     }

  79.     /**
  80.      * @since 2.4.3
  81.      */
  82.     @Override
  83.     public String toString() {
  84.         final StringBuilder builder = new StringBuilder();
  85.         builder.append("ProxiedObjectPool [pool=");
  86.         builder.append(pool);
  87.         builder.append(", proxySource=");
  88.         builder.append(proxySource);
  89.         builder.append("]");
  90.         return builder.toString();
  91.     }
  92. }