You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2011/05/12 12:27:43 UTC
svn commit: r1102233 - in /commons/proper/pool/trunk/src:
java/org/apache/commons/pool2/PoolUtils.java
test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java
test/org/apache/commons/pool2/TestPoolUtils.java
Author: markt
Date: Thu May 12 10:27:42 2011
New Revision: 1102233
URL: http://svn.apache.org/viewvc?rev=1102233&view=rev
Log:
Port clean-up from POOL_FUTURE
http://svn.apache.org/viewvc?view=revision&revision=105355
Remove the following from PoolUtils
* PoolableObjectFactoryAdaptor
* KeyedPoolableObjectFactoryAdaptor
* ObjectPoolAdaptor
* KeyedObjectPoolAdaptor
* CheckedObjectPool
* CheckedKeyedObjectPool
Modified:
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/PoolUtils.java
commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java
commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestPoolUtils.java
Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/PoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/PoolUtils.java?rev=1102233&r1=1102232&r2=1102233&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/PoolUtils.java (original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/PoolUtils.java Thu May 12 10:27:42 2011
@@ -71,129 +71,6 @@ public final class PoolUtils {
}
/**
- * Adapt a <code>KeyedPoolableObjectFactory</code> instance to work where a <code>PoolableObjectFactory</code> is
- * needed. This method is the equivalent of calling
- * {@link #adapt(KeyedPoolableObjectFactory, Object) PoolUtils.adapt(aKeyedPoolableObjectFactory, new Object())}.
- *
- * @param keyedFactory the {@link KeyedPoolableObjectFactory} to delegate to.
- * @return a {@link PoolableObjectFactory} that delegates to <code>keyedFactory</code> with an internal key.
- * @throws IllegalArgumentException when <code>keyedFactory</code> is <code>null</code>.
- * @see #adapt(KeyedPoolableObjectFactory, Object)
- * @since Pool 1.3
- */
- public static <V> PoolableObjectFactory<V> adapt(final KeyedPoolableObjectFactory<Object,V> keyedFactory) throws IllegalArgumentException {
- return adapt(keyedFactory, new Object());
- }
-
- /**
- * Adapt a <code>KeyedPoolableObjectFactory</code> instance to work where a <code>PoolableObjectFactory</code> is
- * needed using the specified <code>key</code> when delegating.
- *
- * @param keyedFactory the {@link KeyedPoolableObjectFactory} to delegate to.
- * @param key the key to use when delegating.
- * @return a {@link PoolableObjectFactory} that delegates to <code>keyedFactory</code> with the specified key.
- * @throws IllegalArgumentException when <code>keyedFactory</code> or <code>key</code> is <code>null</code>.
- * @see #adapt(KeyedPoolableObjectFactory)
- * @since Pool 1.3
- */
- public static <K,V> PoolableObjectFactory<V> adapt(final KeyedPoolableObjectFactory<K,V> keyedFactory, final K key) throws IllegalArgumentException {
- return new PoolableObjectFactoryAdaptor<K,V>(keyedFactory, key);
- }
-
- /**
- * Adapt a <code>PoolableObjectFactory</code> instance to work where a <code>KeyedPoolableObjectFactory</code> is
- * needed. The key is ignored.
- *
- * @param factory the {@link PoolableObjectFactory} to delegate to.
- * @return a {@link KeyedPoolableObjectFactory} that delegates to <code>factory</code> ignoring the key.
- * @throws IllegalArgumentException when <code>factory</code> is <code>null</code>.
- * @since Pool 1.3
- */
- public static <K,V> KeyedPoolableObjectFactory<K,V> adapt(final PoolableObjectFactory<V> factory) throws IllegalArgumentException {
- return new KeyedPoolableObjectFactoryAdaptor<K,V>(factory);
- }
-
- /**
- * Adapt a <code>KeyedObjectPool</code> instance to work where an <code>ObjectPool</code> is needed. This is the
- * equivalent of calling {@link #adapt(KeyedObjectPool, Object) PoolUtils.adapt(aKeyedObjectPool, new Object())}.
- *
- * @param keyedPool the {@link KeyedObjectPool} to delegate to.
- * @return an {@link ObjectPool} that delegates to <code>keyedPool</code> with an internal key.
- * @throws IllegalArgumentException when <code>keyedPool</code> is <code>null</code>.
- * @see #adapt(KeyedObjectPool, Object)
- * @since Pool 1.3
- */
- public static <V> ObjectPool<V> adapt(final KeyedObjectPool<Object,V> keyedPool) throws IllegalArgumentException {
- return adapt(keyedPool, new Object());
- }
-
- /**
- * Adapt a <code>KeyedObjectPool</code> instance to work where an <code>ObjectPool</code> is needed using the
- * specified <code>key</code> when delegating.
- *
- * @param keyedPool the {@link KeyedObjectPool} to delegate to.
- * @param key the key to use when delegating.
- * @return an {@link ObjectPool} that delegates to <code>keyedPool</code> with the specified key.
- * @throws IllegalArgumentException when <code>keyedPool</code> or <code>key</code> is <code>null</code>.
- * @see #adapt(KeyedObjectPool)
- * @since Pool 1.3
- */
- public static <K,V> ObjectPool<V> adapt(final KeyedObjectPool<K,V> keyedPool, final K key) throws IllegalArgumentException {
- return new ObjectPoolAdaptor<K,V>(keyedPool, key);
- }
-
- /**
- * Adapt an <code>ObjectPool</code> to work where an <code>KeyedObjectPool</code> is needed.
- * The key is ignored.
- *
- * @param pool the {@link ObjectPool} to delegate to.
- * @return a {@link KeyedObjectPool} that delegates to <code>pool</code> ignoring the key.
- * @throws IllegalArgumentException when <code>pool</code> is <code>null</code>.
- * @since Pool 1.3
- */
- public static <K,V> KeyedObjectPool<K,V> adapt(final ObjectPool<V> pool) throws IllegalArgumentException {
- return new KeyedObjectPoolAdaptor<K,V>(pool);
- }
-
- /**
- * Wraps an <code>ObjectPool</code> and dynamically checks the type of objects borrowed and returned to the pool.
- * If an object is passed to the pool that isn't of type <code>type</code> a {@link ClassCastException} will be thrown.
- *
- * @param pool the pool to enforce type safety on
- * @param type the class type to enforce.
- * @return an <code>ObjectPool</code> that will only allow objects of <code>type</code>
- * @since Pool 1.3
- */
- public static <T> ObjectPool<T> checkedPool(final ObjectPool<T> pool, final Class<? super T> type) {
- if (pool == null) {
- throw new IllegalArgumentException("pool must not be null.");
- }
- if (type == null) {
- throw new IllegalArgumentException("type must not be null.");
- }
- return new CheckedObjectPool<T>(pool, type);
- }
-
- /**
- * Wraps a <code>KeyedObjectPool</code> and dynamically checks the type of objects borrowed and returned to the keyedPool.
- * If an object is passed to the keyedPool that isn't of type <code>type</code> a {@link ClassCastException} will be thrown.
- *
- * @param keyedPool the keyedPool to enforce type safety on
- * @param type the class type to enforce.
- * @return a <code>KeyedObjectPool</code> that will only allow objects of <code>type</code>
- * @since Pool 1.3
- */
- public static <K,V> KeyedObjectPool<K,V> checkedPool(final KeyedObjectPool<K,V> keyedPool, final Class<? super V> type) {
- if (keyedPool == null) {
- throw new IllegalArgumentException("keyedPool must not be null.");
- }
- if (type == null) {
- throw new IllegalArgumentException("type must not be null.");
- }
- return new CheckedKeyedObjectPool<K,V>(keyedPool, type);
- }
-
- /**
* Periodically check the idle object count for the pool. At most one idle object will be added per period.
* If there is an exception when calling {@link ObjectPool#addObject()} then no more checks will be performed.
*
@@ -570,779 +447,6 @@ public final class PoolUtils {
}
/**
- * Adaptor class that wraps and converts a KeyedPoolableObjectFactory with a fixed
- * key to a PoolableObjectFactory.
- */
- private static class PoolableObjectFactoryAdaptor<K,V> implements PoolableObjectFactory<V> {
- /** Fixed key */
- private final K key;
-
- /** Wrapped factory */
- private final KeyedPoolableObjectFactory<K,V> keyedFactory;
-
- /**
- * Create a PoolableObjectFactoryAdaptor wrapping the provided KeyedPoolableObjectFactory with the
- * given fixed key.
- *
- * @param keyedFactory KeyedPoolableObjectFactory that will manage objects
- * @param key fixed key
- * @throws IllegalArgumentException if either of the parameters is null
- */
- PoolableObjectFactoryAdaptor(final KeyedPoolableObjectFactory<K,V> keyedFactory, final K key)
- throws IllegalArgumentException {
- if (keyedFactory == null) {
- throw new IllegalArgumentException("keyedFactory must not be null.");
- }
- if (key == null) {
- throw new IllegalArgumentException("key must not be null.");
- }
- this.keyedFactory = keyedFactory;
- this.key = key;
- }
-
- /**
- * Create an object instance using the configured factory and key.
- *
- * @return new object instance
- */
- public V makeObject() throws Exception {
- return keyedFactory.makeObject(key);
- }
-
- /**
- * Destroy the object, passing the fixed key to the factory.
- *
- * @param obj object to destroy
- */
- public void destroyObject(final V obj) throws Exception {
- keyedFactory.destroyObject(key, obj);
- }
-
- /**
- * Validate the object, passing the fixed key to the factory.
- *
- * @param obj object to validate
- * @return true if validation is successful
- */
- public boolean validateObject(final V obj) {
- return keyedFactory.validateObject(key, obj);
- }
-
- /**
- * Activate the object, passing the fixed key to the factory.
- *
- * @param obj object to activate
- */
- public void activateObject(final V obj) throws Exception {
- keyedFactory.activateObject(key, obj);
- }
-
- /**
- * Passivate the object, passing the fixed key to the factory.
- *
- * @param obj object to passivate
- */
- public void passivateObject(final V obj) throws Exception {
- keyedFactory.passivateObject(key, obj);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("PoolableObjectFactoryAdaptor");
- sb.append("{key=").append(key);
- sb.append(", keyedFactory=").append(keyedFactory);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
- * Adaptor class that turns a PoolableObjectFactory into a KeyedPoolableObjectFactory by
- * ignoring keys.
- */
- private static class KeyedPoolableObjectFactoryAdaptor<K,V> implements KeyedPoolableObjectFactory<K,V> {
-
- /** Underlying PoolableObjectFactory */
- private final PoolableObjectFactory<V> factory;
-
- /**
- * Create a new KeyedPoolableObjectFactoryAdaptor using the given PoolableObjectFactory to
- * manage objects.
- *
- * @param factory wrapped PoolableObjectFactory
- * @throws IllegalArgumentException if the factory is null
- */
- KeyedPoolableObjectFactoryAdaptor(final PoolableObjectFactory<V> factory) throws IllegalArgumentException {
- if (factory == null) {
- throw new IllegalArgumentException("factory must not be null.");
- }
- this.factory = factory;
- }
-
- /**
- * Create a new object instance, ignoring the key
- *
- * @param key ignored
- * @return newly created object instance
- */
- public V makeObject(final K key) throws Exception {
- return factory.makeObject();
- }
-
- /**
- * Destroy the object, ignoring the key.
- *
- * @param key ignored
- * @param obj instance to destroy
- */
- public void destroyObject(final K key, final V obj) throws Exception {
- factory.destroyObject(obj);
- }
-
- /**
- * Validate the object, ignoring the key
- *
- * @param key ignored
- * @param obj object to validate
- * @return true if validation is successful
- */
- public boolean validateObject(final K key, final V obj) {
- return factory.validateObject(obj);
- }
-
- /**
- * Activate the object, ignoring the key.
- *
- * @param key ignored
- * @param obj object to be activated
- */
- public void activateObject(final K key, final V obj) throws Exception {
- factory.activateObject(obj);
- }
-
- /**
- * Passivate the object, ignoring the key.
- *
- * @param key ignored
- * @param obj object to passivate
- */
- public void passivateObject(final K key, final V obj) throws Exception {
- factory.passivateObject(obj);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("KeyedPoolableObjectFactoryAdaptor");
- sb.append("{factory=").append(factory);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
- * Adapts a KeyedObjectPool to make it an ObjectPool by fixing restricting to
- * a fixed key.
- */
- private static class ObjectPoolAdaptor<K,V> implements ObjectPool<V> {
-
- /** Fixed key */
- private final K key;
-
- /** Underlying KeyedObjectPool */
- private final KeyedObjectPool<K,V> keyedPool;
-
- /**
- * Create a new ObjectPoolAdaptor using the provided KeyedObjectPool and fixed key.
- *
- * @param keyedPool underlying KeyedObjectPool
- * @param key fixed key
- * @throws IllegalArgumentException if either of the parameters is null
- */
- ObjectPoolAdaptor(final KeyedObjectPool<K,V> keyedPool, final K key) throws IllegalArgumentException {
- if (keyedPool == null) {
- throw new IllegalArgumentException("keyedPool must not be null.");
- }
- if (key == null) {
- throw new IllegalArgumentException("key must not be null.");
- }
- this.keyedPool = keyedPool;
- this.key = key;
- }
-
- /**
- * {@inheritDoc}
- */
- public V borrowObject() throws Exception, NoSuchElementException, IllegalStateException {
- return keyedPool.borrowObject(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public void returnObject(final V obj) {
- try {
- keyedPool.returnObject(key, obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void invalidateObject(final V obj) {
- try {
- keyedPool.invalidateObject(key, obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void addObject() throws Exception, IllegalStateException {
- keyedPool.addObject(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumIdle() throws UnsupportedOperationException {
- return keyedPool.getNumIdle(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumActive() throws UnsupportedOperationException {
- return keyedPool.getNumActive(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public void clear() throws Exception, UnsupportedOperationException {
- keyedPool.clear();
- }
-
- /**
- * {@inheritDoc}
- */
- public void close() {
- try {
- keyedPool.close();
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Sets the PoolableObjectFactory for the pool.
- *
- * @param factory new PoolableObjectFactory
- * @deprecated to be removed in version 2.0
- */
- @Deprecated
- public void setFactory(final PoolableObjectFactory<V> factory) throws IllegalStateException, UnsupportedOperationException {
- keyedPool.setFactory(new KeyedPoolableObjectFactoryAdaptor<K,V>(factory));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("ObjectPoolAdaptor");
- sb.append("{key=").append(key);
- sb.append(", keyedPool=").append(keyedPool);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
- * Adapts an ObjectPool to implement KeyedObjectPool by ignoring key arguments.
- */
- private static class KeyedObjectPoolAdaptor<K,V> implements KeyedObjectPool<K,V> {
-
- /** Underlying pool */
- private final ObjectPool<V> pool;
-
- /**
- * Create a new KeyedObjectPoolAdaptor wrapping the given ObjectPool
- *
- * @param pool underlying object pool
- * @throws IllegalArgumentException if pool is null
- */
- KeyedObjectPoolAdaptor(final ObjectPool<V> pool) throws IllegalArgumentException {
- if (pool == null) {
- throw new IllegalArgumentException("pool must not be null.");
- }
- this.pool = pool;
- }
-
- /**
- * Borrow and object from the pool, ignoring the key
- *
- * @param key ignored
- * @return newly created object instance
- */
- public V borrowObject(final K key) throws Exception, NoSuchElementException, IllegalStateException {
- return pool.borrowObject();
- }
-
- /**
- * Return and object to the pool, ignoring the key
- *
- * @param key ignored
- * @param obj object to return
- */
- public void returnObject(final K key, final V obj) {
- try {
- pool.returnObject(obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Invalidate and object, ignoring the key
- *
- * @param obj object to invalidate
- * @param key ignored
- */
- public void invalidateObject(final K key, final V obj) {
- try {
- pool.invalidateObject(obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Add an object to the pool, ignoring the key
- *
- * @param key ignored
- */
- public void addObject(final K key) throws Exception, IllegalStateException {
- pool.addObject();
- }
-
- /**
- * Return the number of objects idle in the pool, ignoring the key.
- *
- * @param key ignored
- * @return idle instance count
- */
- public int getNumIdle(final K key) throws UnsupportedOperationException {
- return pool.getNumIdle();
- }
-
- /**
- * Return the number of objects checked out from the pool, ignoring the key.
- *
- * @param key ignored
- * @return active instance count
- */
- public int getNumActive(final K key) throws UnsupportedOperationException {
- return pool.getNumActive();
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumIdle() throws UnsupportedOperationException {
- return pool.getNumIdle();
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumActive() throws UnsupportedOperationException {
- return pool.getNumActive();
- }
-
- /**
- * {@inheritDoc}
- */
- public void clear() throws Exception, UnsupportedOperationException {
- pool.clear();
- }
-
- /**
- * Clear the pool, ignoring the key (has same effect as {@link #clear()}.
- *
- * @param key ignored.
- */
- public void clear(final K key) throws Exception, UnsupportedOperationException {
- pool.clear();
- }
-
- /**
- * {@inheritDoc}
- */
- public void close() {
- try {
- pool.close();
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Sets the factory used to manage objects.
- *
- * @param factory new factory to use managing object instances
- * @deprecated to be removed in version 2.0
- */
- @Deprecated
- public void setFactory(final KeyedPoolableObjectFactory<K,V> factory) throws IllegalStateException, UnsupportedOperationException {
- pool.setFactory(adapt((KeyedPoolableObjectFactory<Object, V>) factory));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("KeyedObjectPoolAdaptor");
- sb.append("{pool=").append(pool);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
- * An object pool that performs type checking on objects passed
- * to pool methods.
- *
- */
- private static class CheckedObjectPool<T> implements ObjectPool<T> {
- /**
- * Type of objects allowed in the pool. This should be a subtype of the return type of
- * the underlying pool's associated object factory.
- */
- private final Class<? super T> type;
-
- /** Underlying object pool */
- private final ObjectPool<T> pool;
-
- /**
- * Create a CheckedObjectPool accepting objects of the given type using
- * the given pool.
- *
- * @param pool underlying object pool
- * @param type expected pooled object type
- * @throws IllegalArgumentException if either parameter is null
- */
- CheckedObjectPool(final ObjectPool<T> pool, final Class<? super T> type) {
- if (pool == null) {
- throw new IllegalArgumentException("pool must not be null.");
- }
- if (type == null) {
- throw new IllegalArgumentException("type must not be null.");
- }
- this.pool = pool;
- this.type = type;
- }
-
- /**
- * Borrow an object from the pool, checking its type.
- *
- * @return a type-checked object from the pool
- * @throws ClassCastException if the object returned by the pool is not of the expected type
- */
- public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException {
- final T obj = pool.borrowObject();
- if (type.isInstance(obj)) {
- return obj;
- }
- throw new ClassCastException("Borrowed object is not of type: " + type.getName() + " was: " + obj);
- }
-
- /**
- * Return an object to the pool, verifying that it is of the correct type.
- *
- * @param obj object to return
- * @throws ClassCastException if obj is not of the expected type
- */
- public void returnObject(final T obj) {
- if (type.isInstance(obj)) {
- try {
- pool.returnObject(obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- } else {
- throw new ClassCastException("Returned object is not of type: " + type.getName() + " was: " + obj);
- }
- }
-
- /**
- * Invalidates an object from the pool, verifying that it is of the expected type.
- *
- * @param obj object to invalidate
- * @throws ClassCastException if obj is not of the expected type
- */
- public void invalidateObject(final T obj) {
- if (type.isInstance(obj)) {
- try {
- pool.invalidateObject(obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- } else {
- throw new ClassCastException("Invalidated object is not of type: " + type.getName() + " was: " + obj);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException {
- pool.addObject();
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumIdle() throws UnsupportedOperationException {
- return pool.getNumIdle();
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumActive() throws UnsupportedOperationException {
- return pool.getNumActive();
- }
-
- /**
- * {@inheritDoc}
- */
- public void clear() throws Exception, UnsupportedOperationException {
- pool.clear();
- }
-
- /**
- * {@inheritDoc}
- */
- public void close() {
- try {
- pool.close();
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Sets the object factory associated with the pool
- *
- * @param factory object factory
- * @deprecated to be removed in version 2.0
- */
- @Deprecated
- public void setFactory(final PoolableObjectFactory<T> factory) throws IllegalStateException, UnsupportedOperationException {
- pool.setFactory(factory);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("CheckedObjectPool");
- sb.append("{type=").append(type);
- sb.append(", pool=").append(pool);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
- * A keyed object pool that performs type checking on objects passed
- * to pool methods.
- *
- */
- private static class CheckedKeyedObjectPool<K,V> implements KeyedObjectPool<K,V> {
- /**
- * Expected type of objects managed by the pool. This should be
- * a subtype of the return type of the object factory used by the pool.
- */
- private final Class<? super V> type;
-
- /** Underlying pool */
- private final KeyedObjectPool<K,V> keyedPool;
-
- /**
- * Create a new CheckedKeyedObjectPool from the given pool with given expected object type.
- *
- * @param keyedPool underlying pool
- * @param type expected object type
- * @throws IllegalArgumentException if either parameter is null
- */
- CheckedKeyedObjectPool(final KeyedObjectPool<K,V> keyedPool, final Class<? super V> type) {
- if (keyedPool == null) {
- throw new IllegalArgumentException("keyedPool must not be null.");
- }
- if (type == null) {
- throw new IllegalArgumentException("type must not be null.");
- }
- this.keyedPool = keyedPool;
- this.type = type;
- }
-
- /**
- * Borrow an object from the pool, verifying correct return type.
- *
- * @param key pool key
- * @return type-checked object from the pool under the given key
- * @throws ClassCastException if the object returned by the pool is not of the expected type
- */
- public V borrowObject(final K key) throws Exception, NoSuchElementException, IllegalStateException {
- V obj = keyedPool.borrowObject(key);
- if (type.isInstance(obj)) {
- return obj;
- }
- throw new ClassCastException("Borrowed object for key: " + key + " is not of type: " + type.getName() + " was: " + obj);
- }
-
- /**
- * Return an object to the pool, checking its type.
- *
- * @param key the associated key (not type-checked)
- * @param obj the object to return (type-checked)
- * @throws ClassCastException if obj is not of the expected type
- */
- public void returnObject(final K key, final V obj) {
- if (type.isInstance(obj)) {
- try {
- keyedPool.returnObject(key, obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- } else {
- throw new ClassCastException("Returned object for key: " + key + " is not of type: " + type.getName() + " was: " + obj);
- }
- }
-
- /**
- * Invalidate an object to the pool, checking its type.
- *
- * @param key the associated key (not type-checked)
- * @param obj the object to return (type-checked)
- * @throws ClassCastException if obj is not of the expected type
- */
- public void invalidateObject(final K key, final V obj) {
- if (type.isInstance(obj)) {
- try {
- keyedPool.invalidateObject(key, obj);
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- } else {
- throw new ClassCastException("Invalidated object for key: " + key + " is not of type: " + type.getName() + " was: " + obj);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void addObject(final K key) throws Exception, IllegalStateException, UnsupportedOperationException {
- keyedPool.addObject(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumIdle(final K key) throws UnsupportedOperationException {
- return keyedPool.getNumIdle(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumActive(final K key) throws UnsupportedOperationException {
- return keyedPool.getNumActive(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumIdle() throws UnsupportedOperationException {
- return keyedPool.getNumIdle();
- }
-
- /**
- * {@inheritDoc}
- */
- public int getNumActive() throws UnsupportedOperationException {
- return keyedPool.getNumActive();
- }
-
- /**
- * {@inheritDoc}
- */
- public void clear() throws Exception, UnsupportedOperationException {
- keyedPool.clear();
- }
-
- /**
- * {@inheritDoc}
- */
- public void clear(final K key) throws Exception, UnsupportedOperationException {
- keyedPool.clear(key);
- }
-
- /**
- * {@inheritDoc}
- */
- public void close() {
- try {
- keyedPool.close();
- } catch (Exception e) {
- // swallowed as of Pool 2
- }
- }
-
- /**
- * Sets the object factory associated with the pool
- *
- * @param factory object factory
- * @deprecated to be removed in version 2.0
- */
- @Deprecated
- public void setFactory(final KeyedPoolableObjectFactory<K,V> factory) throws IllegalStateException, UnsupportedOperationException {
- keyedPool.setFactory(factory);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- final StringBuffer sb = new StringBuffer();
- sb.append("CheckedKeyedObjectPool");
- sb.append("{type=").append(type);
- sb.append(", keyedPool=").append(keyedPool);
- sb.append('}');
- return sb.toString();
- }
- }
-
- /**
* Timer task that adds objects to the pool until the number of idle
* instances reaches the configured minIdle. Note that this is not the
* same as the pool's minIdle setting.
Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java?rev=1102233&r1=1102232&r2=1102233&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java (original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPoolFactory.java Thu May 12 10:27:42 2011
@@ -40,7 +40,31 @@ public abstract class TestKeyedObjectPoo
protected abstract KeyedObjectPoolFactory<Object,Object> makeFactory(KeyedPoolableObjectFactory<Object,Object> objectFactory) throws UnsupportedOperationException;
protected static KeyedPoolableObjectFactory<Object,Object> createObjectFactory() {
- return PoolUtils.adapt(new MethodCallPoolableObjectFactory());
+ return new KeyedPoolableObjectFactory<Object, Object>() {
+
+ private final MethodCallPoolableObjectFactory wrapped = new MethodCallPoolableObjectFactory();
+
+ public Object makeObject(Object key) throws Exception {
+ return this.wrapped.makeObject();
+ }
+
+ public void destroyObject(Object key, Object obj) throws Exception {
+ this.wrapped.destroyObject(obj);
+ }
+
+ public boolean validateObject(Object key, Object obj) {
+ return this.wrapped.validateObject(obj);
+ }
+
+ public void activateObject(Object key, Object obj) throws Exception {
+ this.wrapped.activateObject(obj);
+ }
+
+ public void passivateObject(Object key, Object obj) throws Exception {
+ this.wrapped.passivateObject(obj);
+ }
+
+ };
}
@Test
Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestPoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestPoolUtils.java?rev=1102233&r1=1102232&r2=1102233&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestPoolUtils.java (original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestPoolUtils.java Thu May 12 10:27:42 2011
@@ -61,217 +61,6 @@ public class TestPoolUtils {
}
@Test
- public void testAdaptKeyedPoolableObjectFactory() throws Exception {
- try {
- PoolUtils.adapt((KeyedPoolableObjectFactory<Object,Object>)null);
- fail("PoolUtils.adapt(KeyedPoolableObjectFactory) must not allow null factory.");
- } catch (IllegalArgumentException iae) {
- // expected
- }
- }
-
- @Test
- public void testAdaptKeyedPoolableObjectFactoryKey() throws Exception {
- try {
- PoolUtils.adapt((KeyedPoolableObjectFactory<Object,Object>)null, new Object());
- fail("PoolUtils.adapt(KeyedPoolableObjectFactory, key) must not allow null factory.");
- } catch (IllegalArgumentException iae) {
- // expected
- }
- try {
- PoolUtils.adapt((KeyedPoolableObjectFactory<Object,Object>)createProxy(KeyedPoolableObjectFactory.class, (List<String>)null), null);
- fail("PoolUtils.adapt(KeyedPoolableObjectFactory, key) must not allow null key.");
- } catch (IllegalArgumentException iae) {
- // expected
- }
-
- final List<String> calledMethods = new ArrayList<String>();
- final KeyedPoolableObjectFactory<Object,Object> kpof =
- (KeyedPoolableObjectFactory<Object,Object>)createProxy(KeyedPoolableObjectFactory.class, calledMethods);
-
- final PoolableObjectFactory<Object> pof = PoolUtils.adapt(kpof);
- final List<String> expectedMethods = invokeEveryMethod(pof);
- assertEquals(expectedMethods, calledMethods);
- }
-
- @Test
- public void testAdaptPoolableObjectFactory() throws Exception {
- try {
- PoolUtils.adapt((PoolableObjectFactory<Object>)null);
- fail("PoolUtils.adapt(PoolableObjectFactory) must not allow null factory.");
- } catch (IllegalArgumentException iae) {
- // expected
- }
-
- final List<String> calledMethods = new ArrayList<String>();
- final PoolableObjectFactory<Object> pof =
- (PoolableObjectFactory<Object>)createProxy(PoolableObjectFactory.class, calledMethods);
-
- final KeyedPoolableObjectFactory<Object,Object> kpof = PoolUtils.adapt(pof);
- final List<String> expectedMethods = invokeEveryMethod(kpof);
- assertEquals(expectedMethods, calledMethods);
- }
-
- @Test
- public void testAdaptKeyedObjectPool() throws Exception {
- try {
- PoolUtils.adapt((KeyedObjectPool<Object,Object>)null);
- fail("PoolUtils.adapt(KeyedObjectPool) must not allow a null pool.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
- }
-
- @Test
- public void testAdaptKeyedObjectPoolKey() throws Exception {
- try {
- PoolUtils.adapt((KeyedObjectPool<Object,Object>)null, new Object());
- fail("PoolUtils.adapt(KeyedObjectPool, key) must not allow a null pool.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
- try {
- PoolUtils.adapt((KeyedObjectPool<Object,Object>)createProxy(KeyedObjectPool.class, (List<String>)null), null);
- fail("PoolUtils.adapt(KeyedObjectPool, key) must not allow a null key.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
- final List<String> calledMethods = new ArrayList<String>();
- final KeyedObjectPool<Object,Object> kop = (KeyedObjectPool<Object,Object>)createProxy(KeyedObjectPool.class, calledMethods);
-
- final ObjectPool<Object> op = PoolUtils.adapt(kop, new Object());
- final List<String> expectedMethods = invokeEveryMethod(op);
- assertEquals(expectedMethods, calledMethods);
- }
-
- @Test
- public void testAdaptObjectPool() throws Exception {
- try {
- PoolUtils.adapt((ObjectPool<Object>)null);
- fail("PoolUtils.adapt(ObjectPool) must not allow a null pool.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
- final List<String> calledMethods = new ArrayList<String>();
- final ObjectPool<Object> op = (ObjectPool<Object>)createProxy(ObjectPool.class, calledMethods);
-
- final KeyedObjectPool<Object,Object> kop = PoolUtils.adapt(op);
- final List<String> expectedMethods = invokeEveryMethod(kop);
- assertEquals(expectedMethods, calledMethods);
- }
-
- @Test
- public void testCheckedPoolObjectPool() throws Exception {
- try {
- PoolUtils.checkedPool((ObjectPool<Object>)null, Object.class);
- fail("PoolUtils.checkedPool(ObjectPool, Class) must not allow a null pool.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
- try {
- PoolUtils.checkedPool((ObjectPool<Object>)createProxy(ObjectPool.class, (List<String>)null), null);
- fail("PoolUtils.checkedPool(ObjectPool, Class) must not allow a null type.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
- /* FIXME checks are now performed directly by the compiler!!!
-
- final List<String> calledMethods = new ArrayList<String>();
- ObjectPool<Integer> op = (ObjectPool<Integer>)createProxy(ObjectPool.class, calledMethods);
-
- ObjectPool<String> cop = PoolUtils.checkedPool(op, String.class);
- final List<String> expectedMethods = invokeEveryMethod(cop);
- assertEquals(expectedMethods, calledMethods);
-
- op = new BaseObjectPool<Integer>() {
- public Integer borrowObject() throws Exception {
- return new Integer(0);
- }
- public void returnObject(Integer obj) {}
- public void invalidateObject(Integer obj) {}
- };
- cop = PoolUtils.checkedPool(op, String.class);
-
- try {
- cop.borrowObject();
- fail("borrowObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- }
- try {
- cop.returnObject(new Integer(1));
- fail("returnObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- }
- try {
- cop.invalidateObject(new Integer(2));
- fail("invalidateObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- } */
- }
-
- @Test
- public void testCheckedPoolKeyedObjectPool() throws Exception {
- try {
- PoolUtils.checkedPool((KeyedObjectPool<Object,Object>)null, Object.class);
- fail("PoolUtils.checkedPool(KeyedObjectPool, Class) must not allow a null pool.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
- try {
- PoolUtils.checkedPool((KeyedObjectPool<Object,Object>)createProxy(KeyedObjectPool.class, (List)null), null);
- fail("PoolUtils.checkedPool(KeyedObjectPool, Class) must not allow a null type.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
- /* FIXME checks are now performed directly by the compiler!!!
-
- final List<String> calledMethods = new ArrayList<String>();
- KeyedObjectPool<Object,Integer> op = (KeyedObjectPool<Object,Integer>)createProxy(KeyedObjectPool.class, calledMethods);
-
- KeyedObjectPool cop = PoolUtils.checkedPool(op, Object.class);
- final List<String> expectedMethods = invokeEveryMethod(cop);
- assertEquals(expectedMethods, calledMethods);
-
-
- op = new BaseKeyedObjectPool<Object,Integer>() {
- public Integer borrowObject(Object key) {
- return new Integer(0);
- }
-
- public void returnObject(Object key, Integer obj) {}
-
- public void invalidateObject(Object key, Integer obj) {}
- };
- cop = PoolUtils.checkedPool(op, String.class);
-
- try {
- cop.borrowObject(null);
- fail("borrowObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- }
- try {
- cop.returnObject(null, new Integer(1));
- fail("returnObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- }
- try {
- cop.invalidateObject(null, new Integer(2));
- fail("invalidateObject should have failed as Integer !instanceof String.");
- } catch (ClassCastException cce) {
- // expected
- } */
- }
-
- @Test
public void testCheckMinIdleObjectPool() throws Exception {
try {
PoolUtils.checkMinIdle(null, 1, 1);