}
public void returnObject(Object key, Object obj) throws Exception {
// grab the pool (list) of objects associated with the given key
CursorableLinkedList pool = null;
synchronized(this) {
pool = (CursorableLinkedList)(_poolMap.get(key));
// if it doesn't exist, create it
if(null == pool) {
pool = new CursorableLinkedList();
_poolMap.put(key, pool);
_poolList.add(key);
}
}
// if we need to validate this object, do so
boolean success = true; // whether or not this object passed validation
if((_testOnReturn && !_factory.validateObject(key, obj))) {
success = false;
try {
_factory.destroyObject(key, obj);
} catch(Exception e) {
// ignored
}
} else {
try {
_factory.passivateObject(key, obj);
} catch(Exception e) {
success = false;
}
}
boolean shouldDestroy = false;
synchronized(this) {
// subtract one from the total and keyed active counts
_totalActive--;
Integer active = (Integer)(_activeMap.get(key));
if(null == active) {
// do nothing, either null or zero is OK
} else if(active.intValue() <= 1) {
_activeMap.remove(key);
} else {
_activeMap.put(key, new Integer(active.intValue() - 1));
}
// if there's no space in the pool, flag the object
// for destruction
// else if we passivated succesfully, return it to the pool
if(_maxIdle > 0 && (pool.size() >= _maxIdle)) {
shouldDestroy = true;
} else if(success) {
pool.addFirst(new ObjectTimestampPair(obj));
_totalIdle++;
}
notifyAll();
}