1 package eu
.etaxonomy
.cdm
.model
;
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
.common
.CdmBase
;
15 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
16 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
19 * Since cdmlib-model cannot access CdmCacher we need to create a mock class
25 public class MockCdmCacher
implements ICdmCacher
{
27 private static final String DEFAULT_CACHE_NAME
= "defaultCache";
29 private static final String DEFAULT_CACHE_MGR_NAME
= "defaultCacheManager";
31 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
34 public MockCdmCacher() {
39 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
40 * sets itself as the cacher object in specific CDM Entity objects.
45 getDefaultCacheManager().removalAll();
46 // Create default cache
47 getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
48 Set
<Language
> langList
= Language
.DEFAULT().getVocabulary().getTerms();
49 for (Language lang
: langList
){
50 put(lang
.getUuid(),lang
);
52 // We start first only with DefinedTermBase
53 DefinedTermBase
.setCacher(this);
58 * Returns the singleton default cache manager.
62 private static CacheManager
getDefaultCacheManager() {
63 // this ensures a singleton cache manager
64 return CacheManager
.create();
68 * Returns the default cache configuration.
72 private CacheConfiguration
getDefaultCacheConfiguration() {
73 return new CacheConfiguration(DEFAULT_CACHE_NAME
, 50)
74 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy
.LFU
)
76 // default ttl and tti set to 2 hours
77 .timeToLiveSeconds(60*60*2)
78 .timeToIdleSeconds(60*60*2)
79 .diskExpiryThreadIntervalSeconds(0)
80 .persistence(new PersistenceConfiguration().strategy(Strategy
.LOCALTEMPSWAP
));
84 * Returns the default cache
88 private static Cache
getDefaultCache() {
89 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME
);
93 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
99 public void put(UUID uuid
, CdmBase cdmEntity
) {
100 getDefaultCache().put(new Element(uuid
, cdmEntity
));
104 * Gets the cache element corresponding to the given {@link java.util.UUID}
109 private Element
getCacheElement(UUID uuid
) {
110 return getDefaultCache().get(uuid
);
114 * Get CDM Entity for given {@link java.util.UUID} from the cache
119 private CdmBase
getCdmEntity(UUID uuid
) {
120 return (CdmBase
)getDefaultCache().get(uuid
).getObjectValue();
124 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
127 public CdmBase
load(UUID uuid
) {
128 Element e
= getCacheElement(uuid
);
133 return (CdmBase
)e
.getObjectValue();
139 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
141 public CdmBase
getFromCache(UUID uuid
) {
142 Element e
= getCacheElement(uuid
);
147 return(CdmBase
)e
.getObjectValue();
153 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
155 public boolean exists(UUID uuid
) {
156 return getCacheElement(uuid
) != null;
161 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
163 public boolean existsAndIsNotNull(UUID uuid
) {
164 Element e
= getCacheElement(uuid
);
167 return (CdmBase
)e
.getObjectValue() != null;