1
|
package eu.etaxonomy.taxeditor.remoting.cache;
|
2
|
|
3
|
import java.io.FileInputStream;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.io.ObjectInputStream;
|
7
|
import java.io.ObjectOutputStream;
|
8
|
import java.lang.reflect.Field;
|
9
|
import java.net.URISyntaxException;
|
10
|
import java.net.URL;
|
11
|
import java.util.HashMap;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import net.sf.ehcache.Cache;
|
16
|
import net.sf.ehcache.Element;
|
17
|
|
18
|
import org.eclipse.core.runtime.FileLocator;
|
19
|
import org.eclipse.core.runtime.Platform;
|
20
|
import org.hibernate.SessionFactory;
|
21
|
import org.hibernate.boot.MetadataSources;
|
22
|
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
|
23
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
24
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
25
|
import org.hibernate.cfg.Configuration;
|
26
|
import org.hibernate.mapping.PersistentClass;
|
27
|
import org.hibernate.mapping.Property;
|
28
|
import org.hibernate.metadata.ClassMetadata;
|
29
|
import org.hibernate.property.access.spi.Getter;
|
30
|
import org.osgi.framework.Bundle;
|
31
|
|
32
|
import org.hibernate.boot.Metadata;
|
33
|
|
34
|
|
35
|
public class CdmModelCacher {
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
|
41
|
|
42
|
public static final String CDM_MAP_SER_FILE_PATH = "resources/cdm.map.ser";
|
43
|
|
44
|
|
45
|
|
46
|
|
47
|
public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException, URISyntaxException {
|
48
|
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = loadModelClassMap();
|
49
|
|
50
|
cache.removeAll();
|
51
|
|
52
|
for(Map.Entry<String, CdmModelFieldPropertyFromClass> entry : modelClassMap.entrySet()) {
|
53
|
cache.put(new Element(entry.getKey(), entry.getValue()));
|
54
|
}
|
55
|
}
|
56
|
|
57
|
public Map<String, CdmModelFieldPropertyFromClass> loadModelClassMap() throws URISyntaxException, IOException, ClassNotFoundException {
|
58
|
|
59
|
Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
|
60
|
|
61
|
URL modelMapFileBundleURL = bundle.getEntry(CDM_MAP_SER_FILE_PATH);
|
62
|
URL modelMapFileURL = FileLocator.resolve(modelMapFileBundleURL);
|
63
|
String modelMapFilePath = modelMapFileURL.getFile();
|
64
|
|
65
|
FileInputStream fin = new FileInputStream(modelMapFilePath);
|
66
|
ObjectInputStream ois = new ObjectInputStream(fin);
|
67
|
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = (Map<String, CdmModelFieldPropertyFromClass>) ois.readObject();
|
68
|
ois.close();
|
69
|
return modelClassMap;
|
70
|
}
|
71
|
|
72
|
|
73
|
public Map<String, CdmModelFieldPropertyFromClass> generateModelClassMap() {
|
74
|
|
75
|
// A SessionFactory is set up once for an application!
|
76
|
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
77
|
.configure(HB_CONFIG_FILE_PATH) // configures settings from hibernate.cfg.xml
|
78
|
.build();
|
79
|
SessionFactory sessionFactory = null;
|
80
|
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<String, CdmModelFieldPropertyFromClass>();
|
81
|
try {
|
82
|
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
|
83
|
Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH);
|
84
|
Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata();
|
85
|
Metadata metadata = new MetadataSources( registry ).getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ).build();
|
86
|
|
87
|
|
88
|
for(ClassMetadata classMetaData :classMetaDataMap.values()) {
|
89
|
Class mappedClass = classMetaData.getMappedClass();
|
90
|
|
91
|
String mappedClassName = mappedClass.getName();
|
92
|
|
93
|
PersistentClass persistentClass =metadata.getEntityBinding(mappedClassName);
|
94
|
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
|
95
|
System.out.println("Adding class : " + mappedClassName + " to cache");
|
96
|
addGetters(persistentClass, cmgmfc);
|
97
|
modelClassMap.put(mappedClassName, cmgmfc);
|
98
|
}
|
99
|
}
|
100
|
catch (Exception e) {
|
101
|
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
|
102
|
// so destroy it manually.
|
103
|
StandardServiceRegistryBuilder.destroy( registry );
|
104
|
}
|
105
|
|
106
|
|
107
|
return modelClassMap;
|
108
|
}
|
109
|
|
110
|
|
111
|
public static Configuration buildConfiguration(String hibernateConfigFilePath) {
|
112
|
Configuration configuration = new Configuration().configure(hibernateConfigFilePath);
|
113
|
configuration.buildMappings();
|
114
|
return configuration;
|
115
|
}
|
116
|
|
117
|
private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
|
118
|
if (persistentClass != null) {
|
119
|
Iterator propertyIt = persistentClass.getPropertyIterator();
|
120
|
|
121
|
while(propertyIt.hasNext())
|
122
|
{
|
123
|
Property property = (Property)propertyIt.next();
|
124
|
Getter getter = property.getGetter(persistentClass.getMappedClass());
|
125
|
if(getter != null && getter.getMember() != null) {
|
126
|
Field field = (Field)getter.getMember();
|
127
|
|
128
|
//logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
|
129
|
cmgmfc.addGetMethods(field.getName());
|
130
|
}
|
131
|
}
|
132
|
addGetters(persistentClass.getSuperclass(), cmgmfc);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
|
137
|
|
138
|
public static void main(String argv[]) {
|
139
|
|
140
|
// To re-create the serialised cdm map run,
|
141
|
// mvn exec:java -Dexec.mainClass="eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher"
|
142
|
// in the eu.etaxonomy.taxeditor.cdmlib project root dir
|
143
|
String CDM_MAP_SER_DIR = "resources/";
|
144
|
|
145
|
CdmModelCacher cdmModelCacher = new CdmModelCacher();
|
146
|
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = cdmModelCacher.generateModelClassMap();
|
147
|
try{
|
148
|
|
149
|
FileOutputStream fout = new FileOutputStream(CDM_MAP_SER_DIR + "cdm.map.ser");
|
150
|
ObjectOutputStream oos = new ObjectOutputStream(fout);
|
151
|
oos.writeObject(modelClassMap);
|
152
|
oos.close();
|
153
|
System.out.println("CDM Map serialized");
|
154
|
|
155
|
}catch(Exception ex){
|
156
|
ex.printStackTrace();
|
157
|
}
|
158
|
|
159
|
}
|
160
|
|
161
|
|
162
|
}
|