1 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
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
;
11 import java
.util
.HashMap
;
12 import java
.util
.Iterator
;
15 import net
.sf
.ehcache
.Cache
;
16 import net
.sf
.ehcache
.Element
;
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
;
27 public class CdmModelCacher
{
32 public static String HB_CONFIG_FILE_PATH
= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
34 public static final String CDM_MAP_SER_FILE_PATH
= "resources/cdm.map.ser";
39 public void cacheGetterFields(Cache cache
) throws IOException
, ClassNotFoundException
, URISyntaxException
{
40 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= loadModelClassMap();
44 for(Map
.Entry
<String
, CdmModelFieldPropertyFromClass
> entry
: modelClassMap
.entrySet()) {
45 cache
.put(new Element(entry
.getKey(), entry
.getValue()));
49 public Map
<String
, CdmModelFieldPropertyFromClass
> loadModelClassMap() throws URISyntaxException
, IOException
, ClassNotFoundException
{
51 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
53 URL modelMapFileBundleURL
= bundle
.getEntry(CDM_MAP_SER_FILE_PATH
);
54 URL modelMapFileURL
= FileLocator
.resolve(modelMapFileBundleURL
);
55 String modelMapFilePath
= modelMapFileURL
.getFile();
57 FileInputStream fin
= new FileInputStream(modelMapFilePath
);
58 ObjectInputStream ois
= new ObjectInputStream(fin
);
59 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= (Map
<String
, CdmModelFieldPropertyFromClass
>) ois
.readObject();
65 public Map
<String
, CdmModelFieldPropertyFromClass
> generateModelClassMap() {
67 Configuration configuration
= buildConfiguration(HB_CONFIG_FILE_PATH
);
68 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
70 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= new HashMap
<String
, CdmModelFieldPropertyFromClass
>();
72 while(classMappingIterator
.hasNext()) {
73 PersistentClass persistentClass
= classMappingIterator
.next();
74 Class mappedClass
= persistentClass
.getMappedClass();
75 String mappedClassName
= mappedClass
.getName();
77 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
78 System
.out
.println("Adding class : " + mappedClassName
+ " to cache");
79 addGetters(persistentClass
, cmgmfc
);
80 modelClassMap
.put(mappedClassName
, cmgmfc
);
86 public static Configuration
buildConfiguration(String hibernateConfigFilePath
) {
87 Configuration configuration
= new Configuration().configure(hibernateConfigFilePath
);
88 configuration
.buildMappings();
92 private void addGetters(PersistentClass persistentClass
, CdmModelFieldPropertyFromClass cmgmfc
) {
93 if (persistentClass
!= null) {
94 Iterator propertyIt
= persistentClass
.getPropertyIterator();
96 while(propertyIt
.hasNext())
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();
103 //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
104 cmgmfc
.addGetMethods(field
.getName());
107 addGetters(persistentClass
.getSuperclass(), cmgmfc
);
113 public static void main(String argv
[]) {
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/";
120 CdmModelCacher cdmModelCacher
= new CdmModelCacher();
121 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= cdmModelCacher
.generateModelClassMap();
124 FileOutputStream fout
= new FileOutputStream(CDM_MAP_SER_DIR
+ "cdm.map.ser");
125 ObjectOutputStream oos
= new ObjectOutputStream(fout
);
126 oos
.writeObject(modelClassMap
);
128 System
.out
.println("CDM Map serialized");
130 }catch(Exception ex
){
131 ex
.printStackTrace();