1 package eu
.etaxonomy
.cdm
.api
.lazyloading
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
5 import java
.util
.HashSet
;
9 import java
.util
.TreeMap
;
10 import java
.util
.TreeSet
;
12 import org
.aspectj
.lang
.ProceedingJoinPoint
;
13 import org
.aspectj
.lang
.annotation
.Around
;
14 import org
.aspectj
.lang
.annotation
.Aspect
;
15 import org
.aspectj
.lang
.annotation
.Pointcut
;
16 import org
.hibernate
.collection
.internal
.PersistentList
;
17 import org
.hibernate
.collection
.internal
.PersistentMap
;
18 import org
.hibernate
.collection
.internal
.PersistentSet
;
19 import org
.hibernate
.collection
.internal
.PersistentSortedMap
;
20 import org
.hibernate
.collection
.internal
.PersistentSortedSet
;
21 import org
.hibernate
.collection
.spi
.PersistentCollection
;
22 import org
.hibernate
.proxy
.LazyInitializer
;
23 import org
.springframework
.beans
.factory
.annotation
.Autowire
;
24 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
25 import org
.springframework
.beans
.factory
.annotation
.Configurable
;
26 import org
.springframework
.stereotype
.Component
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
35 @Configurable(autowire
= Autowire
.BY_TYPE
)
36 public class CdmLazyLoader
{
39 private final Set classes
= new HashSet();
41 public static boolean enableWeaving
= true;
42 private static Set
<String
> classesToIgnore
= new HashSet
<String
>();
46 private ICommonService commonService
;
48 public CdmLazyLoader() {
49 //classesToIgnore.add("eu.etaxonomy.cdm.model.common.TermVocabulary");
50 //classesToIgnore.add("eu.etaxonomy.cdm.model.common.OrderedTermVocabulary");
55 * Point cut for the 'initialize' method of the AbstractLazyInitializer.
58 @Pointcut("execution(* org.hibernate.proxy.AbstractLazyInitializer.initialize())")
59 public void possibleEntityLazyInitializationException() {
64 * 'Around' advice for the initialization of CDM Entity Objects
67 @Around(value
= "possibleEntityLazyInitializationException()")
68 public Object
preloadEntityOnDemand(ProceedingJoinPoint pjp
) throws Throwable
{
70 LazyInitializer ll
= (LazyInitializer
)pjp
.getTarget();
71 if(ll
.isUninitialized()) {
72 int classid
= ((Integer
)ll
.getIdentifier()).intValue();
73 System
.out
.print("--> AspectJ Compile-Time Weaving " + ll
.getEntityName() + " with id " + classid
);
74 Class clazz
= Class
.forName(ll
.getEntityName());
75 CdmBase cdmBase
= CdmBase
.deproxy(commonService
.find(clazz
,classid
),clazz
);
76 ll
.setImplementation(cdmBase
);
77 System
.out
.println("....Done");
85 * Point cut for the 'initialize' method of the AbstractPersistentCollection.
88 @Pointcut("execution(protected final void org.hibernate.collection.internal.AbstractPersistentCollection.initialize(..))")
89 public void possibleCollectionLazyInitializationException() {
93 * 'Around' advice for the initialization of Collection objects
96 @Around(value
= "possibleCollectionLazyInitializationException()")
97 public Object
preloadCollectionOnDemand(ProceedingJoinPoint pjp
) throws Throwable
{
99 PersistentCollection ps
= (PersistentCollection
) pjp
.getTarget();
100 if (ps
.getOwner() != null && !classesToIgnore
.contains(ps
.getOwner().getClass().getName()) && !ps
.wasInitialized() && !classes
.contains(ps
.getKey())) {
101 System
.out
.print("--> AspectJCompile-Time Weaving " + ps
.getRole());
102 classes
.add(ps
.getKey());
104 String role
= ps
.getRole();
105 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
106 System
.out
.print(", field : " + fieldName
);
107 Object owner
= ps
.getOwner();
109 Object col
= commonService
.initializeCollection(((CdmBase
)owner
).getUuid(), fieldName
);
110 ps
.afterInitialize();
112 // Class<?> clazz = ps.getClass();
113 // if (clazz != null) {
114 // CollectionField cf = getCollectionField(col);
115 // Field field = clazz.getDeclaredField(cf.getFieldName());
116 // field.setAccessible(true);
117 // field.set(ps, cf.getCollection());
120 } catch (Exception ex
) {
121 ex
.printStackTrace();
122 System
.out
.println("Error in ReattachSessionAspect : " + ex
.getMessage());
124 classes
.remove(ps
.getKey());
125 System
.out
.println("....Done");
126 throw new Exception("This code is invalid");
130 return pjp
.proceed();
134 private CollectionField
getCollectionField(PersistentCollection pc
) {
136 if(pc
instanceof PersistentSet
) {
137 return new CollectionField(new HashSet((Set
)pc
), "set");
139 if(pc
instanceof PersistentSortedSet
) {
140 return new CollectionField(new TreeSet((Set
)pc
), "set");
142 if(pc
instanceof PersistentList
) {
143 return new CollectionField(new ArrayList((List
)pc
), "list");
145 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
146 return new CollectionField(new HashMap((Map
)pc
), "map");
148 if(pc
instanceof PersistentSortedMap
) {
149 return new CollectionField(new TreeMap((Map
)pc
), "map");
155 private String
getCollectionFieldName(PersistentCollection pc
) {
157 if(pc
instanceof PersistentSet
|| pc
instanceof PersistentSortedSet
) {
160 if(pc
instanceof PersistentList
) {
163 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
170 private class CollectionField
{
171 private final Object col
;
172 private final String fieldName
;
173 public CollectionField(Object col
, String fieldName
) {
175 this.fieldName
= fieldName
;
178 public Object
getCollection() {
182 public String
getFieldName() {
183 return this.fieldName
;