use of org.apache.commons.pool2.PooledObjectFactory in project tomcat by apache.
the class PoolImplUtils method getFactoryType.
/**
* Identifies the concrete type of object that an object factory creates.
*
* @param factoryClass The factory to examine
*
* @return the type of object the factory creates
*/
@SuppressWarnings("rawtypes")
static Class<?> getFactoryType(final Class<? extends PooledObjectFactory> factoryClass) {
final Class<PooledObjectFactory> type = PooledObjectFactory.class;
final Object genericType = getGenericType(type, factoryClass);
if (genericType instanceof Integer) {
// POOL-324 org.apache.commons.pool2.impl.GenericObjectPool.getFactoryType() throws
// java.lang.ClassCastException
//
// A bit hackish, but we must handle cases when getGenericType() does not return a concrete types.
final ParameterizedType pi = getParameterizedType(type, factoryClass);
if (pi != null) {
final Type[] bounds = ((TypeVariable) pi.getActualTypeArguments()[((Integer) genericType).intValue()]).getBounds();
if (bounds != null && bounds.length > 0) {
final Type bound0 = bounds[0];
if (bound0 instanceof Class) {
return (Class<?>) bound0;
}
}
}
// last resort: Always return a Class
return Object.class;
}
return (Class<?>) genericType;
}
use of org.apache.commons.pool2.PooledObjectFactory in project cachecloud by sohutv.
the class JedisPoolTest method returnResourceDestroysResourceOnException.
@Test
public void returnResourceDestroysResourceOnException() {
class CrashingJedis extends Jedis {
@Override
public void resetState() {
throw new RuntimeException();
}
}
final AtomicInteger destroyed = new AtomicInteger(0);
class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {
@Override
public PooledObject<Jedis> makeObject() throws Exception {
return new DefaultPooledObject<Jedis>(new CrashingJedis());
}
@Override
public void destroyObject(PooledObject<Jedis> p) throws Exception {
destroyed.incrementAndGet();
}
@Override
public boolean validateObject(PooledObject<Jedis> p) {
return true;
}
@Override
public void activateObject(PooledObject<Jedis> p) throws Exception {
}
@Override
public void passivateObject(PooledObject<Jedis> p) throws Exception {
}
}
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
pool.initPool(config, new CrashingJedisPooledObjectFactory());
Jedis crashingJedis = pool.getResource();
try {
crashingJedis.close();
} catch (Exception ignored) {
}
assertEquals(destroyed.get(), 1);
}
Aggregations