01: package com.technoetic.xplanner.db.hibernate;
02:
03: import net.sf.ehcache.Cache;
04: import net.sf.ehcache.CacheException;
05: import net.sf.ehcache.CacheManager;
06: import net.sf.hibernate.cache.QueryCache;
07: import net.sf.hibernate.cache.UpdateTimestampsCache;
08: import net.sf.hibernate.cfg.Configuration;
09: import net.sf.hibernate.mapping.PersistentClass;
10: import org.apache.log4j.Logger;
11:
12: import java.util.Iterator;
13:
14: public class EHCacheHelper {
15: private static Logger log = Logger.getLogger(EHCacheHelper.class);
16: private static final int DEFAULT_MAX_CACHE_SIZE = 1000;
17: private static final boolean DEFAULT_OVERFLOW_TO_DISK = false;
18: private static boolean DEFAULT_ETERNAL = false;
19: private static final int DEFAULT_TIME_TO_LIVE = 120;
20: private static final int DEFAULT_TIME_TO_IDLE = 120;
21:
22: public static void configure(Configuration hibernateConfig) {
23: Iterator classMappings = hibernateConfig.getClassMappings();
24: try {
25: while (classMappings.hasNext()) {
26: PersistentClass persistentClass = (PersistentClass) classMappings
27: .next();
28: configureClassCache(persistentClass.getMappedClass());
29: }
30: configureClassCache(QueryCache.class);
31: configureClassCache(UpdateTimestampsCache.class);
32: } catch (Exception e) {
33: log.error("error", e);
34: }
35: }
36:
37: private static void configureClassCache(Class theClass)
38: throws CacheException {
39: String name = theClass.getName();
40: if (!CacheManager.getInstance().cacheExists(name)) {
41: Cache ehcache = new Cache(name, DEFAULT_MAX_CACHE_SIZE,
42: DEFAULT_OVERFLOW_TO_DISK, DEFAULT_ETERNAL,
43: DEFAULT_TIME_TO_LIVE, DEFAULT_TIME_TO_IDLE);
44: CacheManager.getInstance().addCache(ehcache);
45: }
46: }
47: }
|