1 package eu
.etaxonomy
.cdm
.api
.cache
;
5 import net
.sf
.ehcache
.Cache
;
6 import net
.sf
.ehcache
.CacheManager
;
7 import net
.sf
.ehcache
.Element
;
8 import net
.sf
.ehcache
.config
.CacheConfiguration
;
9 import net
.sf
.ehcache
.store
.MemoryStoreEvictionPolicy
;
11 import eu
.etaxonomy
.cdm
.model
.ICdmCacher
;
12 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
13 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
14 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
17 * Since cdmlib-services cannot access CdmCacher we need to create a mock class
19 * See comment in {@link eu.etaxonomy.cdm.api.service.TaxonServiceSearchTest} for
20 * why we need a mock cdm cacher class
23 * - All terms are put into the cache in the constructor
24 * - The number of elements allowed in the cache is set to a big number - 10000
26 * FIXME : Once the CDMCacher is externalised this class should just subclass it.
31 public class MockCdmCacher
implements ICdmCacher
{
33 private static final String DEFAULT_CACHE_NAME
= "defaultCache";
35 private static final String DEFAULT_CACHE_MGR_NAME
= "defaultCacheManager";
37 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
40 public MockCdmCacher() {
45 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
46 * sets itself as the cacher object in specific CDM Entity objects.
51 getDefaultCacheManager().removalAll();
52 // Create default cache
53 getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
54 put(Language
.DEFAULT().getUuid(),Language
.DEFAULT());
55 // We start first only with DefinedTermBase
56 DefinedTermBase
.setCacher(this);
61 * Returns the singleton default cache manager.
65 private static CacheManager
getDefaultCacheManager() {
66 // this ensures a singleton cache manager
67 return CacheManager
.create();
71 * Returns the default cache configuration.
75 private CacheConfiguration
getDefaultCacheConfiguration() {
77 return new CacheConfiguration(DEFAULT_CACHE_NAME
, 10000)
78 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy
.LFU
)
80 // default ttl and tti set to 2 hours
81 .timeToLiveSeconds(60*60*2)
82 .timeToIdleSeconds(60*60*2);
86 * Returns the default cache
90 private static Cache
getDefaultCache() {
91 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME
);
95 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
101 public void put(UUID uuid
, CdmBase cdmEntity
) {
102 getDefaultCache().put(new Element(uuid
, cdmEntity
));
106 * Gets the cache element corresponding to the given {@link java.util.UUID}
111 private Element
getCacheElement(UUID uuid
) {
112 return getDefaultCache().get(uuid
);
116 * Get CDM Entity for given {@link java.util.UUID} from the cache
121 private CdmBase
getCdmEntity(UUID uuid
) {
122 return (CdmBase
)getDefaultCache().get(uuid
).getObjectValue();
126 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
129 public CdmBase
load(UUID uuid
) {
130 Element e
= getCacheElement(uuid
);
135 return (CdmBase
)e
.getObjectValue();
141 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
143 public CdmBase
getFromCache(UUID uuid
) {
144 Element e
= getCacheElement(uuid
);
149 return(CdmBase
)e
.getObjectValue();
155 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
157 public boolean exists(UUID uuid
) {
158 return getCacheElement(uuid
) != null;
163 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
165 public boolean existsAndIsNotNull(UUID uuid
) {
166 Element e
= getCacheElement(uuid
);
169 return (CdmBase
)e
.getObjectValue() != null;