    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 *      https://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 * @since 2.0
    030 */
    031public class ProxiedObjectPool<T> implements ObjectPool<T> {
    032
    033    private final ObjectPool<T> pool;
    034    private final ProxySource<T> proxySource;
    035
    036    /**
    037     * Constructs a new proxied object pool.
    038     *
    039     * @param pool  The object pool to wrap
    040     * @param proxySource The source of the proxy objects
    041     */
    042    public ProxiedObjectPool(final ObjectPool<T> pool, final ProxySource<T> proxySource) {
    043        this.pool = pool;
    044        this.proxySource = proxySource;
    045    }
    046
    047    @Override
    048    public void addObject() throws Exception {
    049        pool.addObject();
    050    }
    051
    052    @SuppressWarnings("unchecked")
    053    @Override
    054    public T borrowObject() throws Exception {
    055        UsageTracking<T> usageTracking = null;
    056        if (pool instanceof UsageTracking) {
    057            usageTracking = (UsageTracking<T>) pool;
    058        }
    059        return proxySource.createProxy(pool.borrowObject(), usageTracking);
    060    }
    061
    062    @Override
    063    public void clear() throws Exception {
    064        pool.clear();
    065    }
    066
    067    @Override
    068    public void close() {
    069        pool.close();
    070    }
    071
    072    @Override
    073    public int getNumActive() {
    074        return pool.getNumActive();
    075    }
    076
    077    @Override
    078    public int getNumIdle() {
    079        return pool.getNumIdle();
    080    }
    081
    082    @Override
    083    public void invalidateObject(final T proxy) throws Exception {
    084        pool.invalidateObject(proxySource.resolveProxy(proxy));
    085    }
    086
    087    @Override
    088    public void returnObject(final T proxy) throws Exception {
    089        pool.returnObject(proxySource.resolveProxy(proxy));
    090    }
    091
    092    /**
    093     * @since 2.4.3
    094     */
    095    @Override
    096    public String toString() {
    097        final StringBuilder builder = new StringBuilder();
    098        builder.append("ProxiedObjectPool [pool=");
    099        builder.append(pool);
    100        builder.append(", proxySource=");
    101        builder.append(proxySource);
    102        builder.append("]");
    103        return builder.toString();
    104    }
    105}