merge
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / CdmModelCacher.java
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.cfg.Configuration;
21 import org.hibernate.mapping.PersistentClass;
22 import org.hibernate.mapping.Property;
23 import org.hibernate.property.Getter;
24 import org.osgi.framework.Bundle;
25
26
27 public class CdmModelCacher {
28
29
30
31
32 public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
33
34 public static final String CDM_MAP_SER_FILE_PATH = "resources/cdm.map.ser";
35
36
37
38
39 public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException, URISyntaxException {
40 Map<String, CdmModelFieldPropertyFromClass> modelClassMap = loadModelClassMap();
41
42 cache.removeAll();
43
44 for(Map.Entry<String, CdmModelFieldPropertyFromClass> entry : modelClassMap.entrySet()) {
45 cache.put(new Element(entry.getKey(), entry.getValue()));
46 }
47 }
48
49 public Map<String, CdmModelFieldPropertyFromClass> loadModelClassMap() throws URISyntaxException, IOException, ClassNotFoundException {
50
51 Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
52
53 URL modelMapFileBundleURL = bundle.getEntry(CDM_MAP_SER_FILE_PATH);
54 URL modelMapFileURL = FileLocator.resolve(modelMapFileBundleURL);
55 String modelMapFilePath = modelMapFileURL.getFile();
56
57 FileInputStream fin = new FileInputStream(modelMapFilePath);
58 ObjectInputStream ois = new ObjectInputStream(fin);
59 Map<String, CdmModelFieldPropertyFromClass> modelClassMap = (Map<String, CdmModelFieldPropertyFromClass>) ois.readObject();
60 ois.close();
61 return modelClassMap;
62 }
63
64
65 public Map<String, CdmModelFieldPropertyFromClass> generateModelClassMap() {
66
67 Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH);
68 Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
69
70 Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<String, CdmModelFieldPropertyFromClass>();
71
72 while(classMappingIterator.hasNext()) {
73 PersistentClass persistentClass = classMappingIterator.next();
74 Class mappedClass = persistentClass.getMappedClass();
75 String mappedClassName = mappedClass.getName();
76
77 CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
78 System.out.println("Adding class : " + mappedClassName + " to cache");
79 addGetters(persistentClass, cmgmfc);
80 modelClassMap.put(mappedClassName, cmgmfc);
81 }
82 return modelClassMap;
83 }
84
85
86 public static Configuration buildConfiguration(String hibernateConfigFilePath) {
87 Configuration configuration = new Configuration().configure(hibernateConfigFilePath);
88 configuration.buildMappings();
89 return configuration;
90 }
91
92 private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
93 if (persistentClass != null) {
94 Iterator propertyIt = persistentClass.getPropertyIterator();
95
96 while(propertyIt.hasNext())
97 {
98 Property property = (Property)propertyIt.next();
99 Getter getter = property.getGetter(persistentClass.getMappedClass());
100 if(getter != null && getter.getMember() != null) {
101 Field field = (Field)getter.getMember();
102
103 //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
104 cmgmfc.addGetMethods(field.getName());
105 }
106 }
107 addGetters(persistentClass.getSuperclass(), cmgmfc);
108 }
109 }
110
111
112
113 public static void main(String argv[]) {
114
115 // To re-create the serialised cdm map run,
116 // mvn exec:java -Dexec.mainClass="eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher"
117 // in the eu.etaxonomy.taxeditor.cdmlib project root dir
118 String CDM_MAP_SER_DIR = "resources/";
119
120 CdmModelCacher cdmModelCacher = new CdmModelCacher();
121 Map<String, CdmModelFieldPropertyFromClass> modelClassMap = cdmModelCacher.generateModelClassMap();
122 try{
123
124 FileOutputStream fout = new FileOutputStream(CDM_MAP_SER_DIR + "cdm.map.ser");
125 ObjectOutputStream oos = new ObjectOutputStream(fout);
126 oos.writeObject(modelClassMap);
127 oos.close();
128 System.out.println("CDM Map serialized");
129
130 }catch(Exception ex){
131 ex.printStackTrace();
132 }
133
134 }
135
136
137 }