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