1 64 65 70 package com.jcorporate.expresso.core.cache; 71 72 import org.apache.log4j.Logger; 73 74 import java.util.Enumeration ; 75 import java.util.Vector ; 76 77 78 84 public class OrderedCache 85 implements Cache { 86 87 private Vector cacheContents = new Vector (); 88 89 private String cacheName = null; 90 91 private long accessCount = 0; 92 93 private int maxSize = 0; 94 95 private static final String thisClass = OrderedCache.class.getName(); 96 97 private static final Logger log = Logger.getLogger(OrderedCache.class); 98 99 102 public OrderedCache() { 103 super(); 104 } 105 106 111 public void addItem(CacheEntry newItem) { 112 113 114 115 if ((maxSize > 0) && (cacheContents.size() >= maxSize)) { 116 long minUsed = java.lang.Long.MAX_VALUE; 117 CacheEntry leastUsedItem = null; 118 CacheEntry oneItem = null; 119 120 synchronized (cacheContents) { 121 for (Enumeration e = cacheContents.elements(); 122 e.hasMoreElements();) { 123 oneItem = (CacheEntry) e.nextElement(); 124 125 126 127 if (oneItem.isExpired()) { 128 break; 129 } 130 if (oneItem.getUsedCount() < minUsed) { 131 leastUsedItem = oneItem; 132 minUsed = oneItem.getUsedCount(); 133 } 134 } 135 136 removeItem(oneItem.getContents()); 137 } 138 } 139 140 cacheContents.addElement(newItem); 141 newItem.clearUsedCount(); 142 } 143 144 147 public void clear() { 148 synchronized (cacheContents) { 149 cacheContents = new Vector (); 150 } 151 } 152 153 154 161 public CacheEntry getCacheEntry(String itemKey) { 162 CacheEntry oneItem = null; 163 synchronized (cacheContents) { 164 for (Enumeration allItems = cacheContents.elements(); 165 allItems.hasMoreElements();) { 166 oneItem = (CacheEntry) allItems.nextElement(); 167 168 if (oneItem.getKey().equals(itemKey)) { 169 if (oneItem.isExpired()) { 170 removeItem(oneItem.getContents()); 171 172 return null; 173 } 174 oneItem.incrementUsedCount(); 175 176 return oneItem; 177 } 178 } 179 } 180 181 return null; 182 } 183 184 190 public Cacheable getItem(String itemKey) { 191 accessCount++; 192 193 CacheEntry oneItem = this.getCacheEntry(itemKey); 194 195 if (oneItem == null) { 196 return null; 197 } else { 198 return oneItem.getContents(); 199 } 200 } 201 202 207 public int getItemCount() { 208 return cacheContents.size(); 209 } 210 211 217 public Vector getItems() { 218 accessCount++; 219 220 Vector aClone = null; 221 CacheEntry oneEntry = null; 222 223 synchronized (cacheContents) { 224 if (cacheContents.size() > 0) { 225 oneEntry = (CacheEntry) cacheContents.get(0); 226 if (oneEntry.isExpired()) { 227 this.clear(); 228 return new Vector (1); 229 } else { 230 aClone = (Vector ) cacheContents.clone(); 231 } 232 } else { 233 return new Vector (1); 234 } 235 } 236 237 Vector v = new Vector (aClone.size()); 238 for (Enumeration e = aClone.elements(); e.hasMoreElements();) { 239 oneEntry = (CacheEntry) e.nextElement(); 240 v.addElement(oneEntry.getContents()); 241 } 242 243 return v; 244 } 245 246 251 public String getName() { 252 return cacheName; 253 } 254 255 260 public long getUsedCount() { 261 return accessCount; 262 } 263 264 265 271 public boolean isOrdered() { 272 return true; 273 } 274 275 276 281 public void removeItem(Cacheable oldItem) { 282 CacheEntry oneItem = null; 283 for (int i = 0; i < cacheContents.size(); i++) { 284 285 try { 286 oneItem = (CacheEntry) cacheContents.elementAt(i); 287 if (oneItem != null) { 288 if (oneItem.getKey().equals(oldItem.getKey())) { 289 oneItem.clearUsedCount(); 290 291 synchronized (cacheContents) { 292 cacheContents.removeElement(oneItem); 293 } 294 } 295 } 296 } catch (Exception ex) { 297 if (log.isDebugEnabled()) { 300 log.debug("Ignored Exception", ex); 301 } 302 } 303 } 304 305 } 306 307 308 314 public void setItems(java.util.List newItems) throws CacheException { 315 if (newItems instanceof java.util.Vector ) { 316 setItems((Vector ) newItems); 317 } else { 318 accessCount++; 319 clear(); 320 321 Object oneObject = null; 322 323 for (java.util.Iterator e = newItems.iterator(); e.hasNext();) { 324 oneObject = e.next(); 325 326 if (oneObject instanceof Cacheable) { 327 CacheEntry ce = new CacheEntry((Cacheable) oneObject, -1); 328 addItem(ce); 329 } else { 330 String myName = (thisClass + "setItems(Vector)"); 331 throw new CacheException(myName + 332 ":Item in vector is of class " + 333 oneObject.getClass().getName() + 334 ", which is not Cacheable - cannot set items"); 335 } 336 } 337 return; 338 } 339 } 340 341 346 public void setItems(Vector newItems) 347 throws CacheException { 348 accessCount++; 349 clear(); 350 351 Object oneObject = null; 352 353 for (Enumeration e = newItems.elements(); e.hasMoreElements();) { 354 oneObject = e.nextElement(); 355 356 if (oneObject instanceof Cacheable) { 357 CacheEntry ce = new CacheEntry((Cacheable) oneObject, -1); 358 addItem(ce); 359 } else { 360 String myName = (thisClass + "setItems(Vector)"); 361 throw new CacheException(myName + 362 ":Item in vector is of class " + 363 oneObject.getClass().getName() + 364 ", which is not Cacheable - cannot set items"); 365 } 366 } 367 } 368 369 370 375 public synchronized void setMaxSize(int newMaxSize) { 376 maxSize = newMaxSize; 377 } 378 379 384 public synchronized void setName(String newName) { 385 cacheName = newName; 386 } 387 388 } 389 | Popular Tags |