e8162c4607c7ed33d9b058c8d49331af44c2d8e3
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / init / MockCdmCacher.java
1 package eu.etaxonomy.cdm.model.common.init;
2
3 import java.util.Set;
4 import java.util.UUID;
5
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;
11
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;
16
17 /**
18 * Since cdmlib-model cannot access CdmCacher we need to create a mock class
19 * for the tests
20 *
21 * @author cmathew
22 *
23 */
24 public class MockCdmCacher implements ICdmCacher {
25
26 private static final String DEFAULT_CACHE_NAME = "defaultCache";
27
28 private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
29 /**
30 * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
31 *
32 */
33 public MockCdmCacher() {
34 init();
35 }
36
37 /**
38 * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
39 * sets itself as the cacher object in specific CDM Entity objects.
40 *
41 */
42 private void init() {
43 // Remove all caches
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);
50 }
51 // We start first only with DefinedTermBase
52 DefinedTermBase.setCacher(this);
53
54 }
55
56 /**
57 * Returns the singleton default cache manager.
58 *
59 * @return
60 */
61 private static CacheManager getDefaultCacheManager() {
62 // this ensures a singleton cache manager
63 return CacheManager.create();
64 }
65
66 /**
67 * Returns the default cache configuration.
68 *
69 * @return
70 */
71 private CacheConfiguration getDefaultCacheConfiguration() {
72 return new CacheConfiguration(DEFAULT_CACHE_NAME, 50)
73 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
74 .eternal(false)
75 // default ttl and tti set to 2 hours
76 .timeToLiveSeconds(60*60*2)
77 .timeToIdleSeconds(60*60*2);
78
79 }
80
81 /**
82 * Returns the default cache
83 *
84 * @return
85 */
86 private static Cache getDefaultCache() {
87 return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
88 }
89
90 /**
91 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
92 * in the cache
93 *
94 * @param uuid
95 * @param cdmEntity
96 */
97 public void put(UUID uuid, CdmBase cdmEntity) {
98 getDefaultCache().put(new Element(uuid, cdmEntity));
99 }
100
101 /**
102 * Gets the cache element corresponding to the given {@link java.util.UUID}
103 *
104 * @param uuid
105 * @return
106 */
107 private Element getCacheElement(UUID uuid) {
108 return getDefaultCache().get(uuid);
109 }
110
111 /**
112 * Get CDM Entity for given {@link java.util.UUID} from the cache
113 *
114 * @param uuid
115 * @return
116 */
117 private CdmBase getCdmEntity(UUID uuid) {
118 return (CdmBase)getDefaultCache().get(uuid).getObjectValue();
119 }
120
121 /* (non-Javadoc)
122 * @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
123 */
124
125 public CdmBase load(UUID uuid) {
126 Element e = getCacheElement(uuid);
127 CdmBase cdmEntity;
128 if (e == null) {
129 return null;
130 } else {
131 return (CdmBase)e.getObjectValue();
132 }
133 }
134
135
136 /* (non-Javadoc)
137 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
138 */
139 public CdmBase getFromCache(UUID uuid) {
140 Element e = getCacheElement(uuid);
141 CdmBase cdmEntity;
142 if (e == null) {
143 return null;
144 } else {
145 return(CdmBase)e.getObjectValue();
146 }
147 }
148
149
150 /* (non-Javadoc)
151 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
152 */
153 public boolean exists(UUID uuid) {
154 return getCacheElement(uuid) != null;
155 }
156
157
158 /* (non-Javadoc)
159 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
160 */
161 public boolean existsAndIsNotNull(UUID uuid) {
162 Element e = getCacheElement(uuid);
163 CdmBase cdmEntity;
164 if (e != null) {
165 return (CdmBase)e.getObjectValue() != null;
166 }
167 return false;
168 }
169
170 }