/** * Checks the configuration for peer listener that work with replication schemes for which place * holders exist. If such a peer listener and place holder exists the listener will be attached * to the place holder. * * @param cacheManager * the cache manager to be modified * @param helper * the configuration helper */ private static void attachPeerListenerToPlaceHolder(CacheManager cacheManager, ConfigurationHelper helper) { Map<String, CacheManagerPeerListener> peerListenerMap = helper.createCachePeerListeners(); if (peerListenerMap != null) { for (String scheme : peerListenerMap.keySet()) { CacheManagerPeerListener listener = cacheManager.getCachePeerListener(scheme); if (listener instanceof PlaceHolderCacheManagerPeerListener) { if (LOG.isDebugEnabled()) { LOG.debug("Adding cache peer listener for scheme " + scheme + " to place holder"); } ((PlaceHolderCacheManagerPeerListener) listener) .attachCacheManagerPeerListener(peerListenerMap.get(scheme)); } else { LOG.warn("Ignoring cache peer listener for scheme " + scheme + " because there is no place holder for that scheme"); } } } }
/** * Checks the configuration for peer provider that work with replication schemes for which place * holders exist. If such a peer provider and place holder exists the provider will be attached * to the place holder. * * @param cacheManager * the cache manager to be modified * @param helper * the configuration helper */ private static void attachPeerProviderToPlaceHolder(CacheManager cacheManager, ConfigurationHelper helper) { Map<String, CacheManagerPeerProvider> peerProviderMap = helper.createCachePeerProviders(); if (peerProviderMap != null) { for (String scheme : peerProviderMap.keySet()) { CacheManagerPeerProvider provider = cacheManager .getCacheManagerPeerProvider(scheme); if (provider instanceof PlaceHolderCacheManagerPeerProvider) { if (LOG.isDebugEnabled()) { LOG.debug("Adding cache peer provider for scheme " + scheme + " to place holder"); } ((PlaceHolderCacheManagerPeerProvider) provider) .attachCacheManagerPeerProvider(peerProviderMap.get(scheme)); } else { LOG.warn("Ignoring cache peer provider for scheme " + scheme + " because there is no place holder for that scheme"); } } } }
/** * Extend the already initialized cache manager with additional configuration. * * @param cacheManager * the cache manager to modify * @param config * the configuration to parse for settings that can be added at runtime */ @SuppressWarnings("unchecked") private static void extendEhCacheWithCustomConfig(CacheManager cacheManager, Configuration config) { ConfigurationHelper helper = new ConfigurationHelper(cacheManager, config); Set<net.sf.ehcache.Cache> caches = helper.createCaches(); for (net.sf.ehcache.Cache cache : caches) { if (cacheManager.cacheExists(cache.getName())) { if (LOG.isDebugEnabled()) { LOG.debug("Skipping cache with name " + cache.getName() + " because it already exists"); } Set<CacheEventListener> listeners = cache.getCacheEventNotificationService() .getCacheEventListeners(); net.sf.ehcache.Cache existCache = cacheManager.getCache(cache.getName()); for (CacheEventListener listener : listeners) { LOG.debug("copy cache event listener for cache " + cache.getName()); existCache.getCacheEventNotificationService().registerListener(listener); } } else { if (LOG.isDebugEnabled()) { LOG.debug("Adding cache with name " + cache.getName()); } cacheManager.addCache(cache); } } attachPeerProviderToPlaceHolder(cacheManager, helper); attachPeerListenerToPlaceHolder(cacheManager, helper); }
@SuppressWarnings({ "unchecked", "unused" }) public void init() { log.info("Loading ehcache"); // log.debug("Appcontext: " + applicationContext.toString()); try { // instance the manager CacheManager cm = CacheManager.getInstance(); // Use the Configuration to create our caches Configuration configuration = new Configuration(); //set initial default cache name String defaultCacheName = Cache.DEFAULT_CACHE_NAME; //add the configs to a configuration for (CacheConfiguration conf : configs) { //set disk expiry conf.setDiskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds); //set eviction policy conf.setMemoryStoreEvictionPolicy(memoryStoreEvictionPolicy); if (null == cache) { //get first cache name and use as default defaultCacheName = conf.getName(); configuration.addDefaultCache(conf); } else { configuration.addCache(conf); } } //instance the helper ConfigurationHelper helper = new ConfigurationHelper(cm, configuration); //create the default cache cache = helper.createDefaultCache(); //init the default cache.initialise(); cache.bootstrap(); //create the un-init'd caches Set<Cache> caches = helper.createCaches(); if (log.isDebugEnabled()) { log.debug("Number of caches: " + caches.size() + " Default cache: " + (cache != null ? 1 : 0)); } for (Cache nonDefaultCache : caches) { nonDefaultCache.initialise(); nonDefaultCache.bootstrap(); //set first cache to be main local member if (null == nonDefaultCache) { log.debug("Default cache name: {}", defaultCacheName); nonDefaultCache = cm.getCache(defaultCacheName); } } } catch (Exception e) { log.warn("Error on cache init", e); } if (log.isDebugEnabled()) { log.debug("Cache is null? {}", (null == cache)); } }