1 package eu
.etaxonomy
.cdm
.api
.lazyloading
;
3 import java
.beans
.Transient
;
4 import java
.lang
.reflect
.Field
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collection
;
7 import java
.util
.HashMap
;
8 import java
.util
.HashSet
;
12 import java
.util
.TreeMap
;
13 import java
.util
.TreeSet
;
15 import org
.aspectj
.lang
.ProceedingJoinPoint
;
16 import org
.aspectj
.lang
.annotation
.Around
;
17 import org
.aspectj
.lang
.annotation
.Aspect
;
18 import org
.aspectj
.lang
.annotation
.Pointcut
;
19 import org
.hibernate
.collection
.internal
.PersistentList
;
20 import org
.hibernate
.collection
.internal
.PersistentMap
;
21 import org
.hibernate
.collection
.internal
.PersistentSet
;
22 import org
.hibernate
.collection
.internal
.PersistentSortedMap
;
23 import org
.hibernate
.collection
.internal
.PersistentSortedSet
;
24 import org
.hibernate
.collection
.spi
.PersistentCollection
;
25 import org
.hibernate
.proxy
.LazyInitializer
;
26 import org
.springframework
.beans
.factory
.annotation
.Autowire
;
27 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
28 import org
.springframework
.beans
.factory
.annotation
.Configurable
;
29 import org
.springframework
.stereotype
.Component
;
31 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
39 @Configurable(autowire
= Autowire
.BY_TYPE
)
40 public class CdmLazyLoader
{
43 private Set classes
= new HashSet();
45 public static boolean enableWeaving
= true;
46 private static Set
<String
> classesToIgnore
= new HashSet
<String
>();
50 private ICommonService commonService
;
52 public CdmLazyLoader() {
53 //classesToIgnore.add("eu.etaxonomy.cdm.model.common.TermVocabulary");
54 //classesToIgnore.add("eu.etaxonomy.cdm.model.common.OrderedTermVocabulary");
59 * Point cut for the 'initialize' method of the AbstractLazyInitializer.
62 @Pointcut("execution(* org.hibernate.proxy.AbstractLazyInitializer.initialize())")
63 public void possibleEntityLazyInitializationException() {
68 * 'Around' advice for the initialization of CDM Entity Objects
71 @Around(value
= "possibleEntityLazyInitializationException()")
72 public Object
preloadEntityOnDemand(ProceedingJoinPoint pjp
) throws Throwable
{
74 LazyInitializer ll
= (LazyInitializer
)pjp
.getTarget();
75 if(ll
.isUninitialized()) {
76 int classid
= ((Integer
)ll
.getIdentifier()).intValue();
77 System
.out
.print("--> AspectJ Compile-Time Weaving " + ll
.getEntityName() + " with id " + classid
);
78 Class clazz
= (Class
<?
extends CdmBase
>) Class
.forName(ll
.getEntityName());
79 CdmBase cdmBase
= CdmBase
.deproxy(commonService
.find(clazz
,classid
),clazz
);
80 ll
.setImplementation(cdmBase
);
81 System
.out
.println("....Done");
89 * Point cut for the 'initialize' method of the AbstractPersistentCollection.
92 @Pointcut("execution(protected final void org.hibernate.collection.internal.AbstractPersistentCollection.initialize(..))")
93 public void possibleCollectionLazyInitializationException() {
97 * 'Around' advice for the initialization of Collection objects
100 @Around(value
= "possibleCollectionLazyInitializationException()")
102 public Object
preloadCollectionOnDemand(ProceedingJoinPoint pjp
) throws Throwable
{
104 PersistentCollection ps
= (PersistentCollection
) pjp
.getTarget();
105 if (ps
.getOwner() != null && !classesToIgnore
.contains(ps
.getOwner().getClass().getName()) && !ps
.wasInitialized() && !classes
.contains(ps
.getKey())) {
106 System
.out
.print("--> AspectJCompile-Time Weaving " + ps
.getRole());
107 classes
.add(ps
.getKey());
109 String role
= ps
.getRole();
110 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
111 System
.out
.print(", field : " + fieldName
);
112 Object owner
= ps
.getOwner();
114 PersistentCollection col
= commonService
.initializeCollection(ps
);
115 ps
.afterInitialize();
117 Class
<?
> clazz
= ps
.getClass();
119 CollectionField cf
= getCollectionField(col
);
120 Field field
= clazz
.getDeclaredField(cf
.getFieldName());
121 field
.setAccessible(true);
122 field
.set(ps
, cf
.getCollection());
124 } catch (Exception ex
) {
125 ex
.printStackTrace();
126 System
.out
.println("Error in ReattachSessionAspect : " + ex
.getMessage());
128 classes
.remove(ps
.getKey());
129 System
.out
.println("....Done");
133 return pjp
.proceed();
137 private CollectionField
getCollectionField(PersistentCollection pc
) {
139 if(pc
instanceof PersistentSet
) {
140 return new CollectionField(new HashSet((Set
)pc
), "set");
142 if(pc
instanceof PersistentSortedSet
) {
143 return new CollectionField(new TreeSet((Set
)pc
), "set");
145 if(pc
instanceof PersistentList
) {
146 return new CollectionField(new ArrayList((List
)pc
), "list");
148 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
149 return new CollectionField(new HashMap((Map
)pc
), "map");
151 if(pc
instanceof PersistentSortedMap
) {
152 return new CollectionField(new TreeMap((Map
)pc
), "map");
158 private String
getCollectionFieldName(PersistentCollection pc
) {
160 if(pc
instanceof PersistentSet
|| pc
instanceof PersistentSortedSet
) {
164 if(pc
instanceof PersistentList
) {
167 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
174 private class CollectionField
{
176 private String fieldName
;
177 public CollectionField(Object col
, String fieldName
) {
179 this.fieldName
= fieldName
;
182 public Object
getCollection() {
186 public String
getFieldName() {
187 return this.fieldName
;