1 23 24 29 42 43 48 49 50 package com.sun.enterprise.util.pool; 51 52 import java.util.Collection ; 53 import java.util.ArrayList ; 54 55 import com.sun.enterprise.util.scheduler.PeriodicallyServicable; 56 import com.sun.enterprise.util.scheduler.PeriodicEventScheduler; 57 58 import com.sun.enterprise.util.ApproximateClock; 59 import java.util.logging.Logger ; 61 import java.util.logging.Level ; 62 import com.sun.logging.LogDomains; 63 65 77 public abstract class AbstractPool 78 implements Pool 79 { 80 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 82 protected boolean bDebug=false; 84 protected Collection collection; 85 protected ArrayList listeners; 86 protected ObjectFactory factory = null; 87 protected int waitCount = 0; 88 protected int createdCount = 0; 89 90 protected Object onHold = null; 91 protected Object closed = null; 92 93 protected static ApproximateClock _clock = new ApproximateClock(15000); 94 protected PeriodicEventScheduler scheduler; 95 96 97 protected AbstractPool() { 98 scheduler = PeriodicEventScheduler.getInstance(); 99 } 100 101 102 109 public Object getObject(boolean canWait, Object param) 110 throws InterruptedException , PoolException 111 { 112 Object object; 113 114 if (closed != null) { 115 throw new PoolException("Pool closed. Cannot obtain object"); 116 } 117 118 synchronized (collection) { 119 while (true) { 120 if (collection.size() > 0) { 121 if ( (object = checkout(param)) != null) { 122 return object; 123 } 124 } else if (canCreate()) { 125 createdCount++; 126 break; 127 } 128 129 if (canWait) { 130 try { 131 waitCount++; 132 beforeWait(param); 133 collection.wait(); 134 afterNotify(param); 135 waitCount--; 136 } catch (InterruptedException inEx) { 137 throw new RequestInterruptedException("InterruptedException", inEx); 138 } 139 } else { 140 return null; 141 } 142 } 143 } 144 145 try { 146 object = factory.create(param); 147 } catch (PoolException poolEx) { 148 synchronized (collection) { 149 createdCount--; 150 } 151 throw poolEx; 152 } 153 afterCreate(object); 154 155 return object; 156 } 157 158 167 public Object getObject(long waitFor, Object param) 168 throws InterruptedException , PoolException 169 { 170 171 if (closed != null) { 172 throw new PoolException("Pool closed. Cannot obtain object"); 173 } 174 175 long now = _clock.getTime(); 176 long timeLeft = waitFor; 177 long startTime = now; 178 Object object; 179 synchronized (collection) { 180 while (true) { 181 if (collection.size() > 0) { 182 if ( (object = checkout(param)) != null) { 183 return object; 184 } 185 } else if (canCreate()) { 186 createdCount++; 187 break; 188 } 189 190 if (timeLeft > 0) { 191 try { 192 waitCount++; 193 beforeWait(param); 194 collection.wait(); 195 afterNotify(param); 196 waitCount--; 197 } catch (InterruptedException inEx) { 198 throw new RequestInterruptedException("InterruptedException", inEx); 199 } 200 } else { 201 return null; 202 } 203 now = _clock.getTime(); 204 timeLeft = now - startTime; 205 startTime = now; 206 } 207 } 208 209 try { 210 object = factory.create(param); 211 } catch (PoolException poolEx) { 212 synchronized (collection) { 213 createdCount--; 214 } 215 throw poolEx; 216 } 217 afterCreate(object); 218 219 return object; 220 } 221 222 227 public void returnObject(Object object) { 228 synchronized (collection) { 229 if (closed != null) { 230 if (waitCount == 0) { 231 destroyObject(object); 232 return; 233 } 234 } 235 236 checkin(object); 237 if (waitCount > 0) { 238 collection.notify(); 239 } 240 } 241 } 242 243 250 public void destroyObject(Object object) { 251 beforeDestroy(object); 252 factory.destroy(object); 253 synchronized (collection) { 254 createdCount--; 255 if (waitCount > 0) { 256 collection.notify(); 257 } 258 } 259 } 260 261 269 protected abstract Object checkin(Object object); 270 271 278 protected abstract Object checkout(Object param); 279 280 281 285 public boolean addPoolListener(PoolListener listener) { 286 synchronized (this) { 287 if (listeners == null) { 288 listeners = new ArrayList (); 289 listeners.add(listener); 290 return true; 291 } 292 293 if (listeners.indexOf(listener) == -1) { 294 listeners.add(listener); 295 return true; 296 } else { 297 return false; 298 } 299 } 300 } 301 302 303 307 public boolean removePoolListener(PoolListener listener) { 308 synchronized (this) { 309 if (listeners == null) { 310 return false; 311 } else { 312 return listeners.remove(listener); 313 } 314 } 315 } 316 317 318 protected abstract boolean canCreate(); 319 320 324 public void preload(int count) { 325 if (count <= 0) { 326 return; 327 } 328 329 ArrayList tempList = new ArrayList (count); 330 for (int i=0; i<count; i++) { 331 try { 332 tempList.add(factory.create(null)); 333 } catch (PoolException poolEx) { 334 335 } 336 } 337 338 count = tempList.size(); 339 synchronized (collection) { 340 for (int i=0; i<count; i++) { 341 checkin(tempList.get(i)); 342 } 343 createdCount += count; 344 } 345 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"After preload(" + count + "): Size: " + collection.size()); 348 } 350 351 public int size() { 352 return collection.size(); 353 } 354 355 358 public int destroyPoolObjects() { 359 return destroyPoolObjects(collection.size()); 360 } 361 362 365 public int destroyPoolObjects(int count) { 366 if (count <= 0) { 367 return 0; 368 } 369 370 Object [] array = collection.toArray(); 371 ArrayList arrayList = null; 372 synchronized (collection) { 373 if (count > collection.size()) { 374 count = collection.size(); 375 } 376 arrayList = new ArrayList (count); 377 for (int i=0; i<count; i++) { 378 arrayList.add(checkout(null)); 379 } 380 count = arrayList.size(); 381 createdCount -= count; 382 } 383 384 for (int i=0; i<count; i++) { 385 factory.destroy(arrayList.get(i)); 386 } 387 return count; 388 } 389 390 394 public void close() { 395 onClose(); 397 synchronized (collection) { 398 closed = "__Closed__"; 399 int diff = collection.size() - this.waitCount; 400 destroyPoolObjects(diff); 401 } 406 } 407 408 409 413 public boolean isClosed() { 414 return (closed != null); 415 } 416 417 419 423 public void afterCreate(Object object) { 424 if (listeners != null) { 425 int size = listeners.size(); 426 for (int i=0; i<size; i++) { 427 ((PoolListener) listeners.get(i)).afterCreate(object); 428 } 429 } 430 } 431 432 436 public void beforeDestroy(Object object) { 437 if (listeners != null) { 438 int size = listeners.size(); 439 for (int i=0; i<size; i++) { 440 ((PoolListener) listeners.get(i)).beforeDestroy(object); 441 } 442 } 443 } 444 445 448 public void beforeWait(Object object) { 449 if (listeners != null) { 450 int size = listeners.size(); 451 for (int i=0; i<size; i++) { 452 ((PoolListener) listeners.get(i)).beforeWait(object); 453 } 454 } 455 } 456 457 460 public void afterNotify(Object object) { 461 if (listeners != null) { 462 int size = listeners.size(); 463 for (int i=0; i<size; i++) { 464 ((PoolListener) listeners.get(i)).afterNotify(object); 465 } 466 } 467 } 468 469 472 public void onClose() { 473 if (listeners != null) { 474 int size = listeners.size(); 475 for (int i=0; i<size; i++) { 476 ((PoolListener) listeners.get(i)).onClose(); 477 } 478 } 479 } 480 481 482 483 } 484 | Popular Tags |