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.ObjectPool;
022import org.apache.commons.pool2.UsageTracking;
023
024/**
025 * Create a new object pool where the pooled objects are wrapped in proxies
026 * allowing better control of pooled objects and in particular the prevention
027 * of the continued use of an object by a client after that client returns the
028 * object to the pool.
029 *
030 * @param <T> type of the pooled object
031 *
032 * @since 2.0
033 */
034public class ProxiedObjectPool<T> implements ObjectPool<T> {
035
036    private final ObjectPool<T> pool;
037    private final ProxySource<T> proxySource;
038
039
040    /**
041     * Create a new proxied object pool.
042     *
043     * @param pool  The object pool to wrap
044     * @param proxySource The source of the proxy objects
045     */
046    public ProxiedObjectPool(final ObjectPool<T> pool, final ProxySource<T> proxySource) {
047        this.pool = pool;
048        this.proxySource = proxySource;
049    }
050
051
052    // --------------------------------------------------- ObjectPool<T> methods
053
054    @Override
055    public void addObject() throws Exception, IllegalStateException,
056            UnsupportedOperationException {
057        pool.addObject();
058    }
059
060
061    @SuppressWarnings("unchecked")
062    @Override
063    public T borrowObject() throws Exception, NoSuchElementException,
064            IllegalStateException {
065        UsageTracking<T> usageTracking = null;
066        if (pool instanceof UsageTracking) {
067            usageTracking = (UsageTracking<T>) pool;
068        }
069        final T pooledObject = pool.borrowObject();
070        return proxySource.createProxy(pooledObject, usageTracking);
071    }
072
073
074    @Override
075    public void clear() throws Exception, UnsupportedOperationException {
076        pool.clear();
077    }
078
079
080    @Override
081    public void close() {
082        pool.close();
083    }
084
085
086    @Override
087    public int getNumActive() {
088        return pool.getNumActive();
089    }
090
091
092    @Override
093    public int getNumIdle() {
094        return pool.getNumIdle();
095    }
096
097
098    @Override
099    public void invalidateObject(final T proxy) throws Exception {
100        final T pooledObject = proxySource.resolveProxy(proxy);
101        pool.invalidateObject(pooledObject);
102    }
103
104
105    @Override
106    public void returnObject(final T proxy) throws Exception {
107        final T pooledObject = proxySource.resolveProxy(proxy);
108        pool.returnObject(pooledObject);
109    }
110
111
112    /**
113     * @since 2.4.3
114     */
115    @Override
116    public String toString() {
117        final StringBuilder builder = new StringBuilder();
118        builder.append("ProxiedObjectPool [pool=");
119        builder.append(pool);
120        builder.append(", proxySource=");
121        builder.append(proxySource);
122        builder.append("]");
123        return builder.toString();
124    }
125}