Source code

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