CacheInitializer.java from Texai at Krugle
Show CacheInitializer.java syntax highlighted
/*
* CacheInitializer.java
*
* Created on January 30, 2007, 9:44 AM
*
* Description: Provides a static method to initialize the system caches.
*
* Copyright (C) 2007 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.kb;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import net.jcip.annotations.ThreadSafe;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
/** This class has static methods that initialize the ehcache caches used by the semantic annotation persistence framework.
* The Constants class contains a list of cache names and initializeCaches() method iterates over those. New caches may be added without
* modifying this class.
*
* @author reed
*/
@ThreadSafe
public final class CacheInitializer {
/** indicates whether the caches have been initialized */
private static boolean areCachesInitialized = false;
/** the cache access lock */
private static Lock cacheAccessLock = new ReentrantLock();
/** Creates a new instance of CacheInitializer. */
protected CacheInitializer() {
super();
}
/** Resets the system caches. */
public static void resetCaches() {
cacheAccessLock.lock();
final CacheManager cacheManager = CacheManager.getInstance();
for (final String namedCache : Constants.NAMED_CACHES) {
if (cacheManager.cacheExists(namedCache)) {
final Cache cache = cacheManager.getCache(namedCache);
cache.removeAll();
cache.clearStatistics();
} else {
cacheManager.addCache(namedCache);
}
}
cacheAccessLock.unlock();
}
/** Initializes the system caches if not yet done. */
public static void initializeCaches() {
cacheAccessLock.lock();
if (!areCachesInitialized) {
areCachesInitialized = true;
for (final String namedCache : Constants.NAMED_CACHES) {
initializeCache(namedCache);
}
}
cacheAccessLock.unlock();
}
/**
* Resets the given cache.
*
* @param namedCache the name of the given cache
*/
public static void resetCache(final String namedCache) {
//preconditions
assert namedCache != null : "namedCache must not be null";
assert !namedCache.isEmpty() : "namedCache must not be empty";
cacheAccessLock.lock();
final CacheManager cacheManager = CacheManager.getInstance();
if (cacheManager.cacheExists(namedCache)) {
final Cache cache = cacheManager.getCache(namedCache);
cache.removeAll();
cache.clearStatistics();
} else {
cacheManager.addCache(namedCache);
}
cacheAccessLock.unlock();
}
/** Initializes the given cache.
*
* @param namedCache the name of the given cache
*/
private static void initializeCache(final String namedCache) {
//preconditions
assert namedCache != null : "namedCache must not be null";
assert !namedCache.isEmpty() : "namedCache must not be empty";
CacheManager cacheManager = CacheManager.getInstance();
if (!cacheManager.cacheExists(namedCache)) {
cacheManager.addCache(namedCache);
}
}
}
See more files for this project here