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
|
* NOTES:
|
22
|
* - All terms are put into the cache in the constructor
|
23
|
* - The number of elements allowed in the cache is set to a big number - 10000
|
24
|
*
|
25
|
* FIXME : Once the CDMCacher is externalised this class should just subclass it.
|
26
|
*
|
27
|
* @author cmathew
|
28
|
*
|
29
|
*/
|
30
|
public class MockCdmCacher implements ICdmCacher {
|
31
|
|
32
|
private static final String DEFAULT_CACHE_NAME = "defaultCache";
|
33
|
|
34
|
private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
|
35
|
/**
|
36
|
* Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
|
37
|
*
|
38
|
*/
|
39
|
public MockCdmCacher() {
|
40
|
init();
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
|
45
|
* sets itself as the cacher object in specific CDM Entity objects.
|
46
|
*
|
47
|
*/
|
48
|
private void init() {
|
49
|
// Remove all caches
|
50
|
getDefaultCacheManager().removalAll();
|
51
|
// Create default cache
|
52
|
getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
|
53
|
Set<Language> langList = Language.DEFAULT().getVocabulary().getTerms();
|
54
|
for (Language lang : langList){
|
55
|
put(lang.getUuid(),lang);
|
56
|
}
|
57
|
// We start first only with DefinedTermBase
|
58
|
DefinedTermBase.setCacher(this);
|
59
|
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Returns the singleton default cache manager.
|
64
|
*
|
65
|
* @return
|
66
|
*/
|
67
|
private static CacheManager getDefaultCacheManager() {
|
68
|
// this ensures a singleton cache manager
|
69
|
return CacheManager.create();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Returns the default cache configuration.
|
74
|
*
|
75
|
* @return
|
76
|
*/
|
77
|
private CacheConfiguration getDefaultCacheConfiguration() {
|
78
|
return new CacheConfiguration(DEFAULT_CACHE_NAME, 10000)
|
79
|
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
|
80
|
.eternal(false)
|
81
|
// default ttl and tti set to 2 hours
|
82
|
.timeToLiveSeconds(60*60*2)
|
83
|
.timeToIdleSeconds(60*60*2);
|
84
|
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Returns the default cache
|
89
|
*
|
90
|
* @return
|
91
|
*/
|
92
|
private static Cache getDefaultCache() {
|
93
|
return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
|
98
|
* in the cache
|
99
|
*
|
100
|
* @param uuid
|
101
|
* @param cdmEntity
|
102
|
* @return
|
103
|
*/
|
104
|
@Override
|
105
|
public CdmBase put(UUID uuid, CdmBase cdmEntity) {
|
106
|
CdmBase cachedCdmEntity = getFromCache(uuid);
|
107
|
if(getFromCache(uuid) == null) {
|
108
|
getDefaultCache().put(new Element(uuid, cdmEntity));
|
109
|
return cdmEntity;
|
110
|
} else {
|
111
|
return cachedCdmEntity;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Gets the cache element corresponding to the given {@link java.util.UUID}
|
117
|
*
|
118
|
* @param uuid
|
119
|
* @return
|
120
|
*/
|
121
|
private Element getCacheElement(UUID uuid) {
|
122
|
return getDefaultCache().get(uuid);
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Get CDM Entity for given {@link java.util.UUID} from the cache
|
127
|
*
|
128
|
* @param uuid
|
129
|
* @return
|
130
|
*/
|
131
|
private CdmBase getCdmEntity(UUID uuid) {
|
132
|
return (CdmBase)getDefaultCache().get(uuid).getObjectValue();
|
133
|
}
|
134
|
|
135
|
/* (non-Javadoc)
|
136
|
* @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
|
137
|
*/
|
138
|
|
139
|
@Override
|
140
|
public CdmBase load(UUID uuid) {
|
141
|
Element e = getCacheElement(uuid);
|
142
|
CdmBase cdmEntity;
|
143
|
if (e == null) {
|
144
|
return null;
|
145
|
} else {
|
146
|
return (CdmBase)e.getObjectValue();
|
147
|
}
|
148
|
}
|
149
|
|
150
|
|
151
|
/* (non-Javadoc)
|
152
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
|
153
|
*/
|
154
|
@Override
|
155
|
public CdmBase getFromCache(UUID uuid) {
|
156
|
Element e = getCacheElement(uuid);
|
157
|
CdmBase cdmEntity;
|
158
|
if (e == null) {
|
159
|
return null;
|
160
|
} else {
|
161
|
return(CdmBase)e.getObjectValue();
|
162
|
}
|
163
|
}
|
164
|
|
165
|
|
166
|
/* (non-Javadoc)
|
167
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
|
168
|
*/
|
169
|
@Override
|
170
|
public boolean exists(UUID uuid) {
|
171
|
return getCacheElement(uuid) != null;
|
172
|
}
|
173
|
|
174
|
|
175
|
/* (non-Javadoc)
|
176
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
|
177
|
*/
|
178
|
@Override
|
179
|
public boolean existsAndIsNotNull(UUID uuid) {
|
180
|
Element e = getCacheElement(uuid);
|
181
|
CdmBase cdmEntity;
|
182
|
if (e != null) {
|
183
|
return (CdmBase)e.getObjectValue() != null;
|
184
|
}
|
185
|
return false;
|
186
|
}
|
187
|
|
188
|
}
|