1 22 package org.jboss.ha.framework.server.util; 23 24 import java.io.Serializable ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.Timer ; 28 import java.util.TimerTask ; 29 import javax.naming.InitialContext ; 30 31 import org.jboss.ha.framework.interfaces.DistributedState; 32 import org.jboss.ha.framework.interfaces.HAPartition; 33 import org.jboss.logging.Logger; 34 import org.jboss.util.CachePolicy; 35 36 49 public class DistributedTimedCachePolicy extends TimerTask 50 implements CachePolicy 51 { 52 54 public static interface TimedEntry extends Serializable 55 { 56 60 public void init(long now); 61 62 65 public boolean isCurrent(long now); 66 67 70 public boolean refresh(); 71 72 74 public void destroy(); 75 76 79 public Object getValue(); 80 } 81 82 protected static Timer resolutionTimer = new Timer (true); 83 protected static Logger log = Logger.getLogger(DistributedTimedCachePolicy.class); 84 85 86 protected DistributedState entryMap; 87 protected String category; 88 protected String partitionName; 89 91 protected int defaultLifetime; 92 93 protected long now; 94 95 protected int resolution; 96 97 101 public DistributedTimedCachePolicy(String category, String partitionName, 102 int defaultLifetime) 103 { 104 this(category, partitionName, defaultLifetime, 0); 105 } 106 120 public DistributedTimedCachePolicy(String category, String partitionName, 121 int defaultLifetime, int resolution) 122 { 123 this.category = category; 124 this.partitionName = partitionName; 125 this.defaultLifetime = defaultLifetime; 126 if( resolution <= 0 ) 127 resolution = 60; 128 this.resolution = resolution; 129 } 130 131 134 public void create() throws Exception 135 { 136 InitialContext ctx = new InitialContext (); 138 String jndiName = "/HAPartition/" + partitionName; 139 HAPartition partition = (HAPartition) ctx.lookup(jndiName); 140 this.entryMap = partition.getDistributedStateService(); 141 log.debug("Obtained DistributedState from partition="+partitionName); 142 now = System.currentTimeMillis(); 143 } 144 147 public void start() 148 { 149 resolutionTimer.scheduleAtFixedRate(this, 0, 1000*resolution); 150 } 151 153 public void stop() 154 { 155 super.cancel(); 156 } 157 159 public void destroy() 160 { 161 } 162 163 169 public Object get(Object key) 170 { 171 Serializable skey = (Serializable ) key; 172 TimedEntry entry = (TimedEntry) entryMap.get(category, skey); 173 if( entry == null ) 174 return null; 175 176 if( entry.isCurrent(now) == false ) 177 { if( entry.refresh() == false ) 179 { entry.destroy(); 181 try 182 { 183 entryMap.remove(category, skey); 184 } 185 catch(Exception e) 186 { 187 log.debug("Failed to remove expired entry", e); 188 } 189 return null; 190 } 191 } 192 Object value = entry.getValue(); 193 return value; 194 } 195 200 public Object peek(Object key) 201 { 202 Serializable skey = (Serializable ) key; 203 TimedEntry entry = (TimedEntry) entryMap.get(category, skey); 204 Object value = null; 205 if( entry != null ) 206 value = entry.getValue(); 207 return value; 208 } 209 217 public void insert(Object key, Object value) 218 { 219 Serializable skey = (Serializable ) key; 220 TimedEntry entry = (TimedEntry) entryMap.get(category, skey); 221 if( entry != null ) 222 throw new IllegalStateException ("Attempt to insert duplicate entry"); 223 if( (value instanceof TimedEntry) == false ) 224 { Serializable svalue = (Serializable ) value; 226 entry = new DefaultTimedEntry(defaultLifetime, svalue); 227 } 228 else 229 { 230 entry = (TimedEntry) value; 231 } 232 233 entry.init(now); 234 try 235 { 236 entryMap.set(category, skey, entry); 237 } 238 catch(Exception e) 239 { 240 log.error("Failed to set entry", e); 241 } 242 } 243 244 247 public void remove(Object key) 248 { 249 Serializable skey = (Serializable ) key; 250 try 251 { 252 TimedEntry entry = (TimedEntry) entryMap.remove(category, skey); 253 if( entry != null ) 254 entry.destroy(); 255 } 256 catch(Exception e) 257 { 258 log.error("Failed to remove entry", e); 259 } 260 } 261 263 public void flush() 264 { 265 Collection keys = entryMap.getAllKeys(category); 266 Iterator iter = keys.iterator(); 268 while( iter.hasNext() ) 269 { 270 Serializable key = (Serializable ) iter.next(); 271 TimedEntry entry = (TimedEntry) entryMap.get(category, key); 272 entry.destroy(); 273 } 274 } 275 276 public int size() 277 { 278 return entryMap.getAllKeys(category).size(); 279 } 280 282 285 public int getDefaultLifetime() 286 { 287 return defaultLifetime; 288 } 289 293 public void setDefaultLifetime(int defaultLifetime) 294 { 295 this.defaultLifetime = defaultLifetime; 296 } 297 298 301 public void run() 302 { 303 now = System.currentTimeMillis(); 304 } 305 306 309 public long currentTimeMillis() 310 { 311 return now; 312 } 313 314 317 public TimedEntry peekEntry(Object key) 318 { 319 Serializable skey = (Serializable ) key; 320 TimedEntry entry = (TimedEntry) entryMap.get(category, skey); 321 return entry; 322 } 323 324 327 static class DefaultTimedEntry implements TimedEntry 328 { 329 long expirationTime; 330 Serializable value; 331 332 DefaultTimedEntry(long lifetime, Serializable value) 333 { 334 this.expirationTime = 1000 * lifetime; 335 this.value = value; 336 } 337 public void init(long now) 338 { 339 expirationTime += now; 340 } 341 public boolean isCurrent(long now) 342 { 343 return expirationTime > now; 344 } 345 public boolean refresh() 346 { 347 return false; 348 } 349 public void destroy() 350 { 351 } 352 public Object getValue() 353 { 354 return value; 355 } 356 } 357 } 358 | Popular Tags |