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