1
|
package eu.etaxonomy.cdm.api.lazyloading;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.Set;
|
9
|
import java.util.TreeMap;
|
10
|
import java.util.TreeSet;
|
11
|
|
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;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
|
31
|
|
32
|
|
33
|
@Aspect
|
34
|
@Component
|
35
|
@Configurable(autowire = Autowire.BY_TYPE)
|
36
|
public class CdmLazyLoader {
|
37
|
|
38
|
private final Set classes = new HashSet<>();
|
39
|
|
40
|
public static boolean enableWeaving = true;
|
41
|
private static Set<String> classesToIgnore = new HashSet<>();
|
42
|
|
43
|
|
44
|
@Autowired
|
45
|
private ICommonService commonService;
|
46
|
|
47
|
public CdmLazyLoader() {
|
48
|
//classesToIgnore.add("eu.etaxonomy.cdm.model.term.TermVocabulary");
|
49
|
//classesToIgnore.add("eu.etaxonomy.cdm.model.term.OrderedTermVocabulary");
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Point cut for the 'initialize' method of the AbstractLazyInitializer.
|
54
|
*/
|
55
|
@Pointcut("execution(* org.hibernate.proxy.AbstractLazyInitializer.initialize())")
|
56
|
public void possibleEntityLazyInitializationException() {
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* 'Around' advice for the initialization of CDM Entity Objects
|
61
|
*
|
62
|
*/
|
63
|
@Around(value = "possibleEntityLazyInitializationException()")
|
64
|
public Object preloadEntityOnDemand(ProceedingJoinPoint pjp) throws Throwable {
|
65
|
if(enableWeaving) {
|
66
|
LazyInitializer ll = (LazyInitializer)pjp.getTarget();
|
67
|
if(ll.isUninitialized()) {
|
68
|
int classid = ((Integer)ll.getIdentifier()).intValue();
|
69
|
System.out.print("--> AspectJ Compile-Time Weaving " + ll.getEntityName() + " with id " + classid);
|
70
|
Class clazz = Class.forName(ll.getEntityName());
|
71
|
CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz);
|
72
|
ll.setImplementation(cdmBase);
|
73
|
System.out.println("....Done");
|
74
|
}
|
75
|
}
|
76
|
return pjp.proceed();
|
77
|
}
|
78
|
|
79
|
|
80
|
/**
|
81
|
* Point cut for the 'initialize' method of the AbstractPersistentCollection.
|
82
|
*/
|
83
|
@Pointcut("execution(protected final void org.hibernate.collection.internal.AbstractPersistentCollection.initialize(..))")
|
84
|
public void possibleCollectionLazyInitializationException() {
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* 'Around' advice for the initialization of Collection objects
|
89
|
*
|
90
|
*/
|
91
|
@Around(value = "possibleCollectionLazyInitializationException()")
|
92
|
public Object preloadCollectionOnDemand(ProceedingJoinPoint pjp) throws Throwable {
|
93
|
if(enableWeaving) {
|
94
|
PersistentCollection ps = (PersistentCollection) pjp.getTarget();
|
95
|
if (ps.getOwner() != null && !classesToIgnore.contains(ps.getOwner().getClass().getName()) && !ps.wasInitialized() && !classes.contains(ps.getKey())) {
|
96
|
System.out.print("--> AspectJCompile-Time Weaving " + ps.getRole());
|
97
|
classes.add(ps.getKey());
|
98
|
try {
|
99
|
String role = ps.getRole();
|
100
|
String fieldName = role.substring(role.lastIndexOf(".") + 1);
|
101
|
System.out.print(", field : " + fieldName);
|
102
|
Object owner = ps.getOwner();
|
103
|
|
104
|
Object col = commonService.initializeCollection(((CdmBase)owner).getUuid(), fieldName);
|
105
|
ps.afterInitialize();
|
106
|
|
107
|
// Class<?> clazz = ps.getClass();
|
108
|
// if (clazz != null) {
|
109
|
// CollectionField cf = getCollectionField(col);
|
110
|
// Field field = clazz.getDeclaredField(cf.getFieldName());
|
111
|
// field.setAccessible(true);
|
112
|
// field.set(ps, cf.getCollection());
|
113
|
// }
|
114
|
|
115
|
} catch (Exception ex) {
|
116
|
ex.printStackTrace();
|
117
|
System.out.println("Error in ReattachSessionAspect : " + ex.getMessage());
|
118
|
} finally {
|
119
|
classes.remove(ps.getKey());
|
120
|
System.out.println("....Done");
|
121
|
throw new Exception("This code is invalid");
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
return pjp.proceed();
|
126
|
}
|
127
|
|
128
|
private CollectionField getCollectionField(PersistentCollection pc) {
|
129
|
if(pc != null) {
|
130
|
if(pc instanceof PersistentSet) {
|
131
|
return new CollectionField(new HashSet((Set)pc), "set");
|
132
|
}
|
133
|
if(pc instanceof PersistentSortedSet) {
|
134
|
return new CollectionField(new TreeSet((Set)pc), "set");
|
135
|
}
|
136
|
if(pc instanceof PersistentList) {
|
137
|
return new CollectionField(new ArrayList((List)pc), "list");
|
138
|
}
|
139
|
if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
|
140
|
return new CollectionField(new HashMap((Map)pc), "map");
|
141
|
}
|
142
|
if(pc instanceof PersistentSortedMap) {
|
143
|
return new CollectionField(new TreeMap((Map)pc), "map");
|
144
|
}
|
145
|
}
|
146
|
return null;
|
147
|
}
|
148
|
|
149
|
private String getCollectionFieldName(PersistentCollection pc) {
|
150
|
if(pc != null) {
|
151
|
if(pc instanceof PersistentSet || pc instanceof PersistentSortedSet) {
|
152
|
return "set";
|
153
|
}
|
154
|
if(pc instanceof PersistentList) {
|
155
|
return "list";
|
156
|
}
|
157
|
if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
|
158
|
return "map";
|
159
|
}
|
160
|
}
|
161
|
return null;
|
162
|
}
|
163
|
|
164
|
private class CollectionField {
|
165
|
private final Object col;
|
166
|
private final String fieldName;
|
167
|
public CollectionField(Object col, String fieldName) {
|
168
|
this.col = col;
|
169
|
this.fieldName = fieldName;
|
170
|
}
|
171
|
|
172
|
public Object getCollection() {
|
173
|
return this.col;
|
174
|
}
|
175
|
|
176
|
public String getFieldName() {
|
177
|
return this.fieldName;
|
178
|
}
|
179
|
}
|
180
|
}
|