1 23 24 29 42 43 48 49 package com.sun.enterprise.util.pool; 50 51 import java.util.List ; 52 import java.util.Iterator ; 53 import java.util.ArrayList ; 54 import java.util.Collection ; 55 56 import java.lang.ref.Reference ; 57 import java.lang.ref.SoftReference ; 58 59 60 import com.sun.enterprise.util.scheduler.PeriodicallyServicable; 61 import com.sun.enterprise.util.scheduler.PeriodicEventScheduler; 62 63 import com.sun.enterprise.util.ApproximateClock; 64 65 import com.sun.enterprise.util.collection.DList; 66 import com.sun.enterprise.util.collection.DListNode; 67 import com.sun.enterprise.util.collection.DListNodeFactory; 68 69 import com.sun.enterprise.util.collection.FastStack; 70 import java.util.logging.Logger ; 72 import java.util.logging.Level ; 73 import com.sun.logging.LogDomains; 74 76 77 public class ObjectPool 78 extends com.sun.enterprise.util.pool.AbstractPool 79 implements PeriodicallyServicable 80 { 81 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 83 protected DList list; 85 protected int minSize; 86 protected int initialSize; 87 protected int maxLimit; 88 protected long maxIdleTime; 89 90 protected Boolean isBounded; 91 92 99 public ObjectPool(ObjectFactory factory, int minSize, int initialSize, long maxIdleTime) { 100 super(); 101 super.factory = factory; 102 this.minSize = minSize; 103 this.initialSize = initialSize; 104 this.maxIdleTime = maxIdleTime; 105 106 setMaxLimit(-1); 107 108 initPool(); 109 } 110 111 119 public ObjectPool(ObjectFactory factory, int minSize, int initialSize, int maxLimit, 120 long maxIdleTime) 121 { 122 super(); 123 super.factory = factory; 124 this.minSize = minSize; 125 this.maxIdleTime = maxIdleTime; 126 this.initialSize = initialSize; 127 128 setMaxLimit(maxLimit); 129 130 initPool(); 131 } 132 133 134 public int getMaxLimit() { 135 return this.maxLimit; 136 } 137 138 public void setMaxLimit(int limit) { 139 if ((limit <= 0) || (limit >= Integer.MAX_VALUE-1)) { 140 this.isBounded = null; 141 } else { 142 this.isBounded = new Boolean (true); 143 this.maxLimit = limit; 144 } 145 } 146 147 148 private void initPool() { 149 list = new DList(); 150 151 super.collection = list; 152 super.preload((minSize < initialSize) ? initialSize : minSize); 153 154 scheduler.addTimeRepeatableTask(this, (int) maxIdleTime); 155 } 156 157 160 protected boolean canCreate() { 161 return (isBounded == null) ? true : (createdCount < maxLimit); 162 } 163 164 172 protected Object checkin(Object object) { 173 list.addAsLastNode(new TimeStampedDListNode(object, _clock.getTime())); 174 return this; 175 } 176 177 private Object obtainObject(Object param) { 178 SoftReference ref; 179 Object object; 180 181 TimeStampedDListNode tsNode = (TimeStampedDListNode) list.delinkLastNode(); 182 return tsNode.object; 183 } 184 185 192 protected Object checkout(Object param) { 193 return obtainObject(param); 194 } 195 196 197 198 200 public void prolog() { 201 } 202 203 public void service() { 204 int killedCount = 0; 205 206 long now = _clock.getTime(); 207 long allowed = now - maxIdleTime; 208 209 TimeStampedDListNode tsNode = null; 210 FastStack stack = new FastStack(); 211 212 synchronized (super.collection) { 213 Object done = null; 214 while (done == null) { 215 tsNode = (TimeStampedDListNode) list.getFirstDListNode(); 216 217 if (tsNode == null) { done = new Object (); 219 } else if (tsNode.timeStamp <= allowed) { 220 list.delink(tsNode); 222 stack.push(tsNode.object); 223 killedCount++; 224 } else { 225 done = new Object (); 227 } 228 } 230 super.createdCount -= killedCount; 231 232 if (createdCount < minSize) { 233 super.preload(minSize - createdCount); 234 } 235 236 if (waitCount > 0) { 237 if (killedCount == 1) { 238 collection.notify(); 239 } else { 240 collection.notifyAll(); 241 } 242 } 243 244 } 246 247 while (! stack.isEmpty()) { 249 Object object = stack.pop(); 250 beforeDestroy(object); 251 factory.destroy(object); 252 } 253 254 _logger.log(Level.FINE,"Leaving service after killing " + killedCount + " (idle) objects. Now size: " + list.size()); 257 } 259 260 public void epilog() { 261 } 262 263 267 public long getFrequency() { 268 return this.maxIdleTime; 269 } 270 271 275 public boolean getExecuteIfMissed() { 276 return true; 277 } 278 279 283 public boolean getExecutionTolerance(long missedByMillis) { 284 return true; 285 } 286 287 290 public String toString() { 291 return ""; 292 } 293 294 296 class TimeStampedDListNode 297 extends DListNode 298 { 299 long timeStamp; 300 301 public TimeStampedDListNode(Object object, long ts) { 302 super(object); 303 this.timeStamp = ts; 304 305 } 310 311 public String toString() { 312 return "TSDListNode: " + object; 313 } 314 315 316 } 317 318 319 } 320 | Popular Tags |