1 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
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
;
9 import org
.apache
.log4j
.Logger
;
12 public class CdmRemoteCacheManager
{
14 private static final Logger logger
= Logger
.getLogger(CdmRemoteCacheManager
.class);
17 private Cache cdmlibModelCache
;
19 private static CdmRemoteCacheManager cdmRemoteCacheManager
= null;
21 public static final String CDM_MODEL_CACHE_NAME
= "cdmModelGetMethodsCache";
23 private static Thread initThread
;
25 private static boolean cacheInitialised
= false;
27 public enum CdmCacheManagerType
{
32 public static CdmRemoteCacheManager
getInstance(){
34 if(cdmRemoteCacheManager
== null) {
35 cdmRemoteCacheManager
= new CdmRemoteCacheManager();
37 return cdmRemoteCacheManager
;
39 private CdmRemoteCacheManager() {
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");
49 SizeOfPolicyConfiguration sizeOfConfig
= new SizeOfPolicyConfiguration();
50 sizeOfConfig
.setMaxDepth(1000);
51 sizeOfConfig
.setMaxDepthExceededBehavior("abort");
53 CacheConfiguration modelcc
= new CacheConfiguration(CDM_MODEL_CACHE_NAME
, 0)
56 .sizeOfPolicy(sizeOfConfig
)
57 .overflowToOffHeap(false);
59 cdmlibModelCache
= new Cache(modelcc
);
61 CacheManager
.create().addCache(cdmlibModelCache
);
62 initCdmModelCache(cdmlibModelCache
);
64 } catch (CacheException e
) {
65 throw new CdmClientCacheException(e
);
70 private void initCdmModelCache(final Cache cache
) {
72 initThread
= new Thread() {
75 synchronized (cdmlibModelCache
) {
76 CdmModelCacher cmdmc
= new CdmModelCacher();
77 cmdmc
.cacheGetterFields(cache
);
78 cacheInitialised
= true;
79 logger
.info("Initialisation of CDM getter fields complete");
80 cdmlibModelCache
.notify();
88 public Cache
getCdmModelGetMethodsCache(){
89 //Note : Even though we synchronize this method, the cache can be simply
90 // retrieved using CacheManager.create().getCache(CDM_MODEL_CACHE_NAME)
91 // in which case the cache may not be fully initialised
92 synchronized (cdmlibModelCache
) {
93 while(!cacheInitialised
) {
95 logger
.info("Waiting for initialisation of CDM getter fields to complete ...");
96 cdmlibModelCache
.wait();
97 } catch (InterruptedException e
) {}
100 logger
.info("CDM getter fields cache initialised");
101 return cdmlibModelCache
;
104 public static void removeEntityCaches() {
105 CacheManager cm
= CacheManager
.create();
106 String
[] cacheNames
= CacheManager
.create().getCacheNames();
107 for(String cacheName
: cacheNames
) {
108 if(!cacheName
.equals(CDM_MODEL_CACHE_NAME
)) {
109 cm
.removeCache(cacheName
);