    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.pool2.proxy;
    018
    019import java.util.NoSuchElementException;
    020
    021import org.apache.commons.pool2.KeyedObjectPool;
    022import org.apache.commons.pool2.UsageTracking;
    023
    024/**
    025 * Create a new keyed object pool where the pooled objects are wrapped in
    026 * proxies allowing better control of pooled objects and in particular the
    027 * prevention of the continued use of an object by a client after that client
    028 * returns the object to the pool.
    029 *
    030 * @param <K> type of the key
    031 * @param <V> type of the pooled object
    032 *
    033 * @since 2.0
    034 */
    035public class ProxiedKeyedObjectPool<K, V> implements KeyedObjectPool<K, V> {
    036
    037    private final KeyedObjectPool<K, V> pool;
    038    private final ProxySource<V> proxySource;
    039
    040
    041    /**
    042     * Constructs a new proxied object pool.
    043     *
    044     * @param pool  The object pool to wrap
    045     * @param proxySource The source of the proxy objects
    046     */
    047    public ProxiedKeyedObjectPool(final KeyedObjectPool<K, V> pool,
    048            final ProxySource<V> proxySource) {
    049        this.pool = pool;
    050        this.proxySource = proxySource;
    051    }
    052
    053
    054    @Override
    055    public void addObject(final K key) throws Exception, IllegalStateException,
    056            UnsupportedOperationException {
    057        pool.addObject(key);
    058    }
    059
    060    @SuppressWarnings("unchecked")
    061    @Override
    062    public V borrowObject(final K key) throws Exception, NoSuchElementException,
    063            IllegalStateException {
    064        UsageTracking<V> usageTracking = null;
    065        if (pool instanceof UsageTracking) {
    066            usageTracking = (UsageTracking<V>) pool;
    067        }
    068        return proxySource.createProxy(pool.borrowObject(key), usageTracking);
    069    }
    070
    071    @Override
    072    public void clear() throws Exception, UnsupportedOperationException {
    073        pool.clear();
    074    }
    075
    076    @Override
    077    public void clear(final K key) throws Exception, UnsupportedOperationException {
    078        pool.clear(key);
    079    }
    080
    081    @Override
    082    public void close() {
    083        pool.close();
    084    }
    085
    086    @Override
    087    public int getNumActive() {
    088        return pool.getNumActive();
    089    }
    090
    091    @Override
    092    public int getNumActive(final K key) {
    093        return pool.getNumActive(key);
    094    }
    095
    096    @Override
    097    public int getNumIdle() {
    098        return pool.getNumIdle();
    099    }
    100
    101    @Override
    102    public int getNumIdle(final K key) {
    103        return pool.getNumIdle(key);
    104    }
    105
    106    @Override
    107    public void invalidateObject(final K key, final V proxy) throws Exception {
    108        pool.invalidateObject(key, proxySource.resolveProxy(proxy));
    109    }
    110
    111    @Override
    112    public void returnObject(final K key, final V proxy) throws Exception {
    113        pool.returnObject(key, proxySource.resolveProxy(proxy));
    114    }
    115
    116
    117    /**
    118     * @since 2.4.3
    119     */
    120    @Override
    121    public String toString() {
    122        final StringBuilder builder = new StringBuilder();
    123        builder.append("ProxiedKeyedObjectPool [pool=");
    124        builder.append(pool);
    125        builder.append(", proxySource=");
    126        builder.append(proxySource);
    127        builder.append("]");
    128        return builder.toString();
    129    }
    130}