1 16 17 package org.apache.commons.pool.impl; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.NoSuchElementException ; 22 import java.util.Stack ; 23 24 import org.apache.commons.pool.BaseKeyedObjectPool; 25 import org.apache.commons.pool.KeyedObjectPool; 26 import org.apache.commons.pool.KeyedPoolableObjectFactory; 27 28 43 public class StackKeyedObjectPool extends BaseKeyedObjectPool implements KeyedObjectPool { 44 50 public StackKeyedObjectPool() { 51 this((KeyedPoolableObjectFactory)null,DEFAULT_MAX_SLEEPING,DEFAULT_INIT_SLEEPING_CAPACITY); 52 } 53 54 62 public StackKeyedObjectPool(int max) { 63 this((KeyedPoolableObjectFactory)null,max,DEFAULT_INIT_SLEEPING_CAPACITY); 64 } 65 66 76 public StackKeyedObjectPool(int max, int init) { 77 this((KeyedPoolableObjectFactory)null,max,init); 78 } 79 80 86 public StackKeyedObjectPool(KeyedPoolableObjectFactory factory) { 87 this(factory,DEFAULT_MAX_SLEEPING); 88 } 89 90 98 public StackKeyedObjectPool(KeyedPoolableObjectFactory factory, int max) { 99 this(factory,max,DEFAULT_INIT_SLEEPING_CAPACITY); 100 } 101 102 114 public StackKeyedObjectPool(KeyedPoolableObjectFactory factory, int max, int init) { 115 _factory = factory; 116 _maxSleeping = (max < 0 ? DEFAULT_MAX_SLEEPING : max); 117 _initSleepingCapacity = (init < 1 ? DEFAULT_INIT_SLEEPING_CAPACITY : init); 118 _pools = new HashMap (); 119 _activeCount = new HashMap (); 120 } 121 122 public synchronized Object borrowObject(Object key) throws Exception { 123 Object obj = null; 124 Stack stack = (Stack )(_pools.get(key)); 125 if(null == stack) { 126 stack = new Stack (); 127 stack.ensureCapacity( _initSleepingCapacity > _maxSleeping ? _maxSleeping : _initSleepingCapacity); 128 _pools.put(key,stack); 129 } 130 try { 131 obj = stack.pop(); 132 _totIdle--; 133 } catch(Exception e) { 134 if(null == _factory) { 135 throw new NoSuchElementException (); 136 } else { 137 obj = _factory.makeObject(key); 138 } 139 } 140 if(null != obj && null != _factory) { 141 _factory.activateObject(key,obj); 142 } 143 incrementActiveCount(key); 144 return obj; 145 } 146 147 public synchronized void returnObject(Object key, Object obj) throws Exception { 148 decrementActiveCount(key); 149 if(null == _factory || _factory.validateObject(key,obj)) { 150 Stack stack = (Stack )(_pools.get(key)); 151 if(null == stack) { 152 stack = new Stack (); 153 stack.ensureCapacity( _initSleepingCapacity > _maxSleeping ? _maxSleeping : _initSleepingCapacity); 154 _pools.put(key,stack); 155 } 156 if(null != _factory) { 157 try { 158 _factory.passivateObject(key,obj); 159 } catch(Exception e) { 160 _factory.destroyObject(key,obj); 161 return; 162 } 163 } 164 if(stack.size() < _maxSleeping) { 165 stack.push(obj); 166 _totIdle++; 167 } else { 168 if(null != _factory) { 169 _factory.destroyObject(key,obj); 170 } 171 } 172 } else { 173 if(null != _factory) { 174 _factory.destroyObject(key,obj); 175 } 176 } 177 } 178 179 public synchronized void invalidateObject(Object key, Object obj) throws Exception { 180 decrementActiveCount(key); 181 if(null != _factory) { 182 _factory.destroyObject(key,obj); 183 } 184 notifyAll(); } 186 187 public void addObject(Object key) throws Exception { 188 Object obj = _factory.makeObject(key); 189 synchronized(this) { 190 incrementActiveCount(key); returnObject(key,obj); 192 } 193 } 194 195 public int getNumIdle() { 196 return _totIdle; 197 } 198 199 public int getNumActive() { 200 return _totActive; 201 } 202 203 public int getNumActive(Object key) { 204 return getActiveCount(key); 205 } 206 207 public synchronized int getNumIdle(Object key) { 208 try { 209 return((Stack )(_pools.get(key))).size(); 210 } catch(Exception e) { 211 return 0; 212 } 213 } 214 215 public synchronized void clear() { 216 Iterator it = _pools.keySet().iterator(); 217 while(it.hasNext()) { 218 Object key = it.next(); 219 Stack stack = (Stack )(_pools.get(key)); 220 destroyStack(key,stack); 221 } 222 _totIdle = 0; 223 _pools.clear(); 224 _activeCount.clear(); 225 } 226 227 public synchronized void clear(Object key) { 228 Stack stack = (Stack )(_pools.remove(key)); 229 destroyStack(key,stack); 230 } 231 232 private synchronized void destroyStack(Object key, Stack stack) { 233 if(null == stack) { 234 return; 235 } else { 236 if(null != _factory) { 237 Iterator it = stack.iterator(); 238 while(it.hasNext()) { 239 try { 240 _factory.destroyObject(key,it.next()); 241 } catch(Exception e) { 242 } 244 } 245 } 246 _totIdle -= stack.size(); 247 _activeCount.remove(key); 248 stack.clear(); 249 } 250 } 251 252 public synchronized String toString() { 253 StringBuffer buf = new StringBuffer (); 254 buf.append(getClass().getName()); 255 buf.append(" contains ").append(_pools.size()).append(" distinct pools: "); 256 Iterator it = _pools.keySet().iterator(); 257 while(it.hasNext()) { 258 Object key = it.next(); 259 buf.append(" |").append(key).append("|="); 260 Stack s = (Stack )(_pools.get(key)); 261 buf.append(s.size()); 262 } 263 return buf.toString(); 264 } 265 266 public synchronized void close() throws Exception { 267 clear(); 268 _pools = null; 269 _factory = null; 270 _activeCount = null; 271 } 272 273 public synchronized void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException { 274 if(0 < getNumActive()) { 275 throw new IllegalStateException ("Objects are already active"); 276 } else { 277 clear(); 278 _factory = factory; 279 } 280 } 281 282 private int getActiveCount(Object key) { 283 try { 284 return ((Integer )_activeCount.get(key)).intValue(); 285 } catch(NoSuchElementException e) { 286 return 0; 287 } catch(NullPointerException e) { 288 return 0; 289 } 290 } 291 292 private void incrementActiveCount(Object key) { 293 _totActive++; 294 Integer old = (Integer )(_activeCount.get(key)); 295 if(null == old) { 296 _activeCount.put(key,new Integer (1)); 297 } else { 298 _activeCount.put(key,new Integer (old.intValue() + 1)); 299 } 300 } 301 302 private void decrementActiveCount(Object key) { 303 _totActive--; 304 Integer active = (Integer )(_activeCount.get(key)); 305 if(null == active) { 306 } else if(active.intValue() <= 1) { 308 _activeCount.remove(key); 309 } else { 310 _activeCount.put(key, new Integer (active.intValue() - 1)); 311 } 312 } 313 314 315 protected static final int DEFAULT_MAX_SLEEPING = 8; 316 317 322 protected static final int DEFAULT_INIT_SLEEPING_CAPACITY = 4; 323 324 325 protected HashMap _pools = null; 326 327 328 protected KeyedPoolableObjectFactory _factory = null; 329 330 331 protected int _maxSleeping = DEFAULT_MAX_SLEEPING; 332 333 334 protected int _initSleepingCapacity = DEFAULT_INIT_SLEEPING_CAPACITY; 335 336 337 protected int _totActive = 0; 338 339 340 protected int _totIdle = 0; 341 342 343 protected HashMap _activeCount = null; 344 345 } 346 | Popular Tags |