2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
11 import java
.lang
.reflect
.Field
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.util
.ReflectionUtils
;
21 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
22 import eu
.etaxonomy
.cdm
.model
.ICdmCacher
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
25 import net
.sf
.ehcache
.Cache
;
26 import net
.sf
.ehcache
.Element
;
33 public class CacheLoader
{
34 private static final Logger logger
= Logger
.getLogger(CacheLoader
.class);
36 private static boolean isRecursiveEnabled
= true;
38 protected final ICdmCacher cdmCacher
;
40 private final Cache cdmlibModelCache
;
44 public CacheLoader(ICdmCacher cdmCacher
) {
45 this.cdmCacher
= cdmCacher
;
46 this.cdmlibModelCache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
51 public CdmModelFieldPropertyFromClass
getFromCdmlibModelCache(String className
) {
52 Element e
= cdmlibModelCache
.get(className
);
56 return (CdmModelFieldPropertyFromClass
) e
.getObjectValue();
60 @SuppressWarnings("unchecked")
61 public <T
extends Object
> T
load(T obj
, boolean recursive
, boolean update
) {
65 if(obj
instanceof CdmBase
) {
66 return (T
) load((CdmBase
)obj
, recursive
, update
);
67 } else if (obj
instanceof Map
) {
68 return (T
) load((Map
<T
,T
>)obj
, recursive
, update
);
69 } else if (obj
instanceof Collection
) {
70 return (T
) load((Collection
<T
>)obj
, recursive
, update
);
71 } else if(obj
instanceof Pager
) {
72 load(((Pager
)obj
).getRecords(), recursive
, update
);
74 } else if(obj
instanceof MergeResult
) {
75 return (T
) load((MergeResult
<CdmBase
>)obj
, recursive
, update
);
81 @SuppressWarnings("unchecked")
82 private <T
extends Object
> T
loadRecursive(T obj
, List
<Object
> alreadyVisitedEntities
, boolean update
) {
86 if(obj
instanceof CdmBase
) {
87 return (T
) loadRecursive((CdmBase
)obj
, alreadyVisitedEntities
, update
);
88 } else if (obj
instanceof Map
) {
89 return (T
) load((Map
<T
,T
>)obj
, alreadyVisitedEntities
, update
);
90 } else if (obj
instanceof Collection
) {
91 return (T
) load((Collection
<T
>)obj
, alreadyVisitedEntities
, update
);
92 } else if (obj
instanceof MergeResult
) {
93 return (T
) loadRecursive((MergeResult
)obj
, alreadyVisitedEntities
, update
);
97 logger
.info("No caching yet for type " + obj
.getClass().getName());
102 public <T
extends Object
> Map
<T
,T
> load(Map
<T
,T
> map
, boolean recursive
, boolean update
){
105 if(isRecursiveEnabled
&& recursive
) {
106 logger
.info("---- starting recursive load for cdm entity map");
107 List
<Object
> alreadyVisitedEntities
= new ArrayList
<Object
>();
108 Map
<T
,T
> cachedMap
= load(map
, alreadyVisitedEntities
, update
);
109 alreadyVisitedEntities
.clear();
110 logger
.info("---- ending recursive load for cdm entity map \n");
113 return load(map
, null, update
);
118 private <T
extends Object
> Map
<T
,T
> load(Map
<T
,T
> map
, List
<Object
> alreadyVisitedEntities
, boolean update
){
119 //map = (Map<T,T>)deproxy(map);
121 if(map
== null || map
.isEmpty()) {
125 Object
[] result
= new Object
[ map
.size() * 2 ];
126 Iterator
<Map
.Entry
<T
,T
>> iter
= map
.entrySet().iterator();
128 // to avoid ConcurrentModificationException
129 alreadyVisitedEntities
.add(map
);
130 while ( iter
.hasNext() ) {
131 Map
.Entry
<T
,T
> e
= iter
.next();
132 result
[i
++] = e
.getKey();
133 result
[i
++] = e
.getValue();
136 for(i
=0; i
<result
.length
;i
++) {
137 if(alreadyVisitedEntities
== null) {
138 result
[i
] = load(result
[i
], false, update
);
140 result
[i
] = loadRecursive(result
[i
], alreadyVisitedEntities
, update
);
144 for(i
= 0; i
< result
.length
; i
+=2 ) {
153 public <T
extends Object
> Collection
<T
> load(Collection
<T
> collection
, boolean recursive
, boolean update
){
155 Collection
<T
> loadedCollection
;
156 if(isRecursiveEnabled
&& recursive
) {
157 logger
.info("---- starting recursive load for cdm entity collection");
158 List
<Object
> alreadyVisitedEntities
= new ArrayList
<Object
>();
159 Collection
<T
> cachedCollection
= load(collection
, alreadyVisitedEntities
, update
);
160 alreadyVisitedEntities
.clear();
161 logger
.info("---- ending recursive load for cdm entity collection \n");
162 loadedCollection
= cachedCollection
;
164 loadedCollection
= load(collection
, null, update
);
166 return loadedCollection
;
169 @SuppressWarnings("unchecked")
170 private <T
extends Object
> Collection
<T
> load(Collection
<T
> collection
, List
<Object
> alreadyVisitedEntities
, boolean update
) {
174 if(collection
== null || collection
.isEmpty()) {
177 int length
= collection
.size();
178 Object
[] result
= new Object
[length
];
179 Iterator
<T
> collectionItr
= collection
.iterator();
181 // to avoid ConcurrentModificationException
182 alreadyVisitedEntities
.add(collection
);
183 while(collectionItr
.hasNext()) {
184 Object obj
= collectionItr
.next();
185 if(alreadyVisitedEntities
== null) {
186 result
[count
] = load(obj
, false, update
);
188 result
[count
] = loadRecursive(obj
, alreadyVisitedEntities
, update
);
196 for ( int i
= 0; i
< length
; i
++ ) {
197 collection
.add((T
)result
[i
]);
204 public MergeResult
<CdmBase
> load(MergeResult
<CdmBase
> mergeResult
, boolean recursive
, boolean update
) {
205 CdmBase cdmBase
= load(mergeResult
.getMergedEntity(), recursive
, update
);
206 load(mergeResult
.getNewEntities(), recursive
, update
);
207 return new MergeResult(cdmBase
, mergeResult
.getNewEntities());
210 public MergeResult
<CdmBase
> loadRecursive(MergeResult
<CdmBase
> mergeResult
,List
<Object
> alreadyVisitedEntities
, boolean update
) {
211 CdmBase cdmBase
= loadRecursive(mergeResult
.getMergedEntity(), alreadyVisitedEntities
, update
);
212 loadRecursive(mergeResult
.getNewEntities(), alreadyVisitedEntities
, update
);
213 return new MergeResult(cdmBase
, mergeResult
.getNewEntities());
217 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
218 * in the cache corresponding to the given cache id
224 public CdmBase
load(CdmBase cdmEntity
, boolean recursive
, boolean update
) {
225 if(cdmEntity
== null) {
229 // start by looking up the cdm entity in the cache
230 CdmBase cachedCdmEntity
= cdmCacher
.getFromCache(cdmEntity
);
232 if(cachedCdmEntity
!= null) {
233 // if cdm entity was found in cache then
234 logger
.info(" - object of type " + cdmEntity
.getClass().getName() + " with id " + cdmEntity
.getId() + " already exists");
235 // .. return if the cached and input objects are identical, else (this is a newly loaded object so) continue
236 if(cachedCdmEntity
== cdmEntity
) {
237 return cachedCdmEntity
;
241 CdmBase loadedCdmBase
;
242 if(isRecursiveEnabled
&& recursive
) {
243 logger
.info("---- starting recursive load for cdm entity " + cdmEntity
.getClass().getName() + " with id " + cdmEntity
.getId());
244 List
<Object
> alreadyVisitedEntities
= new ArrayList
<Object
>();
245 CdmBase cb
= loadRecursive(cdmEntity
, alreadyVisitedEntities
, update
);
246 alreadyVisitedEntities
.clear();
247 logger
.info("---- ending recursive load for cdm entity " + cdmEntity
.getClass().getName() + " with id " + cdmEntity
.getId() + "\n");
250 loadedCdmBase
= load(cdmEntity
);
252 return loadedCdmBase
;
257 protected CdmBase
load(CdmBase cdmEntity
) {
258 logger
.info("loading object of type " + cdmEntity
.getClass().getName() + " with id " + cdmEntity
.getId());
259 cdmCacher
.put((CdmBase
)ProxyUtils
.deproxy(cdmEntity
));
260 return cdmCacher
.getFromCache(cdmEntity
);
264 private CdmBase
loadRecursive(CdmBase cdmEntity
, List
<Object
> alreadyVisitedEntities
, boolean update
) {
266 CdmBase cachedCdmEntity
= load(cdmEntity
);
269 // we want to recursive through the cdmEntity (and not the cachedCdmEntity)
270 // since there could be new or deleted objects in the cdmEntity sub-graph
272 // start by getting the fields from the cdm entity
273 String className
= cdmEntity
.getClass().getName();
274 CdmModelFieldPropertyFromClass cmgmfc
= getFromCdmlibModelCache(className
);
276 alreadyVisitedEntities
.add(cdmEntity
);
277 List
<String
> fields
= cmgmfc
.getFields();
278 for(String field
: fields
) {
279 // retrieve the actual object corresponding to the field.
280 // this object will be either a CdmBase or a Collection / Map
281 // with CdmBase as the generic type
283 CdmBase cdmEntityInSubGraph
= getCdmBaseTypeFieldValue(cdmEntity
, cachedCdmEntity
, field
, alreadyVisitedEntities
, update
);
284 if(cdmEntityInSubGraph
!= null) {
285 //checkForIdenticalCdmEntity(alreadyVisitedEntities, cdmEntityInSubGraph);
286 if(!checkForIdenticalCdmEntity(alreadyVisitedEntities
, cdmEntityInSubGraph
)) {
287 logger
.info("recursive loading object of type " + cdmEntityInSubGraph
.getClass().getName() + " with id " + cdmEntityInSubGraph
.getId());
288 loadRecursive(cdmEntityInSubGraph
, alreadyVisitedEntities
, update
);
290 logger
.info("object of type " + cdmEntityInSubGraph
.getClass().getName() + " with id " + cdmEntityInSubGraph
.getId() + " already visited");
295 throw new CdmClientCacheException("CdmEntity with class " + cdmEntity
.getClass().getName() + " is not found in the cdmlib model cache. " +
296 "The cache may be corrupted or not in sync with the latest model version" );
299 return cachedCdmEntity
;
303 private CdmBase
getCdmBaseTypeFieldValue(CdmBase cdmEntity
,
304 CdmBase cachedCdmEntity
,
306 List
<Object
> alreadyVisitedEntities
,
309 // this method attempts to make sure that for any two objects found in
310 // the object graph, if they are equal then they should also be the same,
311 // which is crucial for the merge to work
312 if(cachedCdmEntity
== null) {
313 throw new CdmClientCacheException("When trying to set field value, the cached cdm entity cannot be null");
316 Class
<?
> clazz
= cdmEntity
.getClass();
318 // this call will search in the provided class as well as
319 // the super classes until it finds the field
320 Field field
= ReflectionUtils
.findField(clazz
, fieldName
);
323 throw new CdmClientCacheException("Field '" + fieldName
324 + "' not found when searching in class '" + clazz
.getName() + "' and its superclasses");
326 field
.setAccessible(true);
327 Object o
= field
.get(cdmEntity
);
328 // resetting the value in cdm entity to the deproxied object
329 o
= ProxyUtils
.deproxy(o
);
330 field
.set(cdmEntity
, o
);
331 Object cachedo
= field
.get(cachedCdmEntity
);
332 CdmBase cdmEntityInSubGraph
= null;
334 if(!ProxyUtils
.isProxy(o
) && (update
|| ProxyUtils
.isProxy(cachedo
))) {
335 // if we are in update mode we have to make the field of the cached entity
336 // up-to-date by setting it to the value of the cdm entity being loaded
338 // if the cdm entity is a proxy then we always update to make sure that
339 // newly created entities are always up-to-date
341 // NOTE : the field is overridden in the case of the exception
343 field
.set(cachedCdmEntity
, o
);
347 if(o
!= null && !ProxyUtils
.isProxy(o
)) {
348 if(CdmBase
.class.isAssignableFrom(o
.getClass())) {
349 logger
.info("found initialised cdm entity '" + fieldName
+ "' in object of type " + clazz
.getName() + " with id " + cdmEntity
.getId());
351 cdmEntityInSubGraph
= (CdmBase
)o
;
352 CdmBase cachedCdmEntityInSubGraph
= cdmCacher
.getFromCache(cdmEntityInSubGraph
);
354 if(cachedCdmEntityInSubGraph
!= null) {
355 if(cachedCdmEntityInSubGraph
!= cdmEntityInSubGraph
) {
356 // exception : is the case where
357 // the field has been already initialised, cached and
358 // is not the same as the one in the cache, in which case we set the value
359 // of the field to the one found in the cache
360 logger
.info("setting cached + real value to '" + fieldName
+ "' in object of type " + clazz
.getName() + " with id " + cdmEntity
.getId());
361 field
.set(cachedCdmEntity
, cachedCdmEntityInSubGraph
);
362 field
.set(cdmEntity
, cachedCdmEntityInSubGraph
);
364 // since the field value object in cdmEntity
365 // is the same as the field value object in cachedCdmEntity
366 // we are sure that the its subgraph is also correctly loaded,
367 // so we can exit the recursion
371 } else if(o
instanceof Map
&& !checkForIdenticalCdmEntity(alreadyVisitedEntities
, o
)) {
372 loadRecursive((Map
)o
, alreadyVisitedEntities
, update
);
373 } else if(o
instanceof Collection
&& !checkForIdenticalCdmEntity(alreadyVisitedEntities
, o
)) {
374 loadRecursive((Collection
)o
, alreadyVisitedEntities
, update
);
377 // we return the original cdm entity in the sub graph because we
378 // want to continue to recurse on the input cdm entity graph
379 // and not the one in the cache
380 return cdmEntityInSubGraph
;
381 } catch (SecurityException e
) {
382 throw new CdmClientCacheException(e
);
383 } catch (IllegalArgumentException e
) {
384 throw new CdmClientCacheException(e
);
385 } catch (IllegalAccessException e
) {
386 throw new CdmClientCacheException(e
);
390 private boolean checkForIdenticalCdmEntity(List
<Object
> objList
, Object objToCompare
) {
391 if(objToCompare
!= null) {
392 for(Object obj
: objList
) {
393 if(obj
== objToCompare
) {
402 public static boolean isRecursiveEnabled() {
403 return isRecursiveEnabled
;
406 public static void setRecursiveEnabled(boolean ire
) {
407 isRecursiveEnabled
= ire
;