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