1 16 17 package org.apache.commons.pool.impl; 18 19 import java.util.Iterator ; 20 import java.util.NoSuchElementException ; 21 import java.util.Stack ; 22 23 import org.apache.commons.pool.BaseObjectPool; 24 import org.apache.commons.pool.ObjectPool; 25 import org.apache.commons.pool.PoolableObjectFactory; 26 27 43 public class StackObjectPool extends BaseObjectPool implements ObjectPool { 44 50 public StackObjectPool() { 51 this((PoolableObjectFactory)null,DEFAULT_MAX_SLEEPING,DEFAULT_INIT_SLEEPING_CAPACITY); 52 } 53 54 62 public StackObjectPool(int maxIdle) { 63 this((PoolableObjectFactory)null,maxIdle,DEFAULT_INIT_SLEEPING_CAPACITY); 64 } 65 66 76 public StackObjectPool(int maxIdle, int initIdleCapacity) { 77 this((PoolableObjectFactory)null,maxIdle,initIdleCapacity); 78 } 79 80 86 public StackObjectPool(PoolableObjectFactory factory) { 87 this(factory,DEFAULT_MAX_SLEEPING,DEFAULT_INIT_SLEEPING_CAPACITY); 88 } 89 90 98 public StackObjectPool(PoolableObjectFactory factory, int maxIdle) { 99 this(factory,maxIdle,DEFAULT_INIT_SLEEPING_CAPACITY); 100 } 101 102 114 public StackObjectPool(PoolableObjectFactory factory, int maxIdle, int initIdleCapacity) { 115 _factory = factory; 116 _maxSleeping = (maxIdle < 0 ? DEFAULT_MAX_SLEEPING : maxIdle); 117 int initcapacity = (initIdleCapacity < 1 ? DEFAULT_INIT_SLEEPING_CAPACITY : initIdleCapacity); 118 _pool = new Stack (); 119 _pool.ensureCapacity( initcapacity > _maxSleeping ? _maxSleeping : initcapacity); 120 } 121 122 public synchronized Object borrowObject() throws Exception { 123 assertOpen(); 124 Object obj = null; 125 if (!_pool.empty()) { 126 obj = _pool.pop(); 127 } else { 128 if(null == _factory) { 129 throw new NoSuchElementException (); 130 } else { 131 obj = _factory.makeObject(); 132 } 133 } 134 if(null != _factory && null != obj) { 135 _factory.activateObject(obj); 136 } 137 _numActive++; 138 return obj; 139 } 140 141 public void returnObject(Object obj) throws Exception { 142 assertOpen(); 143 boolean success = true; 144 if(null != _factory) { 145 if(!(_factory.validateObject(obj))) { 146 success = false; 147 } else { 148 try { 149 _factory.passivateObject(obj); 150 } catch(Exception e) { 151 success = false; 152 } 153 } 154 } 155 156 boolean shouldDestroy = !success; 157 158 synchronized(this) { 159 _numActive--; 160 if (success) { 161 Object toBeDestroyed = null; 162 if(_pool.size() >= _maxSleeping) { 163 shouldDestroy = true; 164 toBeDestroyed = _pool.remove(0); } 166 _pool.push(obj); 167 obj = toBeDestroyed; } 169 notifyAll(); } 171 172 if(shouldDestroy) { try { 174 _factory.destroyObject(obj); 175 } catch(Exception e) { 176 } 178 } 179 } 180 181 public synchronized void invalidateObject(Object obj) throws Exception { 182 assertOpen(); 183 _numActive--; 184 if(null != _factory ) { 185 _factory.destroyObject(obj); 186 } 187 notifyAll(); } 189 190 public int getNumIdle() { 191 assertOpen(); 192 return _pool.size(); 193 } 194 195 public int getNumActive() { 196 assertOpen(); 197 return _numActive; 198 } 199 200 public synchronized void clear() { 201 assertOpen(); 202 if(null != _factory) { 203 Iterator it = _pool.iterator(); 204 while(it.hasNext()) { 205 try { 206 _factory.destroyObject(it.next()); 207 } catch(Exception e) { 208 } 210 } 211 } 212 _pool.clear(); 213 } 214 215 synchronized public void close() throws Exception { 216 clear(); 217 _pool = null; 218 _factory = null; 219 super.close(); 220 } 221 222 227 public void addObject() throws Exception { 228 Object obj = _factory.makeObject(); 229 synchronized(this) { 230 _numActive++; this.returnObject(obj); 232 } 233 } 234 235 synchronized public void setFactory(PoolableObjectFactory factory) throws IllegalStateException { 236 assertOpen(); 237 if(0 < getNumActive()) { 238 throw new IllegalStateException ("Objects are already active"); 239 } else { 240 clear(); 241 _factory = factory; 242 } 243 } 244 245 246 protected static final int DEFAULT_MAX_SLEEPING = 8; 247 248 253 protected static final int DEFAULT_INIT_SLEEPING_CAPACITY = 4; 254 255 256 protected Stack _pool = null; 257 258 259 protected PoolableObjectFactory _factory = null; 260 261 262 protected int _maxSleeping = DEFAULT_MAX_SLEEPING; 263 264 265 protected int _numActive = 0; 266 } 267 | Popular Tags |