2d5b705f6c31c6eba6b1ca8cca5d0edc6dd5665c
[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.File;
4 import java.io.InputStream;
5
6 import net.sf.ehcache.Cache;
7 import net.sf.ehcache.CacheException;
8 import net.sf.ehcache.CacheManager;
9
10 import org.springframework.core.io.ClassPathResource;
11 import org.springframework.core.io.Resource;
12
13 import eu.etaxonomy.cdm.common.CdmUtils;
14
15
16 public class CdmRemoteCacheManager {
17
18 private CacheManager cdmlibModelCacheManager;
19
20 private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
21
22 public static final Resource CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE =
23 new ClassPathResource("cdmlib-ehcache.xml");
24
25 public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
26
27
28 public enum CdmCacheManagerType {
29 CDMLIB_MODEL,
30 DEFAULT
31 }
32
33 public static CdmRemoteCacheManager getInstance(){
34 if(cdmRemoteCacheManager == null) {
35 cdmRemoteCacheManager = new CdmRemoteCacheManager();
36 }
37 return cdmRemoteCacheManager;
38 }
39 private CdmRemoteCacheManager() {
40
41 System.setProperty("ehcache.disk.store.dir", CdmUtils.getCdmHomeDir().getAbsolutePath() + File.separator + "cdmlib-model");
42 try {
43 // NOTE:Programmatically creating the cache manager may solve the problem of
44 // recreating data written to disk on startup
45 // see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
46 //String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
47 InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
48 cdmlibModelCacheManager = new CacheManager(in);
49
50 } catch (CacheException e) {
51 throw new CdmClientCacheException(e);
52 }
53 // } catch (IOException e) {
54 // throw new CdmClientCacheException(e);
55 // }
56 }
57
58 public Cache getCdmModelGetMethodsCache(){
59 return cdmlibModelCacheManager.getCache(CDM_MODEL_CACHE_NAME);
60 }
61
62 public CacheManager getCdmModelGetMethodsCacheManager() {
63 return cdmlibModelCacheManager;
64 }
65
66 public CacheManager getDefaultCacheManager() {
67 return CacheManager.create();
68 }
69
70 public void shutdown(CdmCacheManagerType ccmt) {
71 CacheManager cm;
72 switch(ccmt) {
73 case CDMLIB_MODEL:
74 cdmlibModelCacheManager.shutdown();
75 break;
76 case DEFAULT:
77 cm = CacheManager.create();
78 cm.shutdown();
79 break;
80 default:
81 //do nothing
82 }
83 }
84
85 public static void checkCacheProperties() {
86 String pathToCache = System.getProperty("ehcache.disk.store.dir");
87 if(pathToCache == null || pathToCache.isEmpty()) {
88 throw new CdmClientCacheException("'ehcache.disk.store.dir' property is not set");
89 }
90 }
91
92 }