1 package eu
.etaxonomy
.cdm
.model
.common
.init
;
6 import net
.sf
.ehcache
.Cache
;
7 import net
.sf
.ehcache
.CacheManager
;
8 import net
.sf
.ehcache
.Element
;
9 import net
.sf
.ehcache
.config
.CacheConfiguration
;
10 import net
.sf
.ehcache
.config
.PersistenceConfiguration
;
11 import net
.sf
.ehcache
.config
.PersistenceConfiguration
.Strategy
;
12 import net
.sf
.ehcache
.store
.MemoryStoreEvictionPolicy
;
14 import eu
.etaxonomy
.cdm
.model
.ICdmCacher
;
15 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
16 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
17 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
20 * Since cdmlib-model cannot access CdmCacher we need to create a mock class
26 public class MockCdmCacher
implements ICdmCacher
{
28 private static final String DEFAULT_CACHE_NAME
= "defaultCache";
30 private static final String DEFAULT_CACHE_MGR_NAME
= "defaultCacheManager";
32 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
35 public MockCdmCacher() {
40 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
41 * sets itself as the cacher object in specific CDM Entity objects.
46 getDefaultCacheManager().removalAll();
47 // Create default cache
48 getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
49 Set
<Language
> langList
= Language
.DEFAULT().getVocabulary().getTerms();
50 for (Language lang
: langList
){
51 put(lang
.getUuid(),lang
);
53 // We start first only with DefinedTermBase
54 DefinedTermBase
.setCacher(this);
59 * Returns the singleton default cache manager.
63 private static CacheManager
getDefaultCacheManager() {
64 // this ensures a singleton cache manager
65 return CacheManager
.create();
69 * Returns the default cache configuration.
73 private CacheConfiguration
getDefaultCacheConfiguration() {
74 return new CacheConfiguration(DEFAULT_CACHE_NAME
, 50)
75 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy
.LFU
)
77 // default ttl and tti set to 2 hours
78 .timeToLiveSeconds(60*60*2)
79 .timeToIdleSeconds(60*60*2)
80 .diskExpiryThreadIntervalSeconds(0)
81 .persistence(new PersistenceConfiguration().strategy(Strategy
.LOCALTEMPSWAP
));
85 * Returns the default cache
89 private static Cache
getDefaultCache() {
90 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME
);
94 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
100 public void put(UUID uuid
, CdmBase cdmEntity
) {
101 getDefaultCache().put(new Element(uuid
, cdmEntity
));
105 * Gets the cache element corresponding to the given {@link java.util.UUID}
110 private Element
getCacheElement(UUID uuid
) {
111 return getDefaultCache().get(uuid
);
115 * Get CDM Entity for given {@link java.util.UUID} from the cache
120 private CdmBase
getCdmEntity(UUID uuid
) {
121 return (CdmBase
)getDefaultCache().get(uuid
).getObjectValue();
125 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
128 public CdmBase
load(UUID uuid
) {
129 Element e
= getCacheElement(uuid
);
134 return (CdmBase
)e
.getObjectValue();
140 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
142 public CdmBase
getFromCache(UUID uuid
) {
143 Element e
= getCacheElement(uuid
);
148 return(CdmBase
)e
.getObjectValue();
154 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
156 public boolean exists(UUID uuid
) {
157 return getCacheElement(uuid
) != null;
162 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
164 public boolean existsAndIsNotNull(UUID uuid
) {
165 Element e
= getCacheElement(uuid
);
168 return (CdmBase
)e
.getObjectValue() != null;