1 23 24 29 30 package com.sun.appserv.util.cache; 31 32 import java.text.MessageFormat ; 33 34 import java.util.Properties ; 35 import java.util.Map ; 36 import java.util.ResourceBundle ; 37 38 43 public class BoundedMultiLruCache extends MultiLruCache { 44 45 protected long maxSize = Constants.DEFAULT_MAX_CACHE_SIZE; 47 protected long currentSize; 48 private Object currentSizeLk = new Object (); 49 50 54 public void init(int maxCapacity, Properties props) throws Exception { 55 super.init(maxCapacity, props); 56 currentSize = 0; 57 58 if (props != null) { 59 String strMaxSize = props.getProperty("MaxSize"); 60 int multiplier = 1; 61 long size = -1; 62 63 String prop = strMaxSize; 64 if (prop != null) { 65 int index; 66 67 prop = prop.toUpperCase(); 69 70 if ((index = prop.indexOf("KB")) != -1) { 72 multiplier = Constants.KB; 73 prop = prop.substring(0, index); 74 } else if ((index = prop.indexOf("MB")) != -1) { 75 multiplier = Constants.MB; 76 prop = prop.substring(0, index); 77 } 78 79 try { 80 size = Long.parseLong(prop.trim()); 81 } catch (NumberFormatException nfe) {} 82 } 83 84 if (size > 0) 86 maxSize = (size * multiplier); 87 else { 88 String msg = _rb.getString("cache.BoundedMultiLruCache.illegalMaxSize"); 89 90 Object [] params = { strMaxSize }; 91 msg = MessageFormat.format(msg, params); 92 93 throw new IllegalArgumentException (msg); 94 } 95 } 96 } 97 98 105 protected CacheItem itemAdded(CacheItem item) { 106 LruCacheItem overflow = (LruCacheItem) super.itemAdded(item); 107 108 if (overflow != null) { 110 decrementCurrentSize(overflow.getSize()); 111 } 112 incrementCurrentSize(item.getSize()); 113 114 return overflow; 115 } 116 117 123 protected void itemRefreshed(CacheItem item, int oldSize) { 124 super.itemRefreshed(item, oldSize); 125 126 129 decrementCurrentSize(oldSize); 130 incrementCurrentSize(item.getSize()); 131 } 132 133 139 protected void itemRemoved(CacheItem item) { 140 super.itemRemoved(item); 141 142 decrementCurrentSize(item.getSize()); 144 } 145 146 150 protected boolean isThresholdReached() { 151 return (currentSize > maxSize || super.isThresholdReached()); 152 } 153 154 157 protected final void incrementCurrentSize(int size) { 158 synchronized(currentSizeLk) { 159 currentSize += size; 160 } 161 } 162 163 protected final void decrementCurrentSize(int size) { 164 synchronized(currentSizeLk) { 165 currentSize -= size; 166 } 167 } 168 169 172 173 179 public Object getStatByName(String key) { 180 Object stat = super.getStatByName(key); 181 182 if (stat == null && key != null) { 183 if (key.equals(Constants.STAT_BOUNDEDMULTILRUCACHE_CURRENT_SIZE)) 184 stat = new Long (currentSize); 185 else if (key.equals(Constants.STAT_BOUNDEDMULTILRUCACHE_MAX_SIZE)) { 186 if (maxSize == Constants.DEFAULT_MAX_CACHE_SIZE) 187 stat = Constants.STAT_DEFAULT; 188 else 189 stat = new Long (maxSize); 190 } 191 } 192 193 return stat; 194 } 195 196 public Map getStats() { 197 Map stats = super.getStats(); 198 199 stats.put(Constants.STAT_BOUNDEDMULTILRUCACHE_CURRENT_SIZE, 201 new Long (currentSize)); 202 if (maxSize == Constants.DEFAULT_MAX_CACHE_SIZE) { 203 stats.put(Constants.STAT_BOUNDEDMULTILRUCACHE_MAX_SIZE, 204 Constants.STAT_DEFAULT); 205 } else { 206 stats.put(Constants.STAT_BOUNDEDMULTILRUCACHE_MAX_SIZE, 207 new Long (maxSize)); 208 } 209 return stats; 210 } 211 } 212 | Popular Tags |