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
.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
;
32 import org
.hibernate
.boot
.Metadata
;
35 public class CdmModelCacher
{
40 public static String HB_CONFIG_FILE_PATH
= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
42 public static final String CDM_MAP_SER_FILE_PATH
= "resources/cdm.map.ser";
47 public void cacheGetterFields(Cache cache
) throws IOException
, ClassNotFoundException
, URISyntaxException
{
48 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= loadModelClassMap();
52 for(Map
.Entry
<String
, CdmModelFieldPropertyFromClass
> entry
: modelClassMap
.entrySet()) {
53 cache
.put(new Element(entry
.getKey(), entry
.getValue()));
57 public Map
<String
, CdmModelFieldPropertyFromClass
> loadModelClassMap() throws URISyntaxException
, IOException
, ClassNotFoundException
{
59 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
61 URL modelMapFileBundleURL
= bundle
.getEntry(CDM_MAP_SER_FILE_PATH
);
62 URL modelMapFileURL
= FileLocator
.resolve(modelMapFileBundleURL
);
63 String modelMapFilePath
= modelMapFileURL
.getFile();
65 FileInputStream fin
= new FileInputStream(modelMapFilePath
);
66 ObjectInputStream ois
= new ObjectInputStream(fin
);
67 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= (Map
<String
, CdmModelFieldPropertyFromClass
>) ois
.readObject();
73 public Map
<String
, CdmModelFieldPropertyFromClass
> generateModelClassMap() {
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
79 SessionFactory sessionFactory
= null;
80 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= new HashMap
<String
, CdmModelFieldPropertyFromClass
>();
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();
88 for(ClassMetadata classMetaData
:classMetaDataMap
.values()) {
89 Class mappedClass
= classMetaData
.getMappedClass();
91 String mappedClassName
= mappedClass
.getName();
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
);
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
);
107 return modelClassMap
;
111 public static Configuration
buildConfiguration(String hibernateConfigFilePath
) {
112 Configuration configuration
= new Configuration().configure(hibernateConfigFilePath
);
113 configuration
.buildMappings();
114 return configuration
;
117 private void addGetters(PersistentClass persistentClass
, CdmModelFieldPropertyFromClass cmgmfc
) {
118 if (persistentClass
!= null) {
119 Iterator propertyIt
= persistentClass
.getPropertyIterator();
121 while(propertyIt
.hasNext())
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();
128 //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
129 cmgmfc
.addGetMethods(field
.getName());
132 addGetters(persistentClass
.getSuperclass(), cmgmfc
);
138 public static void main(String argv
[]) {
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/";
145 CdmModelCacher cdmModelCacher
= new CdmModelCacher();
146 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= cdmModelCacher
.generateModelClassMap();
149 FileOutputStream fout
= new FileOutputStream(CDM_MAP_SER_DIR
+ "cdm.map.ser");
150 ObjectOutputStream oos
= new ObjectOutputStream(fout
);
151 oos
.writeObject(modelClassMap
);
153 System
.out
.println("CDM Map serialized");
155 }catch(Exception ex
){
156 ex
.printStackTrace();