1 2 3 package org.enhydra.shark.caching; 4 5 import java.util.*; 6 7 import org.enhydra.shark.api.internal.caching.ResourceCache; 8 import org.enhydra.shark.api.internal.working.WfResourceInternal; 9 import org.enhydra.shark.api.RootException; 10 import org.enhydra.shark.utilities.LRUMap; 11 12 import org.enhydra.shark.api.internal.working.CallbackUtilities; 13 18 public class LRUResourceCache implements ResourceCache { 19 20 private final int defaultCacheSize=100; 21 22 25 protected LRUMap cache; 26 27 34 public void configure (CallbackUtilities cus) throws RootException { 35 cus.getProperties(); 36 String resCacheSize=cus.getProperty("LRUResourceCache.Size"); 37 try { 38 int cacheSize=Integer.parseInt(resCacheSize.trim()); 39 cache = new LRUMap(cacheSize); 40 } catch (Exception ex){ 41 cache = new LRUMap(defaultCacheSize); 42 } 43 cus.info("Resource Cache configured - max. size is "+cache.getMaximumSize()); 44 } 45 46 54 public void add(String username, WfResourceInternal res) throws RootException { 55 synchronized(this) { 56 cache.put(username, res); 58 } 59 } 60 61 67 public void remove(String username) throws RootException { 68 synchronized(this) { 69 cache.remove(username); 70 } 71 } 72 73 81 public synchronized WfResourceInternal get(String username) throws RootException { 82 WfResourceInternal res = null; 83 synchronized(this) { 84 res =(WfResourceInternal)cache.get(username); 85 } 86 return res; 87 } 88 89 95 public java.util.List getAll () throws RootException { 96 if (cache.size()> 0) { 97 synchronized(this) { 98 return new ArrayList(cache.values()); 99 } 100 } else { 101 return new ArrayList(); 102 } 103 } 104 105 113 public void setSize (int size) throws RootException { 114 if (size<0) throw new RootException("Can't set negative resource cache size"); 115 synchronized(this) { 116 cache.setMaximumSize(size); 117 } 118 } 119 120 126 public int getSize () throws RootException { 127 return cache.getMaximumSize(); 128 } 129 130 public int howManyEntries() throws RootException { 131 return cache.size(); 132 } 133 134 } 135 136 137 | Popular Tags |