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