1 7 package org.jboss.cache.loader; 8 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.Modification; 11 import org.jboss.cache.RegionManager; 12 import org.jboss.cache.config.CacheLoaderConfig; 13 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 14 15 import java.io.ObjectInputStream ; 16 import java.io.ObjectOutputStream ; 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 33 public class ChainingCacheLoader extends AbstractCacheLoader 34 { 35 private final List <CacheLoader> cacheLoaders = new ArrayList <CacheLoader>(2); 36 private final List <CacheLoader> writeCacheLoaders = new ArrayList <CacheLoader>(2); 37 private final List <CacheLoaderConfig.IndividualCacheLoaderConfig> cacheLoaderConfigs = new ArrayList <CacheLoaderConfig.IndividualCacheLoaderConfig>(2); 38 39 44 public void setConfig(IndividualCacheLoaderConfig config) 45 { 46 } 48 49 public IndividualCacheLoaderConfig getConfig() 50 { 51 return null; 52 } 53 54 62 public Set <String > getChildrenNames(Fqn fqn) throws Exception 63 { 64 Set <String > answer = null; 65 Iterator <CacheLoader> i = cacheLoaders.iterator(); 66 while (i.hasNext()) 67 { 68 CacheLoader l = i.next(); 69 answer = l.getChildrenNames(fqn); 70 if (answer != null && answer.size() > 0) break; 71 } 72 return answer; 73 } 74 75 83 public Map get(Fqn name) throws Exception 84 { 85 Map answer = null; 86 Iterator <CacheLoader> i = cacheLoaders.iterator(); 87 while (i.hasNext()) 88 { 89 CacheLoader l = i.next(); 90 answer = l.get(name); 91 if (answer != null) break; 92 } 93 return answer; 94 } 95 96 102 public boolean exists(Fqn name) throws Exception 103 { 104 boolean answer = false; 105 Iterator <CacheLoader> i = cacheLoaders.iterator(); 106 while (i.hasNext()) 107 { 108 CacheLoader l = i.next(); 109 answer = l.exists(name); 110 if (answer) break; 111 } 112 return answer; 113 } 114 115 119 public Object put(Fqn name, Object key, Object value) throws Exception 120 { 121 Object answer = null; 122 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 123 boolean isFirst = true; 124 while (i.hasNext()) 125 { 126 CacheLoader l = i.next(); 127 Object tAnswer = l.put(name, key, value); 128 if (isFirst) 129 { 130 answer = tAnswer; 131 isFirst = false; 132 } 133 134 } 135 return answer; 136 } 137 138 147 public void put(Fqn name, Map attributes) throws Exception 148 { 149 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 150 while (i.hasNext()) 151 { 152 CacheLoader l = i.next(); 153 l.put(name, attributes); 154 } 155 } 156 157 164 public void put(List <Modification> modifications) throws Exception 165 { 166 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 167 while (i.hasNext()) 168 { 169 CacheLoader l = i.next(); 170 l.put(modifications); 171 } 172 } 173 174 178 public Object remove(Fqn name, Object key) throws Exception 179 { 180 Object answer = null; 181 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 182 boolean isFirst = true; 183 while (i.hasNext()) 184 { 185 CacheLoader l = i.next(); 186 Object tAnswer = l.remove(name, key); 187 if (isFirst) 188 { 189 answer = tAnswer; 190 isFirst = false; 191 } 192 } 193 return answer; 194 } 195 196 200 public void remove(Fqn name) throws Exception 201 { 202 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 203 while (i.hasNext()) 204 { 205 CacheLoader l = i.next(); 206 l.remove(name); 207 } 208 } 209 210 216 public void removeData(Fqn name) throws Exception 217 { 218 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 219 while (i.hasNext()) 220 { 221 CacheLoader l = i.next(); 222 l.removeData(name); 223 } 224 } 225 226 241 public void prepare(Object tx, List modifications, boolean one_phase) throws Exception 242 { 243 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 244 while (i.hasNext()) 245 { 246 CacheLoader l = i.next(); 247 l.prepare(tx, modifications, one_phase); 248 } 249 } 250 251 260 public void commit(Object tx) throws Exception 261 { 262 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 263 while (i.hasNext()) 264 { 265 CacheLoader l = i.next(); 266 l.commit(tx); 267 } 268 } 269 270 276 public void rollback(Object tx) 277 { 278 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 279 while (i.hasNext()) 280 { 281 CacheLoader l = i.next(); 282 l.rollback(tx); 283 } 284 } 285 286 287 292 public void create() throws Exception 293 { 294 Iterator <CacheLoader> it = cacheLoaders.iterator(); 295 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> cfgIt = cacheLoaderConfigs.iterator(); 296 while (it.hasNext() && cfgIt.hasNext()) 297 { 298 CacheLoader cl = it.next(); 299 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgIt.next(); 300 cl.create(); 301 } 302 } 303 304 public void start() throws Exception 305 { 306 Iterator <CacheLoader> it = cacheLoaders.iterator(); 307 while (it.hasNext()) 308 { 309 (it.next()).start(); 310 } 311 } 312 313 public void stop() 314 { 315 Iterator <CacheLoader> it = cacheLoaders.iterator(); 316 while (it.hasNext()) 317 { 318 (it.next()).stop(); 319 } 320 } 321 322 public void destroy() 323 { 324 Iterator <CacheLoader> it = cacheLoaders.iterator(); 325 while (it.hasNext()) 326 { 327 (it.next()).destroy(); 328 } 329 } 330 331 334 public void setRegionManager(RegionManager manager) 335 { 336 } 338 339 public void loadEntireState(ObjectOutputStream os) throws Exception 340 { 341 Iterator <CacheLoader> i = cacheLoaders.iterator(); 342 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator(); 343 while (i.hasNext() && cfgs.hasNext()) 344 { 345 CacheLoader l = i.next(); 346 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next(); 347 if (cfg.isFetchPersistentState()) 348 { 349 l.loadEntireState(os); 350 break; 351 } 352 } 353 } 354 355 public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception 356 { 357 Iterator <CacheLoader> i = cacheLoaders.iterator(); 358 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator(); 359 while (i.hasNext() && cfgs.hasNext()) 360 { 361 CacheLoader l = i.next(); 362 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next(); 363 if (cfg.isFetchPersistentState()) 364 { 365 l.loadState(subtree, os); 366 break; 367 } 368 } 369 } 370 371 public void storeEntireState(ObjectInputStream is) throws Exception 372 { 373 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 374 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator(); 375 while (i.hasNext()) 376 { 377 CacheLoader l = i.next(); 378 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next(); 379 if (cfg.isFetchPersistentState()) 380 { 381 l.storeEntireState(is); 382 break; 383 } 384 } 385 386 } 387 388 public void storeState(Fqn subtree, ObjectInputStream is) throws Exception 389 { 390 Iterator <CacheLoader> i = writeCacheLoaders.iterator(); 391 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> cfgs = cacheLoaderConfigs.iterator(); 392 while (i.hasNext()) 393 { 394 CacheLoader l = i.next(); 395 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = cfgs.next(); 396 if (cfg.isFetchPersistentState()) 397 { 398 l.storeState(subtree, is); 399 break; 400 } 401 } 402 } 403 404 407 public int getSize() 408 { 409 return cacheLoaders.size(); 410 } 411 412 415 public List <CacheLoader> getCacheLoaders() 416 { 417 return Collections.unmodifiableList(cacheLoaders); 418 } 419 420 426 public void addCacheLoader(CacheLoader l, CacheLoaderConfig.IndividualCacheLoaderConfig cfg) 427 { 428 synchronized (this) 429 { 430 cacheLoaderConfigs.add(cfg); 431 cacheLoaders.add(l); 432 433 if (!cfg.isIgnoreModifications()) 434 { 435 writeCacheLoaders.add(l); 436 } 437 } 438 } 439 440 public String toString() 441 { 442 StringBuffer buf = new StringBuffer ("ChainingCacheLoader{"); 443 Iterator <CacheLoader> i = cacheLoaders.iterator(); 444 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> c = cacheLoaderConfigs.iterator(); 445 int count = 0; 446 while (i.hasNext() && c.hasNext()) 447 { 448 CacheLoader loader = i.next(); 449 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = c.next(); 450 451 buf.append(++count); 452 buf.append(": IgnoreMods? "); 453 buf.append(cfg.isIgnoreModifications()); 454 buf.append(" CLoader: "); 455 buf.append(loader); 456 buf.append("; "); 457 } 458 buf.append("}"); 459 return buf.toString(); 460 } 461 462 public void purgeIfNecessary() throws Exception 463 { 464 Iterator <CacheLoader> loaders = cacheLoaders.iterator(); 465 Iterator <CacheLoaderConfig.IndividualCacheLoaderConfig> configs = cacheLoaderConfigs.iterator(); 466 467 while (loaders.hasNext() && configs.hasNext()) 468 { 469 CacheLoader myLoader = loaders.next(); 470 CacheLoaderConfig.IndividualCacheLoaderConfig myConfig = configs.next(); 471 472 if (!myConfig.isIgnoreModifications() && myConfig.isPurgeOnStartup()) myLoader.remove(Fqn.ROOT); 473 } 474 475 476 } 477 } 478 | Popular Tags |