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.List;
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 {
056        pool.addObject(key);
057    }
058
059    @SuppressWarnings("unchecked")
060    @Override
061    public V borrowObject(final K key) throws Exception {
062        UsageTracking<V> usageTracking = null;
063        if (pool instanceof UsageTracking) {
064            usageTracking = (UsageTracking<V>) pool;
065        }
066        return proxySource.createProxy(pool.borrowObject(key), usageTracking);
067    }
068
069    @Override
070    public void clear() throws Exception {
071        pool.clear();
072    }
073
074    @Override
075    public void clear(final K key) throws Exception {
076        pool.clear(key);
077    }
078
079    @Override
080    public void close() {
081        pool.close();
082    }
083
084    /**
085     * Gets a copy of the pool key list.
086     *
087     * @return a copy of the pool key list.
088     * @since 2.12.0
089     */
090    @Override
091    public List<K> getKeys() {
092        return pool.getKeys();
093    }
094
095    @Override
096    public int getNumActive() {
097        return pool.getNumActive();
098    }
099
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}