1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.remoting.cache;
|
11
|
|
12
|
import java.lang.reflect.Field;
|
13
|
import java.util.Collection;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Iterator;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import net.sf.ehcache.Cache;
|
21
|
import net.sf.ehcache.Element;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.hibernate.proxy.HibernateProxy;
|
25
|
import org.hibernate.proxy.LazyInitializer;
|
26
|
import org.springframework.util.ReflectionUtils;
|
27
|
|
28
|
import eu.etaxonomy.cdm.model.ICdmCacher;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
|
31
|
/**
|
32
|
* @author cmathew
|
33
|
* @date 19 Feb 2015
|
34
|
*
|
35
|
*/
|
36
|
public class CacheLoader {
|
37
|
private static final Logger logger = Logger.getLogger(CacheLoader.class);
|
38
|
|
39
|
private static boolean isRecursiveEnabled = true;
|
40
|
|
41
|
protected final ICdmCacher cdmCacher;
|
42
|
|
43
|
private final Cache cdmlibModelCache;
|
44
|
|
45
|
|
46
|
|
47
|
public CacheLoader(ICdmCacher cdmCacher) {
|
48
|
this.cdmCacher = cdmCacher;
|
49
|
this.cdmlibModelCache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
|
50
|
|
51
|
}
|
52
|
|
53
|
|
54
|
public CdmModelFieldPropertyFromClass getFromCdmlibModelCache(String className) {
|
55
|
Element e = cdmlibModelCache.get(className);
|
56
|
if (e == null) {
|
57
|
return null;
|
58
|
} else {
|
59
|
return (CdmModelFieldPropertyFromClass) e.getObjectValue();
|
60
|
}
|
61
|
}
|
62
|
|
63
|
@SuppressWarnings("unchecked")
|
64
|
public <T extends Object> T load(T obj, boolean recursive, boolean update) {
|
65
|
if(obj == null) {
|
66
|
return null;
|
67
|
}
|
68
|
if(obj instanceof CdmBase) {
|
69
|
return (T) load((CdmBase)obj, recursive, update);
|
70
|
} else if (obj instanceof Map) {
|
71
|
return (T) load((Map<T,T>)obj, recursive, update);
|
72
|
} else if (obj instanceof Collection) {
|
73
|
return (T) load((Collection<T>)obj, recursive, update);
|
74
|
}
|
75
|
|
76
|
return obj;
|
77
|
}
|
78
|
|
79
|
@SuppressWarnings("unchecked")
|
80
|
private <T extends Object> T loadRecursive(T obj, Set<Object> alreadyVisitedEntities, boolean update) {
|
81
|
if(obj == null) {
|
82
|
return null;
|
83
|
}
|
84
|
if(obj instanceof CdmBase) {
|
85
|
return (T) loadRecursive((CdmBase)obj, alreadyVisitedEntities, update);
|
86
|
} else if (obj instanceof Map) {
|
87
|
return (T) load((Map<T,T>)obj, alreadyVisitedEntities, update);
|
88
|
} else if (obj instanceof Collection) {
|
89
|
return (T) load((Collection<T>)obj, alreadyVisitedEntities, update);
|
90
|
}
|
91
|
|
92
|
|
93
|
logger.info("No caching yet for type " + obj.getClass().getName());
|
94
|
|
95
|
return obj;
|
96
|
}
|
97
|
|
98
|
public <T extends Object> Map<T,T> load(Map<T,T> map, boolean recursive, boolean update){
|
99
|
|
100
|
|
101
|
if(isRecursiveEnabled && recursive) {
|
102
|
logger.info("---- starting recursive load for cdm entity map");
|
103
|
Set<Object> alreadyVisitedEntities = new HashSet<Object>();
|
104
|
Map<T,T> cachedMap = load(map, alreadyVisitedEntities, update);
|
105
|
alreadyVisitedEntities.clear();
|
106
|
logger.info("---- ending recursive load for cdm entity map \n");
|
107
|
return cachedMap;
|
108
|
} else {
|
109
|
return load(map, null, update);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
|
114
|
private <T extends Object> Map<T,T> load(Map<T,T> map, Set<Object> alreadyVisitedEntities, boolean update){
|
115
|
//map = (Map<T,T>)deproxy(map);
|
116
|
|
117
|
if(map == null || map.isEmpty()) {
|
118
|
return map;
|
119
|
}
|
120
|
|
121
|
int originalMapSize = map.size();
|
122
|
Object[] result = new Object[ map.size() * 2 ];
|
123
|
Iterator<Map.Entry<T,T>> iter = map.entrySet().iterator();
|
124
|
int i=0;
|
125
|
// to avoid ConcurrentModificationException
|
126
|
alreadyVisitedEntities.add(map);
|
127
|
while ( iter.hasNext() ) {
|
128
|
Map.Entry<T,T> e = iter.next();
|
129
|
result[i++] = e.getKey();
|
130
|
result[i++] = e.getValue();
|
131
|
}
|
132
|
|
133
|
for(i=0; i<result.length;i++) {
|
134
|
if(alreadyVisitedEntities == null) {
|
135
|
result[i] = load(result[i], false, update);
|
136
|
} else {
|
137
|
result[i] = loadRecursive(result[i], alreadyVisitedEntities, update);
|
138
|
}
|
139
|
}
|
140
|
map.clear();
|
141
|
for(i = 0; i < originalMapSize; i+=2 ) {
|
142
|
map.put(
|
143
|
(T)result[i],
|
144
|
(T)result[i+1]
|
145
|
);
|
146
|
}
|
147
|
return map;
|
148
|
}
|
149
|
|
150
|
public <T extends Object> Collection<T> load(Collection<T> collection, boolean recursive, boolean update){
|
151
|
|
152
|
Collection<T> loadedCollection;
|
153
|
if(isRecursiveEnabled && recursive) {
|
154
|
logger.info("---- starting recursive load for cdm entity collection");
|
155
|
Set<Object> alreadyVisitedEntities = new HashSet<Object>();
|
156
|
Collection<T> cachedCollection = load(collection, alreadyVisitedEntities, update);
|
157
|
alreadyVisitedEntities.clear();
|
158
|
logger.info("---- ending recursive load for cdm entity collection \n");
|
159
|
loadedCollection = cachedCollection;
|
160
|
} else {
|
161
|
loadedCollection = load(collection, null, update);
|
162
|
}
|
163
|
return loadedCollection;
|
164
|
}
|
165
|
|
166
|
@SuppressWarnings("unchecked")
|
167
|
private <T extends Object> Collection<T> load(Collection<T> collection, Set<Object> alreadyVisitedEntities, boolean update) {
|
168
|
|
169
|
|
170
|
|
171
|
if(collection == null || collection.isEmpty()) {
|
172
|
return collection;
|
173
|
}
|
174
|
int length = collection.size();
|
175
|
Object[] result = new Object[length];
|
176
|
Iterator<T> collectionItr = collection.iterator();
|
177
|
int count = 0;
|
178
|
// to avoid ConcurrentModificationException
|
179
|
alreadyVisitedEntities.add(collection);
|
180
|
while(collectionItr.hasNext()) {
|
181
|
Object obj = collectionItr.next();
|
182
|
if(alreadyVisitedEntities == null) {
|
183
|
result[count] = load(obj, false, update);
|
184
|
} else {
|
185
|
result[count] = loadRecursive(obj, alreadyVisitedEntities, update);
|
186
|
}
|
187
|
|
188
|
count++;
|
189
|
}
|
190
|
|
191
|
collection.clear();
|
192
|
|
193
|
for ( int i = 0; i < length; i++ ) {
|
194
|
collection.add((T)result[i]);
|
195
|
}
|
196
|
|
197
|
return collection;
|
198
|
}
|
199
|
|
200
|
|
201
|
/**
|
202
|
* Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
|
203
|
* in the cache corresponding to the given cache id
|
204
|
*
|
205
|
* @param cacheId
|
206
|
* @param uuid
|
207
|
* @param cdmEntity
|
208
|
*/
|
209
|
public CdmBase load(CdmBase cdmEntity, boolean recursive, boolean update) {
|
210
|
if(cdmEntity == null) {
|
211
|
return null;
|
212
|
}
|
213
|
|
214
|
// start by looking up the cdm entity in the cache
|
215
|
CdmBase cachedCdmEntity = cdmCacher.getFromCache(cdmEntity);
|
216
|
|
217
|
if(cachedCdmEntity != null) {
|
218
|
// if cdm entity was found in cache then
|
219
|
logger.info(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " already exists");
|
220
|
// .. return if the cached and input objects are identical, else (this is a newly loaded object so) continue
|
221
|
if(cachedCdmEntity == cdmEntity) {
|
222
|
return cachedCdmEntity;
|
223
|
}
|
224
|
}
|
225
|
|
226
|
CdmBase loadedCdmBase;
|
227
|
if(isRecursiveEnabled && recursive) {
|
228
|
logger.info("---- starting recursive load for cdm entity " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId());
|
229
|
Set<Object> alreadyVisitedEntities = new HashSet<Object>();
|
230
|
CdmBase cb = loadRecursive(cdmEntity, alreadyVisitedEntities, update);
|
231
|
alreadyVisitedEntities.clear();
|
232
|
logger.info("---- ending recursive load for cdm entity " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + "\n");
|
233
|
loadedCdmBase = cb;
|
234
|
} else {
|
235
|
loadedCdmBase = load(cdmEntity);
|
236
|
}
|
237
|
return loadedCdmBase;
|
238
|
|
239
|
}
|
240
|
|
241
|
|
242
|
protected CdmBase load(CdmBase cdmEntity) {
|
243
|
logger.info("loading object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId());
|
244
|
|
245
|
cdmEntity = (CdmBase)ProxyUtils.deproxy(cdmEntity);
|
246
|
|
247
|
cdmCacher.put(cdmEntity);
|
248
|
|
249
|
return cdmCacher.getFromCache(cdmEntity);
|
250
|
|
251
|
}
|
252
|
|
253
|
|
254
|
private CdmBase loadRecursive(CdmBase cdmEntity, Set<Object> alreadyVisitedEntities, boolean update) {
|
255
|
|
256
|
CdmBase cachedCdmEntity = load(cdmEntity);
|
257
|
|
258
|
|
259
|
// we want to recursive through the cdmEntity (and not the cachedCdmEntity)
|
260
|
// since there could be new or deleted objects in the cdmEntity sub-graph
|
261
|
|
262
|
// start by getting the fields from the cdm entity
|
263
|
String className = cdmEntity.getClass().getName();
|
264
|
CdmModelFieldPropertyFromClass cmgmfc = getFromCdmlibModelCache(className);
|
265
|
if(cmgmfc != null) {
|
266
|
alreadyVisitedEntities.add(cdmEntity);
|
267
|
List<String> fields = cmgmfc.getFields();
|
268
|
for(String field : fields) {
|
269
|
// retrieve the actual object corresponding to the field.
|
270
|
// this object will be either a CdmBase or a Collection / Map
|
271
|
// with CdmBase as the generic type
|
272
|
|
273
|
CdmBase cdmEntityInSubGraph = getCdmBaseTypeFieldValue(cdmEntity, cachedCdmEntity, field, alreadyVisitedEntities, update);
|
274
|
if(cdmEntityInSubGraph != null) {
|
275
|
//checkForIdenticalCdmEntity(alreadyVisitedEntities, cdmEntityInSubGraph);
|
276
|
if(!alreadyVisitedEntities.contains(cdmEntityInSubGraph)) {
|
277
|
logger.info("recursive loading object of type " + cdmEntityInSubGraph.getClass().getName() + " with id " + cdmEntityInSubGraph.getId());
|
278
|
loadRecursive(cdmEntityInSubGraph, alreadyVisitedEntities, update);
|
279
|
} else {
|
280
|
logger.info("object of type " + cdmEntityInSubGraph.getClass().getName() + " with id " + cdmEntityInSubGraph.getId() + " already visited");
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
} else {
|
285
|
throw new CdmClientCacheException("CdmEntity with class " + cdmEntity.getClass().getName() + " is not found in the cdmlib model cache. " +
|
286
|
"The cache may be corrupted or not in sync with the latest model version" );
|
287
|
}
|
288
|
|
289
|
return cachedCdmEntity;
|
290
|
}
|
291
|
|
292
|
|
293
|
private CdmBase getCdmBaseTypeFieldValue(CdmBase cdmEntity,
|
294
|
CdmBase cachedCdmEntity,
|
295
|
String fieldName,
|
296
|
Set<Object> alreadyVisitedEntities,
|
297
|
boolean update) {
|
298
|
|
299
|
// this method attempts to make sure that for any two objects found in
|
300
|
// the object graph, if they are equal then they should also be the same,
|
301
|
// which is crucial for the merge to work
|
302
|
if(cachedCdmEntity == null) {
|
303
|
throw new CdmClientCacheException("When trying to set field value, the cached cdm entity cannot be null");
|
304
|
}
|
305
|
|
306
|
Class<?> clazz = cdmEntity.getClass();
|
307
|
try {
|
308
|
// this call will search in the provided class as well as
|
309
|
// the super classes until it finds the field
|
310
|
Field field = ReflectionUtils.findField(clazz, fieldName);
|
311
|
|
312
|
if(field == null) {
|
313
|
throw new CdmClientCacheException("Field '" + fieldName
|
314
|
+ "' not found when searching in class '" + clazz.getName() + "' and its supercalsses");
|
315
|
}
|
316
|
field.setAccessible(true);
|
317
|
Object o = field.get(cdmEntity);
|
318
|
o = ProxyUtils.deproxy(o);
|
319
|
field.set(cdmEntity, o);
|
320
|
|
321
|
CdmBase cdmEntityInSubGraph = null;
|
322
|
if(update) {
|
323
|
// if we are in update mode we have to make the field of the cached entity
|
324
|
// up-to-date by setting it to the value of the cdm entity being loaded
|
325
|
// NOTE : two exceptions to this are found below
|
326
|
field.set(cachedCdmEntity, o);
|
327
|
|
328
|
}
|
329
|
if(o != null && !ProxyUtils.isProxy(o)) {
|
330
|
if(CdmBase.class.isAssignableFrom(o.getClass())) {
|
331
|
logger.info("found initialised cdm entity '" + fieldName + "' in object of type " + clazz.getName() + " with id " + cdmEntity.getId());
|
332
|
cdmEntityInSubGraph = (CdmBase)o;
|
333
|
CdmBase cachedCdmEntityInSubGraph = cdmCacher.getFromCache(cdmEntityInSubGraph);
|
334
|
|
335
|
Object oldCachedCdmEntityInSubGraph = field.get(cachedCdmEntity);
|
336
|
if(ProxyUtils.isProxy(oldCachedCdmEntityInSubGraph)) {
|
337
|
LazyInitializer hli =
|
338
|
((HibernateProxy)oldCachedCdmEntityInSubGraph).getHibernateLazyInitializer();
|
339
|
|
340
|
if(cdmEntityInSubGraph.getId() == ((Integer)hli.getIdentifier()).intValue()) {
|
341
|
// exception 1 : is the case where
|
342
|
// the earlier value of the field in the cached entity
|
343
|
// was a proxy with the same id then we don't need to
|
344
|
// update it here as it will be updated on demand,
|
345
|
// so we reset it to the earlier proxy
|
346
|
field.set(cachedCdmEntity, oldCachedCdmEntityInSubGraph);
|
347
|
return null;
|
348
|
}
|
349
|
}
|
350
|
|
351
|
if(cachedCdmEntityInSubGraph != null) {
|
352
|
if(cachedCdmEntityInSubGraph != cdmEntityInSubGraph) {
|
353
|
// exception 2 : is the case where
|
354
|
// the field has been already initialised, cached and
|
355
|
// is not the same as the one in the cache, in which case we set the value
|
356
|
// of the field to the one found in the cache
|
357
|
logger.info("setting cached + real value to '" + fieldName + "' in object of type " + clazz.getName() + " with id " + cdmEntity.getId());
|
358
|
field.set(cachedCdmEntity, cachedCdmEntityInSubGraph);
|
359
|
field.set(cdmEntity, cachedCdmEntityInSubGraph);
|
360
|
} else {
|
361
|
// since the field value object in cdmEntity
|
362
|
// is the same as the field value object in cachedCdmEntity
|
363
|
// we are sure that the its subgraph is also correctly loaded,
|
364
|
// so we can exit the recursion
|
365
|
return null;
|
366
|
}
|
367
|
}
|
368
|
} else if(o instanceof Map && !alreadyVisitedEntities.contains(o)) {
|
369
|
loadRecursive((Map)o, alreadyVisitedEntities, update);
|
370
|
} else if(o instanceof Collection && !alreadyVisitedEntities.contains(o)) {
|
371
|
loadRecursive((Collection)o, alreadyVisitedEntities, update);
|
372
|
}
|
373
|
}
|
374
|
// we return the original cdm entity in the sub graph because we
|
375
|
// want to continue to recurse on the input cdm entity graph
|
376
|
// and not the one in the cache
|
377
|
return cdmEntityInSubGraph;
|
378
|
} catch (SecurityException e) {
|
379
|
throw new CdmClientCacheException(e);
|
380
|
} catch (IllegalArgumentException e) {
|
381
|
throw new CdmClientCacheException(e);
|
382
|
} catch (IllegalAccessException e) {
|
383
|
throw new CdmClientCacheException(e);
|
384
|
}
|
385
|
}
|
386
|
|
387
|
private boolean checkForIdenticalCdmEntity(Set<CdmBase> cbSet, CdmBase cbToCompare) {
|
388
|
if(cbToCompare != null) {
|
389
|
for(CdmBase cb : cbSet) {
|
390
|
|
391
|
if(cb == cbToCompare) {
|
392
|
return true;
|
393
|
} else {
|
394
|
if(cb.equals(cbToCompare)) {
|
395
|
logger.info("equal but non identical object found of type " + cbToCompare.getUserFriendlyTypeName() + " with id " + cbToCompare.getId());
|
396
|
}
|
397
|
}
|
398
|
}
|
399
|
}
|
400
|
return false;
|
401
|
}
|
402
|
|
403
|
|
404
|
public static boolean isRecursiveEnabled() {
|
405
|
return isRecursiveEnabled;
|
406
|
}
|
407
|
|
408
|
public static void setRecursiveEnabled(boolean ire) {
|
409
|
isRecursiveEnabled = ire;
|
410
|
}
|
411
|
}
|