# HG changeset patch # User Sascha L. Teichmann # Date 1304096197 0 # Node ID 7f7d6037d24232e49ec92594ea1c348baf04759a # Parent fc3cf0ef777e819e23d9630ef1bab05d27e71214 Added ehcache support. flys-artifacts/trunk@1782 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r fc3cf0ef777e -r 7f7d6037d242 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Apr 29 15:10:44 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Apr 29 16:56:37 2011 +0000 @@ -1,3 +1,14 @@ +2011-04-29 Sascha L. Teichmann + + * pom.xml: Added dependency to Ehcache. Apache 2.0 license. + + * doc/conf/conf.xml: Added configuration of ehcache. + + * doc/conf/cache.xml: New. Cache configurations. + + * src/main/java/de/intevation/flys/artifacts/cache/CacheFactory.java: + New. Factory to access caches. + 2011-04-29 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java: diff -r fc3cf0ef777e -r 7f7d6037d242 flys-artifacts/doc/conf/cache.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/doc/conf/cache.xml Fri Apr 29 16:56:37 2011 +0000 @@ -0,0 +1,30 @@ + + + + + + + + + + + diff -r fc3cf0ef777e -r 7f7d6037d242 flys-artifacts/doc/conf/conf.xml --- a/flys-artifacts/doc/conf/conf.xml Fri Apr 29 15:10:44 2011 +0000 +++ b/flys-artifacts/doc/conf/conf.xml Fri Apr 29 16:56:37 2011 +0000 @@ -62,6 +62,11 @@ 60000 + + + ${artifacts.config.dir}/cache.xml + + diff -r fc3cf0ef777e -r 7f7d6037d242 flys-artifacts/pom.xml --- a/flys-artifacts/pom.xml Fri Apr 29 15:10:44 2011 +0000 +++ b/flys-artifacts/pom.xml Fri Apr 29 16:56:37 2011 +0000 @@ -30,6 +30,11 @@ + net.sf.ehcache + ehcache-core + 2.4.2 + + jfree jfreechart 1.0.13 diff -r fc3cf0ef777e -r 7f7d6037d242 flys-artifacts/src/main/java/de/intevation/flys/artifacts/cache/CacheFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/cache/CacheFactory.java Fri Apr 29 16:56:37 2011 +0000 @@ -0,0 +1,56 @@ +package de.intevation.flys.artifacts.cache; + +import de.intevation.artifacts.common.utils.Config; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheException; +import net.sf.ehcache.CacheManager; + +import org.apache.log4j.Logger; + +public final class CacheFactory +{ + private static Logger log = Logger.getLogger(CacheFactory.class); + + public static final String XPATH_CACHE_CONFIG_FILE = + "/artifact-database/cache/config-file/text()"; + + private CacheFactory() { + } + + private static boolean initialized; + + private static CacheManager cacheManager; + + public static final Cache getCache() { + return getCache(Cache.DEFAULT_CACHE_NAME); + } + + public static final synchronized Cache getCache(String cacheName) { + if (!initialized) { + initialized = true; // try only once + String configFile = Config.getStringXPath(XPATH_CACHE_CONFIG_FILE); + if (configFile != null) { + configFile = Config.replaceConfigDir(configFile); + try { + cacheManager = CacheManager.create(configFile); + //System.setProperty( + // "net.sf.ehcache.enableShutdownHook", "true"); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + cacheManager.shutdown(); + } + }); + } + catch (CacheException ce) { + log.error("cannot configure cache", ce); + } + } + } + + return cacheManager != null + ? cacheManager.getCache(cacheName) + : null; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :