CdmApplicationRemoteController : refactoring code to simplify application context...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / CdmRemoteCacheManager.java
1 package eu.etaxonomy.taxeditor.remoting.cache;
2
3 import java.io.InputStream;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import net.sf.ehcache.Cache;
8 import net.sf.ehcache.CacheException;
9 import net.sf.ehcache.CacheManager;
10 import net.sf.ehcache.config.CacheConfiguration;
11 import net.sf.ehcache.config.SizeOfPolicyConfiguration;
12
13 import org.springframework.core.io.ClassPathResource;
14 import org.springframework.core.io.Resource;
15
16
17 public class CdmRemoteCacheManager {
18
19 private CacheManager cdmlibModelCacheManager;
20
21 private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
22
23 private final Set<CdmTransientEntityCacher> transientEntityCachers = new HashSet<CdmTransientEntityCacher>();
24
25 public static final Resource CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE =
26 new ClassPathResource("cdmlib-ehcache.xml");
27
28 public static final String CDM_MODEL_CACHE_MGR_NAME = "cdmlibModelCacheManager";
29 public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
30
31
32 public enum CdmCacheManagerType {
33 CDMLIB_MODEL,
34 DEFAULT
35 }
36
37 public static CdmRemoteCacheManager getInstance(){
38 if(cdmRemoteCacheManager == null) {
39 cdmRemoteCacheManager = new CdmRemoteCacheManager();
40 }
41 return cdmRemoteCacheManager;
42 }
43 private CdmRemoteCacheManager() {
44
45
46 try {
47 // NOTE:Programmatically creating the cache manager may solve the problem of
48 // recreating data written to disk on startup
49 // see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
50 //String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
51 InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
52
53 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
54 sizeOfConfig.setMaxDepth(1000);
55 sizeOfConfig.setMaxDepthExceededBehavior("abort");
56
57 CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
58 .eternal(true)
59 .statistics(true)
60 .sizeOfPolicy(sizeOfConfig)
61 .overflowToOffHeap(false);
62
63 Cache modelCache = new Cache(modelcc);
64
65 cdmlibModelCacheManager = CacheManager.create(CDM_MODEL_CACHE_MGR_NAME);
66 cdmlibModelCacheManager.addCache(modelCache);
67
68 } catch (CacheException e) {
69 throw new CdmClientCacheException(e);
70 }
71
72 }
73
74 public Cache getCdmModelGetMethodsCache(){
75 return cdmlibModelCacheManager.getCache(CDM_MODEL_CACHE_NAME);
76 }
77
78 public CacheManager getCdmModelGetMethodsCacheManager() {
79 return cdmlibModelCacheManager;
80 }
81
82 public CacheManager getDefaultCacheManager() {
83 return CacheManager.create();
84 }
85
86 public void shutdown(CdmCacheManagerType ccmt) {
87 CacheManager cm;
88 switch(ccmt) {
89 case CDMLIB_MODEL:
90 cdmlibModelCacheManager.shutdown();
91 break;
92 case DEFAULT:
93 cm = CacheManager.create();
94 cm.shutdown();
95 break;
96 default:
97 //do nothing
98 }
99 }
100
101
102
103 }