1 7 package org.jboss.cache.loader; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.CacheListener; 12 import org.jboss.cache.CacheSPI; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.config.CacheLoaderConfig; 15 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 16 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 57 public class CacheLoaderManager 58 { 59 private static Log log = LogFactory.getLog(CacheLoaderManager.class); 60 private CacheLoaderConfig config; 61 private CacheSPI cache; 62 private CacheLoader loader; 63 private boolean fetchPersistentState; 64 65 72 public void setConfig(CacheLoaderConfig config, CacheSPI cache) throws Exception 73 { 74 this.config = config == null ? new CacheLoaderConfig() : config; 75 this.cache = cache; 76 loader = createCacheLoader(); 77 } 78 79 87 private CacheLoader createCacheLoader() throws Exception 88 { 89 CacheLoader tmpLoader = null; 90 93 ArrayList <IndividualCacheLoaderConfig> finalConfigs = 94 new ArrayList <IndividualCacheLoaderConfig>(); 95 96 if (config.useChainingCacheLoader()) 98 { 99 tmpLoader = new ChainingCacheLoader(); 101 ChainingCacheLoader ccl = (ChainingCacheLoader) tmpLoader; 102 Iterator it = config.getIndividualCacheLoaderConfigs().iterator(); 103 104 int numLoadersWithFetchPersistentState = 0; 106 while (it.hasNext()) 107 { 108 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = (CacheLoaderConfig.IndividualCacheLoaderConfig) it.next(); 109 if (cfg.isFetchPersistentState()) 110 { 111 numLoadersWithFetchPersistentState++; 112 fetchPersistentState = true; 113 } 114 if (numLoadersWithFetchPersistentState > 1) 115 { 116 throw new Exception ("Invalid cache loader configuration!! Only ONE cache loader may have fetchPersistentState set to true. Cache will not start!"); 117 } 118 if (cfg.isSingletonStore() && config.isShared()) 119 { 120 throw new Exception ("Invalid cache loader configuration!! If a cache loader is configured as a singleton, the cache loader cannot be shared in a cluster!"); 121 } 122 123 CacheLoader l = createCacheLoader(cfg, cache); 124 cfg = l.getConfig(); 125 finalConfigs.add(cfg); 126 ccl.addCacheLoader(l, cfg); 129 130 } 131 } 132 else 133 { 134 CacheLoaderConfig.IndividualCacheLoaderConfig cfg = config.getIndividualCacheLoaderConfigs().get(0); 135 tmpLoader = createCacheLoader(cfg, cache); 136 finalConfigs.add(tmpLoader.getConfig()); 137 fetchPersistentState = cfg.isFetchPersistentState(); 138 } 139 140 config.setIndividualCacheLoaderConfigs(finalConfigs); 142 143 return tmpLoader; 144 } 145 146 154 private CacheLoader createCacheLoader(CacheLoaderConfig.IndividualCacheLoaderConfig cfg, CacheSPI cache) throws Exception 155 { 156 CacheLoader tmpLoader = createInstance(cfg.getClassName()); 158 159 if (tmpLoader != null) 160 { 161 if (cfg.isAsync()) 163 { 164 CacheLoader asyncDecorator; 165 asyncDecorator = new AsyncCacheLoader(tmpLoader); 166 tmpLoader = asyncDecorator; 167 } 168 169 if (cfg.isSingletonStore()) 171 { 172 SingletonStoreCacheLoader singletonDecorator = new SingletonStoreCacheLoader(tmpLoader, cfg.isPushStateWhenCoordinator()); 173 addCacheListener(cache, singletonDecorator.getCacheListener()); 174 tmpLoader = singletonDecorator; 175 } 176 177 tmpLoader.setConfig(cfg); 179 180 tmpLoader.setCache(cache); 181 if (cache != null && cache.getConfiguration().isUseRegionBasedMarshalling()) 186 { 187 tmpLoader.setRegionManager(cache.getRegionManager()); 188 } 189 } 190 return tmpLoader; 191 } 192 193 private CacheLoader createInstance(String className) throws ClassNotFoundException , IllegalAccessException , InstantiationException 194 { 195 Class cl = Thread.currentThread().getContextClassLoader().loadClass(className); 196 return (CacheLoader) cl.newInstance(); 197 } 198 199 204 public void preloadCache() throws Exception 205 { 206 if (config.getPreload() == null || config.getPreload().equals("")) return; 207 if (log.isDebugEnabled()) log.debug("preloading transient state from cache loader " + loader); 208 StringTokenizer st = new StringTokenizer (config.getPreload(), ","); 209 String tok; 210 Fqn fqn; 211 long start, stop, total; 212 start = System.currentTimeMillis(); 213 while (st.hasMoreTokens()) 214 { 215 tok = st.nextToken(); 216 fqn = Fqn.fromString(tok.trim()); 217 if (log.isTraceEnabled()) log.trace("preloading " + fqn); 218 preload(fqn, true, true); 219 } 220 221 stop = System.currentTimeMillis(); 222 total = stop - start; 223 if (log.isDebugEnabled()) 224 { 225 log.debug("preloading transient state from cache loader was successful (in " + total + " milliseconds)"); 226 } 227 } 228 229 237 public void preload(Fqn fqn, boolean preloadParents, boolean preloadChildren) throws Exception 238 { 239 240 cache.get(fqn, "bla"); 243 244 if (preloadParents) 246 { 247 Fqn tmp_fqn = Fqn.ROOT; 248 for (int i = 0; i < fqn.size() - 1; i++) 249 { 250 tmp_fqn = new Fqn(tmp_fqn, fqn.get(i)); 251 cache.get(tmp_fqn, "bla"); 252 } 253 } 254 255 if (preloadChildren) 256 { 257 Set children = loader.getChildrenNames(fqn); 259 if (children != null) 260 { 261 for (Iterator it = children.iterator(); it.hasNext();) 262 { 263 String child_name = (String ) it.next(); 264 Fqn child_fqn = new Fqn(fqn, child_name); 265 preload(child_fqn, false, true); 266 } 267 } 268 } 269 } 270 271 274 public CacheLoaderConfig getCacheLoaderConfig() 275 { 276 return config; 277 } 278 279 282 public CacheLoader getCacheLoader() 283 { 284 return loader; 285 } 286 287 292 public void setCacheLoader(CacheLoader loader) 293 { 294 this.loader = loader; 295 } 296 297 300 public boolean isPassivation() 301 { 302 return config.isPassivation(); 303 } 304 305 308 public boolean isFetchPersistentState() 309 { 310 return fetchPersistentState; 311 } 312 313 public void stopCacheLoader() 314 { 315 if (loader == null) throw new RuntimeException ("Problem with configured cache loader - it has been set to null!"); 316 loader.stop(); 318 loader.destroy(); 320 } 321 322 public void startCacheLoader() throws Exception 323 { 324 if (loader == null) throw new RuntimeException ("Improperly configured cache loader - cache loader is null!"); 325 loader.create(); 327 loader.start(); 329 330 purgeLoaders(false); 331 } 332 333 public void purgeLoaders(boolean force) throws Exception 334 { 335 if ((loader instanceof ChainingCacheLoader) && !force) 336 { 337 ((ChainingCacheLoader) loader).purgeIfNecessary(); 338 } 339 else 340 { 341 CacheLoaderConfig.IndividualCacheLoaderConfig first = getCacheLoaderConfig().getFirstCacheLoaderConfig(); 342 if (force || 343 (first != null && first.isPurgeOnStartup())) 344 { 345 loader.remove(Fqn.ROOT); 346 } 347 } 348 } 349 350 protected void addCacheListener(CacheSPI cache, CacheListener listener) 351 { 352 cache.addCacheListener(listener); 353 } 354 } 355 | Popular Tags |