1 package org.sapia.ubik.net; 2 3 import java.util.*; 4 5 6 17 public abstract class Pool { 18 public static final long NO_TIME_OUT = -1; 19 public static final int NO_MAX = 0; 20 protected List _objects = Collections.synchronizedList(new ArrayList( 21 50)); 22 protected int _maxSize = NO_MAX; 23 protected int _currentCount; 24 protected long _lastUsageTime = System.currentTimeMillis(); 25 26 public Pool() { 27 } 28 29 public Pool(int maxSize) { 30 _maxSize = maxSize; 31 } 32 33 43 public synchronized Object acquire() throws InterruptedException , Exception { 44 return acquire(NO_TIME_OUT); 45 } 46 47 60 public synchronized Object acquire(long timeout) 61 throws InterruptedException , NoObjectAvailableException, Exception { 62 _lastUsageTime = System.currentTimeMillis(); 63 64 Object obj; 65 66 if (_objects.size() == 0) { 67 if (_maxSize <= NO_MAX) { 68 obj = newObject(); 69 } else { 70 long start = System.currentTimeMillis(); 71 72 while (_objects.size() == 0) { 73 if (timeout > 0) { 74 wait(timeout); 75 76 if ((System.currentTimeMillis() - start) > timeout) { 77 break; 78 } 79 } else { 80 if (_currentCount > _maxSize) { 81 wait(); 82 } else { 83 break; 84 } 85 } 86 } 87 88 if (_objects.size() == 0) { 89 if (_currentCount >= _maxSize) { 90 throw new NoObjectAvailableException(); 91 } else { 92 obj = newObject(); 93 } 94 } else { 95 obj = _objects.remove(0); 96 } 97 } 98 } else { 99 obj = _objects.remove(0); 100 } 101 102 return onAcquire(obj); 103 } 104 105 protected Object onAcquire(Object o) throws Exception { 106 return o; 107 } 108 109 protected void onRelease(Object o) { 110 } 111 112 118 public synchronized void release(Object obj) { 119 _objects.add(obj); 120 onRelease(obj); 121 notify(); 122 } 123 124 128 public long getLastUsageTime() { 129 return _lastUsageTime; 130 } 131 132 138 public int getCreatedCount() { 139 return _currentCount; 140 } 141 142 147 public int size() { 148 return _objects.size(); 149 } 150 151 160 public synchronized void shrinkTo(int size) { 161 while ((_objects.size() > size) && (_objects.size() > 0)) { 162 _currentCount--; 163 164 if (_currentCount < 0) { 165 _currentCount = 0; 166 } 167 168 cleanup(_objects.remove(0)); 169 } 170 } 171 172 178 public synchronized void fill(int toSize) throws Exception { 179 for (int i = 0; i < toSize; i++) { 180 if ((_maxSize > NO_MAX) && (_objects.size() >= _maxSize)) { 181 break; 182 } 183 184 _objects.add(newObject()); 185 } 186 } 187 188 200 public synchronized Object acquireCreate() throws Exception { 201 if(_objects.size() == 0){ 202 if(getCreatedCount() >= _maxSize && _maxSize > NO_MAX){ 203 return null; 204 } 205 else{ 206 return acquire(); 207 } 208 } 209 else{ 210 return acquire(); 211 } 212 } 213 214 217 218 226 protected abstract Object doNewObject() throws Exception ; 227 228 235 protected void cleanup(Object pooled) { 236 } 237 238 private Object newObject() throws Exception { 239 Object toReturn = doNewObject(); 240 _currentCount++; 241 242 return toReturn; 243 } 244 } 245 | Popular Tags |