1
|
package eu.etaxonomy.taxeditor.remoting.cache;
|
2
|
|
3
|
import net.sf.ehcache.Cache;
|
4
|
import net.sf.ehcache.CacheException;
|
5
|
import net.sf.ehcache.CacheManager;
|
6
|
import net.sf.ehcache.config.CacheConfiguration;
|
7
|
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
|
11
|
|
12
|
public class CdmRemoteCacheManager {
|
13
|
|
14
|
private static final Logger logger = Logger.getLogger(CdmRemoteCacheManager.class);
|
15
|
|
16
|
|
17
|
private Cache cdmlibModelCache;
|
18
|
|
19
|
private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
|
20
|
|
21
|
public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
|
22
|
|
23
|
private static Thread initThread;
|
24
|
|
25
|
private static boolean cacheInitialised = false;
|
26
|
|
27
|
public enum CdmCacheManagerType {
|
28
|
CDMLIB_MODEL,
|
29
|
DEFAULT
|
30
|
}
|
31
|
|
32
|
public static CdmRemoteCacheManager getInstance(){
|
33
|
|
34
|
if(cdmRemoteCacheManager == null) {
|
35
|
cdmRemoteCacheManager = new CdmRemoteCacheManager();
|
36
|
}
|
37
|
return cdmRemoteCacheManager;
|
38
|
}
|
39
|
private CdmRemoteCacheManager() {
|
40
|
|
41
|
|
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
|
|
49
|
SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
|
50
|
sizeOfConfig.setMaxDepth(1000);
|
51
|
sizeOfConfig.setMaxDepthExceededBehavior("abort");
|
52
|
|
53
|
CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
|
54
|
.eternal(true)
|
55
|
.statistics(true)
|
56
|
.sizeOfPolicy(sizeOfConfig)
|
57
|
.overflowToOffHeap(false);
|
58
|
|
59
|
cdmlibModelCache = new Cache(modelcc);
|
60
|
|
61
|
CacheManager.create().addCache(cdmlibModelCache);
|
62
|
|
63
|
initCache(cdmlibModelCache);
|
64
|
|
65
|
} catch (CacheException e) {
|
66
|
throw new CdmClientCacheException(e);
|
67
|
}
|
68
|
|
69
|
}
|
70
|
|
71
|
private void initCache(final Cache cache) {
|
72
|
|
73
|
initThread = new Thread() {
|
74
|
@Override
|
75
|
public void run(){
|
76
|
synchronized (cdmlibModelCache) {
|
77
|
CdmModelCacher cmdmc = new CdmModelCacher();
|
78
|
cmdmc.cacheGetterFields(cache);
|
79
|
cacheInitialised = true;
|
80
|
logger.info("Initialisation of CDM getter fields complete");
|
81
|
cdmlibModelCache.notify();
|
82
|
}
|
83
|
}
|
84
|
|
85
|
};
|
86
|
initThread.start();
|
87
|
}
|
88
|
|
89
|
public Cache getCdmModelGetMethodsCache(){
|
90
|
synchronized (cdmlibModelCache) {
|
91
|
while(!cacheInitialised) {
|
92
|
try {
|
93
|
logger.info("Waiting for initialisation of CDM getter fields to complete ...");
|
94
|
cdmlibModelCache.wait();
|
95
|
} catch (InterruptedException e) {}
|
96
|
}
|
97
|
}
|
98
|
logger.info("CDM getter fields cache initialised");
|
99
|
return cdmlibModelCache;
|
100
|
}
|
101
|
|
102
|
public static void removeEntityCaches() {
|
103
|
CacheManager cm = CacheManager.create();
|
104
|
String[] cacheNames = CacheManager.create().getCacheNames();
|
105
|
for(String cacheName : cacheNames) {
|
106
|
if(!cacheName.equals(CDM_MODEL_CACHE_NAME)) {
|
107
|
cm.removeCache(cacheName);
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
|
113
|
|
114
|
}
|