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