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     * Create 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    @SuppressWarnings("unchecked")
055    @Override
056    public V borrowObject(final K key) throws Exception, NoSuchElementException,
057            IllegalStateException {
058        UsageTracking<V> usageTracking = null;
059        if (pool instanceof UsageTracking) {
060            usageTracking = (UsageTracking<V>) pool;
061        }
062        final V pooledObject = pool.borrowObject(key);
063        final V proxy = proxySource.createProxy(pooledObject, usageTracking);
064        return proxy;
065    }
066
067    @Override
068    public void returnObject(final K key, final V proxy) throws Exception {
069        final V pooledObject = proxySource.resolveProxy(proxy);
070        pool.returnObject(key, pooledObject);
071    }
072
073    @Override
074    public void invalidateObject(final K key, final V proxy) throws Exception {
075        final V pooledObject = proxySource.resolveProxy(proxy);
076        pool.invalidateObject(key, pooledObject);
077    }
078
079    @Override
080    public void addObject(final K key) throws Exception, IllegalStateException,
081            UnsupportedOperationException {
082        pool.addObject(key);
083    }
084
085    @Override
086    public int getNumIdle(final K key) {
087        return pool.getNumIdle(key);
088    }
089
090    @Override
091    public int getNumActive(final K key) {
092        return pool.getNumActive(key);
093    }
094
095    @Override
096    public int getNumIdle() {
097        return pool.getNumIdle();
098    }
099
100    @Override
101    public int getNumActive() {
102        return pool.getNumActive();
103    }
104
105    @Override
106    public void clear() throws Exception, UnsupportedOperationException {
107        pool.clear();
108    }
109
110    @Override
111    public void clear(final K key) throws Exception, UnsupportedOperationException {
112        pool.clear(key);
113    }
114
115    @Override
116    public void close() {
117        pool.close();
118    }
119
120
121    /**
122     * @since 2.4.3
123     */
124    @Override
125    public String toString() {
126        final StringBuilder builder = new StringBuilder();
127        builder.append("ProxiedKeyedObjectPool [pool=");
128        builder.append(pool);
129        builder.append(", proxySource=");
130        builder.append(proxySource);
131        builder.append("]");
132        return builder.toString();
133    }
134}