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
.store
.MemoryStoreEvictionPolicy
;
12 import eu
.etaxonomy
.cdm
.model
.ICdmCacher
;
13 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
14 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
15 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
18 * Since cdmlib-model cannot access CdmCacher we need to create a mock class
24 public class MockCdmCacher
implements ICdmCacher
{
26 private static final String DEFAULT_CACHE_NAME
= "defaultCache";
28 private static final String DEFAULT_CACHE_MGR_NAME
= "defaultCacheManager";
30 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
33 public MockCdmCacher() {
38 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
39 * sets itself as the cacher object in specific CDM Entity objects.
44 getDefaultCacheManager().removalAll();
45 // Create default cache
46 getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
47 Set
<Language
> langList
= Language
.DEFAULT().getVocabulary().getTerms();
48 for (Language lang
: langList
){
49 put(lang
.getUuid(),lang
);
51 // We start first only with DefinedTermBase
52 DefinedTermBase
.setCacher(this);
57 * Returns the singleton default cache manager.
61 private static CacheManager
getDefaultCacheManager() {
62 // this ensures a singleton cache manager
63 return CacheManager
.create();
67 * Returns the default cache configuration.
71 private CacheConfiguration
getDefaultCacheConfiguration() {
72 return new CacheConfiguration(DEFAULT_CACHE_NAME
, 50)
73 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy
.LFU
)
75 // default ttl and tti set to 2 hours
76 .timeToLiveSeconds(60*60*2)
77 .timeToIdleSeconds(60*60*2);
82 * Returns the default cache
86 private static Cache
getDefaultCache() {
87 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME
);
91 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
97 public void put(UUID uuid
, CdmBase cdmEntity
) {
98 getDefaultCache().put(new Element(uuid
, cdmEntity
));
102 * Gets the cache element corresponding to the given {@link java.util.UUID}
107 private Element
getCacheElement(UUID uuid
) {
108 return getDefaultCache().get(uuid
);
112 * Get CDM Entity for given {@link java.util.UUID} from the cache
117 private CdmBase
getCdmEntity(UUID uuid
) {
118 return (CdmBase
)getDefaultCache().get(uuid
).getObjectValue();
122 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
125 public CdmBase
load(UUID uuid
) {
126 Element e
= getCacheElement(uuid
);
131 return (CdmBase
)e
.getObjectValue();
137 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
139 public CdmBase
getFromCache(UUID uuid
) {
140 Element e
= getCacheElement(uuid
);
145 return(CdmBase
)e
.getObjectValue();
151 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
153 public boolean exists(UUID uuid
) {
154 return getCacheElement(uuid
) != null;
159 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
161 public boolean existsAndIsNotNull(UUID uuid
) {
162 Element e
= getCacheElement(uuid
);
165 return (CdmBase
)e
.getObjectValue() != null;