a2c4d1c663961a885a222b89d35475889267db9e
[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.IOException;
4 import java.net.URISyntaxException;
5
6 import net.sf.ehcache.Cache;
7 import net.sf.ehcache.CacheException;
8 import net.sf.ehcache.CacheManager;
9 import net.sf.ehcache.config.CacheConfiguration;
10 import net.sf.ehcache.config.SizeOfPolicyConfiguration;
11
12 import org.apache.log4j.Logger;
13
14
15 public class CdmRemoteCacheManager {
16
17 private static final Logger logger = Logger.getLogger(CdmRemoteCacheManager.class);
18
19
20 private Cache cdmlibModelCache;
21
22 private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
23
24 public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
25
26 private static Thread initThread;
27
28 private static boolean cacheInitialised = false;
29
30 public enum CdmCacheManagerType {
31 CDMLIB_MODEL,
32 DEFAULT
33 }
34
35 public static CdmRemoteCacheManager getInstance(){
36
37 if(cdmRemoteCacheManager == null) {
38 cdmRemoteCacheManager = new CdmRemoteCacheManager();
39 }
40 return cdmRemoteCacheManager;
41 }
42 private CdmRemoteCacheManager() {
43
44
45 try {
46 // NOTE:Programmatically creating the cache manager may solve the problem of
47 // recreating data written to disk on startup
48 // see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
49 //String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
50 //InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
51
52 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
53 sizeOfConfig.setMaxDepth(1000);
54 sizeOfConfig.setMaxDepthExceededBehavior("abort");
55
56 CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
57 .eternal(true)
58 .statistics(true)
59 .sizeOfPolicy(sizeOfConfig)
60 .overflowToOffHeap(false);
61
62 cdmlibModelCache = new Cache(modelcc);
63
64 CacheManager.create().addCache(cdmlibModelCache);
65 CdmModelCacher cmdmc = new CdmModelCacher();
66 cmdmc.cacheGetterFields(cdmlibModelCache);
67
68 } catch (CacheException e) {
69 throw new CdmClientCacheException(e);
70 } catch (ClassNotFoundException e) {
71 throw new CdmClientCacheException(e);
72 } catch (IOException e) {
73 throw new CdmClientCacheException(e);
74 } catch (URISyntaxException e) {
75 throw new CdmClientCacheException(e);
76 }
77
78 }
79
80
81 public Cache getCdmModelGetMethodsCache(){
82 return cdmlibModelCache;
83 }
84
85 public static void removeEntityCaches() {
86 CacheManager cm = CacheManager.create();
87 String[] cacheNames = CacheManager.create().getCacheNames();
88 for(String cacheName : cacheNames) {
89 if(!cacheName.equals(CDM_MODEL_CACHE_NAME)) {
90 cm.removeCache(cacheName);
91 }
92 }
93 }
94 }