1 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
3 import java
.io
.IOException
;
4 import java
.net
.URISyntaxException
;
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
;
12 import org
.apache
.log4j
.Logger
;
15 public class CdmRemoteCacheManager
{
17 private static final Logger logger
= Logger
.getLogger(CdmRemoteCacheManager
.class);
20 private Cache cdmlibModelCache
;
22 private static CdmRemoteCacheManager cdmRemoteCacheManager
= null;
24 public static final String CDM_MODEL_CACHE_NAME
= "cdmModelGetMethodsCache";
26 private static Thread initThread
;
28 private static boolean cacheInitialised
= false;
30 public enum CdmCacheManagerType
{
35 public static CdmRemoteCacheManager
getInstance(){
37 if(cdmRemoteCacheManager
== null) {
38 cdmRemoteCacheManager
= new CdmRemoteCacheManager();
40 return cdmRemoteCacheManager
;
42 private CdmRemoteCacheManager() {
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");
52 SizeOfPolicyConfiguration sizeOfConfig
= new SizeOfPolicyConfiguration();
53 sizeOfConfig
.setMaxDepth(1000);
54 sizeOfConfig
.setMaxDepthExceededBehavior("abort");
56 CacheConfiguration modelcc
= new CacheConfiguration(CDM_MODEL_CACHE_NAME
, 0)
59 .sizeOfPolicy(sizeOfConfig
)
60 .overflowToOffHeap(false);
62 cdmlibModelCache
= new Cache(modelcc
);
64 CacheManager
.create().addCache(cdmlibModelCache
);
65 CdmModelCacher cmdmc
= new CdmModelCacher();
66 cmdmc
.cacheGetterFields(cdmlibModelCache
);
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
);
81 public Cache
getCdmModelGetMethodsCache(){
82 return cdmlibModelCache
;
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
);