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