1 25 26 package org.objectweb.perseus.pool.lib; 27 28 import org.objectweb.fractal.api.control.BindingController; 29 import org.objectweb.perseus.pool.api.Pool; 30 import org.objectweb.perseus.pool.api.PoolAttributes; 31 import org.objectweb.perseus.pool.api.PoolMatchFactory; 32 import org.objectweb.perseus.pool.api.PoolException; 33 34 import java.util.Collection ; 35 import java.util.Collections ; 36 import java.util.Iterator ; 37 38 45 public class LArrayPool implements Pool, PoolAttributes, BindingController { 46 47 public final static String POOL_MATCH_FACTORY_BINDING = "pool-match-factory"; 48 49 private PRCell arrayPool[] = null; 50 private int minSize = 0; 51 private int maxSize = 0; 52 private int newMinSize = 0; 53 private int newMaxSize = 0; 54 private long timeout = 0; 55 private int curSize = 0; 56 private int freeSize = 0; 57 private PoolMatchFactory matchFactory = null; 58 private PRCell freeList = null; 59 private PRCell activeList = null; 60 61 64 public String [] listFc() { 65 return new String [] { POOL_MATCH_FACTORY_BINDING }; 66 } 67 68 public Object lookupFc(String clientItfName) { 69 if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName)) 70 return matchFactory; 71 else 72 return null; 73 } 74 75 public void bindFc(String clientItfName, Object serverItf) { 76 if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName)) 77 matchFactory = (PoolMatchFactory) serverItf; 78 } 79 80 public void unbindFc(String clientItfName) { 81 if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName)) 82 matchFactory = null; 83 } 84 85 86 89 public long getTimeout() { 90 return timeout; 91 } 92 93 public int getMinSize() { 94 return newMinSize; 95 } 96 97 public int getMaxSize() { 98 return newMaxSize; 99 } 100 101 public synchronized void setTimeout(long crto) { 102 } 103 104 public void setMinSize(int minsize) { 105 newMinSize = minsize; 106 } 107 108 public void setMaxSize(int maxsize) { 109 newMaxSize = maxsize; 110 } 111 112 public long getTTL() { 113 return -1; 114 } 115 116 public void setTTL(long ttl) { 117 } 118 119 public long getInactiveTTL() { 120 return -1; 121 } 122 123 public void setInactiveTTL(long ttl) { 124 } 125 126 private void updateSize () throws PoolException { 127 if (maxSize != newMaxSize) { 128 changeMaxSize(newMaxSize); 129 maxSize = newMaxSize; 130 } 131 if (minSize != newMinSize) { 132 changeMinSize(newMinSize); 133 minSize = newMinSize; 134 } 135 } 136 137 private void changeMaxSize(int maxsize) throws PoolException { 138 if ((maxsize < 0) || (maxsize < newMinSize) || (maxsize == maxSize)) 139 return; 140 PRCell nap[]; 141 if (maxsize > maxSize) { 142 nap = new PRCell[maxsize]; 143 int i; 144 for (i = 0; i < maxSize; i++) 145 nap[i] = arrayPool[i]; 146 for (; i < maxsize; i++) 147 nap[i] = null; 148 maxSize = maxsize; 149 } else { 150 int cursize = curSize; 151 if (maxsize < curSize) { 152 int nbRemove = maxSize - maxsize; 154 PRCell remcell; 155 while ((freeList != null) && (nbRemove > 0)) { 156 remcell = freeList; 157 freeList = freeList.remove(freeList); 158 matchFactory.destroyResource(remcell.getResource()); 159 arrayPool[remcell.getIndex()] = null; 160 nbRemove--; 161 cursize--; 162 } 163 nap = new PRCell[cursize]; 165 maxSize = cursize; 166 } else { 167 nap = new PRCell[maxsize]; 168 maxSize = maxsize; 169 } 170 for (int i = 0, j = 0; i < curSize; i++) { 172 if (arrayPool[i] != null) 173 nap[j++] = arrayPool[i]; 174 } 175 curSize = cursize; 176 } 177 arrayPool = nap; 178 } 179 180 private void changeMinSize(int minsize) throws PoolException { 181 if ((minsize < 0) || (minsize > maxSize) || (minsize == minSize)) 182 return; 183 if (minsize < minSize) { 184 minSize = minsize; 185 return; 186 } 187 PRCell newprc; 188 while (minsize > curSize) { 189 newprc = new PRCell(matchFactory.createResource(null), curSize); 190 arrayPool[curSize++] = newprc; 191 freeList = newprc.insert(freeList); 192 freeSize++; 193 } 194 minSize = minsize; 195 } 196 197 200 public Object getResource(Object hints) 201 throws PoolException { 202 return getResource(hints, null); 203 } 204 205 public synchronized Object getResource(Object hints, 206 Object user) throws PoolException { 207 updateSize(); 208 PRCell res = null; 209 if (freeList != null) { 210 Iterator it = freeList.iterator(); 211 while (it.hasNext()) { 212 res = (PRCell) it.next(); 213 if (matchFactory.matchResource(res.getResource(), hints)) { 214 freeList = res.remove(freeList); 215 activeList = res.insert(activeList); 216 freeSize--; 217 break; 218 } 219 } 220 } 221 if (res == null) { 222 if (curSize < maxSize) { 223 res = new PRCell(matchFactory.createResource(hints), curSize); 224 arrayPool[curSize++] = res; 225 activeList = res.insert(activeList); 226 } else { 227 throw new PoolException("No more resource available in pool!!"); 228 } 229 } 230 return res.getResource(); 232 } 233 234 public synchronized int getSize() { 235 return curSize; 236 } 237 238 public synchronized int getFreeResourceNumber() { 239 return freeSize; 240 } 241 public synchronized int getUsedResourceNumber() { 242 return curSize - freeSize; 243 } 244 public synchronized Collection getUsers() { 245 return Collections.EMPTY_LIST; 246 } 247 248 249 public synchronized void releaseResource(Object resource) 250 throws PoolException { 251 updateSize(); 252 if (activeList == null) 253 throw new PoolException("Releases inactive resource - 1!!"); 254 PRCell prc = activeList.search(resource); 255 if (prc == null) 256 throw new PoolException("Releases inactive resource - 2!!"); 257 activeList = prc.remove(activeList); 258 freeList = prc.insert(freeList); 259 freeSize++; 260 } 262 263 264 private void printLists() { 265 if (maxSize != 200) 266 return; 267 System.err.println("minSize=" + minSize + ", maxSize=" + maxSize 268 + ", curSize=" + curSize + ", freeSize=" + freeSize); 269 System.err.print("activeList:"); 270 if (activeList == null) 271 System.err.print(" null"); 272 else { 273 PRCell cur = activeList; 274 do { 275 System.err.print(" " + cur); 276 cur = cur.getNext(); 277 } while (cur != activeList); 278 } 279 System.err.println("."); 280 System.err.print("freeList:"); 281 if (freeList == null) 282 System.err.print(" null"); 283 else { 284 PRCell cur = freeList; 285 do { 286 System.err.print(" " + cur); 287 cur = cur.getNext(); 288 } while (cur != freeList); 289 } 290 System.err.println("."); 291 } 292 293 } 294 295 class PRCell { 296 private PRCell next; 297 private PRCell prev; 298 private Object poolResource; 299 private int index; 300 301 PRCell(Object pr, int ind) { 302 next = prev = null; 303 poolResource = pr; 304 index = ind; 305 } 306 307 int getIndex() { 308 return index; 309 } 310 311 PRCell getNext() { 312 return next; 313 } 314 315 Object getResource() { 316 return poolResource; 317 } 318 319 void destroy() throws PoolException { 320 } 321 322 PRCell insert(PRCell prclist) { 323 if (prclist == null) { 324 next = prev = this; 325 } else { 326 PRCell prev2 = prclist.prev; 327 prclist.prev.next = this; 328 prclist.prev = this; 329 next = prclist; 330 prev = prev2; 331 } 332 return this; 333 } 334 335 Iterator iterator() { 336 return new PRCIter(this); 337 } 338 339 PRCell remove(PRCell prclist) throws PoolException { 340 if (prclist == this) { 341 if (prev == this) { 342 next = prev = null; 343 return null; 344 } 345 PRCell next2 = prclist.next; 346 prclist.prev.next = next2; 347 next2.prev = prclist.prev; 348 next = prev = null; 349 return next2; 350 } 351 PRCell rem = prclist.next; 352 while ((rem != this) && (rem != prclist)) 353 rem = rem.next; 354 if (rem == prclist) 355 throw new PoolException("LArrayPool.PRCell.remove: cell not on this list!!"); 356 rem.next.prev = rem.prev; 357 rem.prev.next = rem.next; 358 next = prev = null; 359 return prclist; 360 } 361 362 PRCell search(Object pr) { 363 PRCell end = this; 364 PRCell cur = this; 365 do { 366 if (cur.poolResource == pr) 367 return cur; 368 cur = cur.next; 369 } while (cur != end); 370 return null; 371 } 372 373 void setIndex(int ind) { 374 index = ind; 375 } 376 377 class PRCIter implements Iterator { 378 PRCell cur, end; 379 380 PRCIter(PRCell list) { 381 if (list != null) 382 end = list.prev; 383 cur = list; 384 } 385 386 public boolean hasNext() { 387 return cur != null; 388 } 389 390 public Object next() { 391 if (cur == null) 392 return null; 393 if (cur == end) { 394 cur = null; 395 return end; 396 } 397 cur = cur.next; 398 return cur.prev; 399 } 400 401 public void remove() { 402 } 403 } 404 } | Popular Tags |