1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.jboss.cache.Cache; 29 import org.jboss.cache.Fqn; 30 import org.jboss.cache.InvocationContext; 31 import org.jboss.cache.Node; 32 import org.jboss.cache.config.Option; 33 import org.jboss.cache.lock.TimeoutException; 34 import org.jboss.cache.pojo.PojoCache; 35 36 public class JBossCacheWrapper 37 { 38 static final Option GRAVITATE_OPTION = new Option(); 39 static final Option LOCAL_OPTION = new Option(); 40 static 41 { 42 GRAVITATE_OPTION.setForceDataGravitation(true); 43 LOCAL_OPTION.setCacheModeLocal(true); 44 } 45 46 private static final int RETRY = 3; 47 private PojoCache pojoCache_; 48 private Cache plainCache_; 49 50 JBossCacheWrapper(PojoCache cache) 51 { 52 pojoCache_ = cache; 53 plainCache_ = pojoCache_.getCache(); 54 } 55 56 63 Object get(Fqn fqn, String id) 64 { 65 return get(fqn, id, false); 66 } 67 68 75 Object get(Fqn fqn, String id, boolean gravitate) 76 { 77 Exception ex = null; 78 for (int i = 0; i < RETRY; i++) 79 { 80 InvocationContext ctx = null; 81 Option existing = null; 82 try 83 { 84 85 Object value = null; 86 if (gravitate) 87 { 88 ctx = plainCache_.getInvocationContext(); 89 existing = ctx.getOptionOverrides(); 90 ctx.setOptionOverrides(GRAVITATE_OPTION); 91 value = plainCache_.get(fqn, id); 92 } 93 else 94 { 95 value = plainCache_.get(fqn, id); 96 } 97 return value; 98 } 99 catch (TimeoutException e) 100 { 101 ex = e; 102 } 103 catch (Exception e) 104 { 105 if (e instanceof RuntimeException ) 106 throw (RuntimeException ) e; 107 throw new RuntimeException ("JBossCacheService: exception occurred in cache get ... ", e); 108 } 109 finally 110 { 111 if (ctx != null) 112 ctx.setOptionOverrides(existing); 113 } 114 } 115 throw new RuntimeException ("JBossCacheService: exception occurred in cache get after retry ... ", ex); 116 } 117 118 126 void put(Fqn fqn, String id, Object value) 127 { 128 Exception ex = null; 129 for (int i = 0; i < RETRY; i++) 130 { 131 try 132 { 133 plainCache_.put(fqn, id, value); 134 return; 135 } 136 catch (TimeoutException e) 137 { 138 ex = e; 139 } 140 catch (Exception e) 141 { 142 throw new RuntimeException ("JBossCacheService: exception occurred in cache put ... ", e); 143 } 144 } 145 throw new RuntimeException ("JBossCacheService: exception occurred in cache put after retry ... ", ex); 146 } 147 148 149 155 void put(Fqn fqn, Map map) 156 { 157 Exception ex = null; 158 for (int i = 0; i < RETRY; i++) 159 { 160 try 161 { 162 plainCache_.put(fqn, map); 163 return; 164 } 165 catch (TimeoutException e) 166 { 167 ex = e; 168 } 169 catch (Exception e) 170 { 171 throw new RuntimeException ("JBossCacheService: exception occurred in cache put ... ", e); 172 } 173 } 174 throw new RuntimeException ("JBossCacheService: exception occurred in cache put after retry ... ", ex); 175 } 176 177 184 Object remove(Fqn fqn, String id) 185 { 186 Exception ex = null; 187 for (int i = 0; i < RETRY; i++) 188 { 189 try 190 { 191 return plainCache_.remove(fqn, id); 192 } 193 catch (TimeoutException e) 194 { 195 ex = e; 196 } 197 catch (Exception e) 198 { 199 throw new RuntimeException ("JBossCacheService: exception occurred in cache remove ... ", e); 200 } 201 } 202 throw new RuntimeException ("JBossCacheService: exception occurred in cache remove after retry ... ", ex); 203 } 204 205 210 void remove(Fqn fqn) 211 { 212 Exception ex = null; 213 for (int i = 0; i < RETRY; i++) 214 { 215 try 216 { 217 plainCache_.removeNode(fqn); 218 return; 219 } 220 catch (TimeoutException e) 221 { 222 ex = e; 223 } 224 catch (Exception e) 225 { 226 throw new RuntimeException ("JBossCacheService: exception occurred in cache remove ... ", e); 227 } 228 } 229 throw new RuntimeException ("JBossCacheService: exception occurred in cache remove after retry ... ", ex); 230 } 231 232 237 void removeLocal(Fqn fqn) 238 { 239 Exception ex = null; 240 for (int i = 0; i < RETRY; i++) 241 { 242 InvocationContext ctx = plainCache_.getInvocationContext(); 243 Option existing = ctx.getOptionOverrides(); 244 try 245 { 246 ctx.setOptionOverrides(LOCAL_OPTION); 247 plainCache_.removeNode(fqn); 248 return; 249 } 250 catch (TimeoutException e) 251 { 252 ex = e; 253 } 254 catch (Exception e) 255 { 256 throw new RuntimeException ("JBossCacheService: exception occurred in cache removeLocal ... ", e); 257 } 258 finally 259 { 260 ctx.setOptionOverrides(existing); 261 } 262 } 263 throw new RuntimeException ("JBossCacheService: exception occurred in cache removeLocal after retry ... ", ex); 264 } 265 266 271 void evict(Fqn fqn) 272 { 273 Exception ex = null; 274 for (int i = 0; i < RETRY; i++) 275 { 276 try 277 { 278 plainCache_.evict(fqn, false); 279 return; 280 } 281 catch (TimeoutException e) 282 { 283 ex = e; 284 } 285 catch (Exception e) 286 { 287 throw new RuntimeException ("JBossCacheService: exception occurred in cache evict ... ", e); 288 } 289 } 290 throw new RuntimeException ("JBossCacheService: exception occurred in cache evict after retry ... ", ex); 291 } 292 293 void evictSubtree(Fqn fqn) 294 { 295 Exception ex = null; 296 for (int i = 0; i < RETRY; i++) 297 { 298 try 299 { 300 plainCache_.evict(fqn, true); 319 320 } 321 catch (TimeoutException e) 322 { 323 ex = e; 324 } 325 catch (Exception e) 326 { 327 throw new RuntimeException ("JBossCacheService: exception occurred in cache evict ... ", e); 328 } 329 } 330 throw new RuntimeException ("JBossCacheService: exception occurred in cache evictSubtree after retry ... ", ex); 331 } 332 333 void removeLocalSubtree(Fqn fqn) 334 { 335 removeLocal(fqn); 338 339 Exception ex = null; 341 for (int i = 0; i < RETRY; i++) 342 { 343 try 344 { 345 removeLocal(fqn); 348 349 Node base = plainCache_.getChild(fqn); 352 if (base != null) 353 { 354 Set children = base.getChildren(); 355 if (children != null) 356 { 357 for (Iterator it = children.iterator(); it.hasNext(); ) 358 { 359 Node child = (Node) it.next(); 360 removeLocalSubtree(child.getFqn()); 361 } 362 363 removeLocal(fqn); 364 } 365 } 366 return; 367 368 } 369 catch (TimeoutException e) 370 { 371 ex = e; 372 } 373 catch (Exception e) 374 { 375 throw new RuntimeException ("JBossCacheService: exception occurred in cache removeLocal ... ", e); 376 } 377 } 378 throw new RuntimeException ("JBossCacheService: exception occurred in cache removeLocalSubtree after retry ... ", ex); 379 } 380 381 } 382 | Popular Tags |