1
|
package eu.etaxonomy.taxeditor.remoting.cache;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.InputStream;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.Set;
|
7
|
|
8
|
import net.sf.ehcache.Cache;
|
9
|
import net.sf.ehcache.CacheException;
|
10
|
import net.sf.ehcache.CacheManager;
|
11
|
|
12
|
import org.springframework.core.io.ClassPathResource;
|
13
|
import org.springframework.core.io.Resource;
|
14
|
|
15
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
16
|
|
17
|
|
18
|
public class CdmRemoteCacheManager {
|
19
|
|
20
|
private CacheManager cdmlibModelCacheManager;
|
21
|
|
22
|
private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
|
23
|
|
24
|
private final Set<CdmTransientEntityCacher> transientEntityCachers = new HashSet<CdmTransientEntityCacher>();
|
25
|
|
26
|
public static final Resource CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE =
|
27
|
new ClassPathResource("cdmlib-ehcache.xml");
|
28
|
|
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
|
System.setProperty("ehcache.disk.store.dir", CdmUtils.getCdmHomeDir().getAbsolutePath() + File.separator + "cdmlib-model");
|
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
|
cdmlibModelCacheManager = new CacheManager(in);
|
53
|
|
54
|
} catch (CacheException e) {
|
55
|
throw new CdmClientCacheException(e);
|
56
|
}
|
57
|
// } catch (IOException e) {
|
58
|
// throw new CdmClientCacheException(e);
|
59
|
// }
|
60
|
}
|
61
|
|
62
|
public Cache getCdmModelGetMethodsCache(){
|
63
|
return cdmlibModelCacheManager.getCache(CDM_MODEL_CACHE_NAME);
|
64
|
}
|
65
|
|
66
|
public CacheManager getCdmModelGetMethodsCacheManager() {
|
67
|
return cdmlibModelCacheManager;
|
68
|
}
|
69
|
|
70
|
public CacheManager getDefaultCacheManager() {
|
71
|
return CacheManager.create();
|
72
|
}
|
73
|
|
74
|
public void shutdown(CdmCacheManagerType ccmt) {
|
75
|
CacheManager cm;
|
76
|
switch(ccmt) {
|
77
|
case CDMLIB_MODEL:
|
78
|
cdmlibModelCacheManager.shutdown();
|
79
|
break;
|
80
|
case DEFAULT:
|
81
|
cm = CacheManager.create();
|
82
|
cm.shutdown();
|
83
|
break;
|
84
|
default:
|
85
|
//do nothing
|
86
|
}
|
87
|
}
|
88
|
|
89
|
public static void checkCacheProperties() {
|
90
|
String pathToCache = System.getProperty("ehcache.disk.store.dir");
|
91
|
if(pathToCache == null || pathToCache.isEmpty()) {
|
92
|
throw new CdmClientCacheException("'ehcache.disk.store.dir' property is not set");
|
93
|
}
|
94
|
}
|
95
|
|
96
|
|
97
|
}
|