1
|
package eu.etaxonomy.taxeditor.remoting.cache;
|
2
|
|
3
|
import java.lang.reflect.Field;
|
4
|
import java.lang.reflect.Method;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.List;
|
8
|
|
9
|
import net.sf.ehcache.Cache;
|
10
|
import net.sf.ehcache.Element;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.hibernate.cfg.Configuration;
|
14
|
import org.hibernate.mapping.PersistentClass;
|
15
|
import org.hibernate.mapping.Property;
|
16
|
import org.hibernate.property.Getter;
|
17
|
|
18
|
|
19
|
public class CdmModelCacher {
|
20
|
|
21
|
|
22
|
private static final Logger logger = Logger.getLogger(CdmModelCacher.class);
|
23
|
|
24
|
private final List<CdmModelFieldPropertyFromClass> cmgmfcList = new ArrayList<CdmModelFieldPropertyFromClass>();
|
25
|
|
26
|
public void cacheGetters() {
|
27
|
|
28
|
Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
|
29
|
configuration.buildMappings();
|
30
|
Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
|
31
|
|
32
|
Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
|
33
|
cache.removeAll();
|
34
|
|
35
|
while(classMappingIterator.hasNext()) {
|
36
|
PersistentClass persistentClass = classMappingIterator.next();
|
37
|
Class mappedClass = persistentClass.getMappedClass();
|
38
|
String mappedClassName = mappedClass.getName();
|
39
|
|
40
|
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
|
41
|
Iterator propertyIt = persistentClass.getPropertyIterator();
|
42
|
|
43
|
logger.info("Adding class : " + mappedClassName + " to cache");
|
44
|
|
45
|
while(propertyIt.hasNext())
|
46
|
{
|
47
|
Property property = (Property)propertyIt.next();
|
48
|
Getter getter = property.getGetter(mappedClass);
|
49
|
if(getter != null && getter.getMember() != null) {
|
50
|
Field field = (Field)getter.getMember();
|
51
|
String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
|
52
|
logger.info(" - getMethod : " + getMethod + " for type " + field.getType().getName());
|
53
|
cmgmfc.addGetMethods(getMethod);
|
54
|
}
|
55
|
}
|
56
|
cache.put(new Element(mappedClassName, cmgmfc));
|
57
|
|
58
|
}
|
59
|
cache.flush();
|
60
|
}
|
61
|
|
62
|
public void cacheGetterFields() {
|
63
|
|
64
|
Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
|
65
|
configuration.buildMappings();
|
66
|
Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
|
67
|
|
68
|
Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
|
69
|
cache.removeAll();
|
70
|
|
71
|
while(classMappingIterator.hasNext()) {
|
72
|
PersistentClass persistentClass = classMappingIterator.next();
|
73
|
Class mappedClass = persistentClass.getMappedClass();
|
74
|
String mappedClassName = mappedClass.getName();
|
75
|
|
76
|
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
|
77
|
addGetters(persistentClass, cmgmfc);
|
78
|
logger.info("Adding class : " + mappedClassName + " to cache");
|
79
|
|
80
|
cache.put(new Element(mappedClassName, cmgmfc));
|
81
|
|
82
|
}
|
83
|
cache.flush();
|
84
|
}
|
85
|
|
86
|
private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
|
87
|
if (persistentClass != null) {
|
88
|
Iterator propertyIt = persistentClass.getPropertyIterator();
|
89
|
|
90
|
while(propertyIt.hasNext())
|
91
|
{
|
92
|
Property property = (Property)propertyIt.next();
|
93
|
Getter getter = property.getGetter(persistentClass.getMappedClass());
|
94
|
if(getter != null && getter.getMember() != null) {
|
95
|
Field field = (Field)getter.getMember();
|
96
|
//String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
|
97
|
logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
|
98
|
cmgmfc.addGetMethods(field.getName());
|
99
|
}
|
100
|
}
|
101
|
addGetters(persistentClass.getSuperclass(), cmgmfc);
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public void checkGetterMethods() {
|
106
|
|
107
|
Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
|
108
|
configuration.buildMappings();
|
109
|
Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
|
110
|
|
111
|
Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
|
112
|
cache.removeAll();
|
113
|
|
114
|
while(classMappingIterator.hasNext()) {
|
115
|
PersistentClass persistentClass = classMappingIterator.next();
|
116
|
Class mappedClass = persistentClass.getMappedClass();
|
117
|
String mappedClassName = mappedClass.getName();
|
118
|
|
119
|
Iterator propertyIt = persistentClass.getPropertyIterator();
|
120
|
|
121
|
Method[] methods = mappedClass.getMethods();
|
122
|
|
123
|
while(propertyIt.hasNext())
|
124
|
{
|
125
|
Property property = (Property)propertyIt.next();
|
126
|
Getter getter = property.getGetter(mappedClass);
|
127
|
if(getter != null && getter.getMember() != null) {
|
128
|
Field field = (Field)getter.getMember();
|
129
|
String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
|
130
|
|
131
|
boolean foundMethod = false;
|
132
|
for(Method method : methods) {
|
133
|
if(method.getName().equals(getMethod)) {
|
134
|
foundMethod = true;
|
135
|
break;
|
136
|
}
|
137
|
}
|
138
|
if(!foundMethod) {
|
139
|
logger.info("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
|
140
|
//throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
|
146
|
}
|
147
|
|
148
|
}
|
149
|
|
150
|
public List<CdmModelFieldPropertyFromClass> getCdmModelGetMethodFromClassList() {
|
151
|
cmgmfcList.clear();
|
152
|
Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
|
153
|
configuration.buildMappings();
|
154
|
Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
|
155
|
|
156
|
while(classMappingIterator.hasNext()) {
|
157
|
PersistentClass persistentClass = classMappingIterator.next();
|
158
|
Class mappedClass = persistentClass.getMappedClass();
|
159
|
String mappedClassName = mappedClass.getName();
|
160
|
|
161
|
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
|
162
|
Iterator propertyIt = persistentClass.getPropertyIterator();
|
163
|
|
164
|
while(propertyIt.hasNext())
|
165
|
{
|
166
|
Property property = (Property)propertyIt.next();
|
167
|
Getter getter = property.getGetter(mappedClass);
|
168
|
if(getter != null && getter.getMember() != null) {
|
169
|
Field field = (Field)getter.getMember();
|
170
|
String getMethod = getMethodNameFromFieldName(getter.getMember().getName(),field.getType().getName());
|
171
|
cmgmfc.addGetMethods(getMethod);
|
172
|
}
|
173
|
}
|
174
|
cmgmfcList.add(cmgmfc);
|
175
|
}
|
176
|
return cmgmfcList;
|
177
|
|
178
|
}
|
179
|
|
180
|
public static String getMethodNameFromFieldName(String fieldName, String type) {
|
181
|
String prefix = type != null && type.toLowerCase().endsWith("boolean") ? "is" : "get";
|
182
|
String getMethod = prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
|
183
|
return getMethod;
|
184
|
}
|
185
|
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
}
|