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
.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
;
33 public class CdmModelCacher
{
38 public static String HB_CONFIG_FILE_PATH
= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
40 public static final String CDM_MAP_SER_FILE_PATH
= "resources/cdm.map.ser";
45 public void cacheGetterFields(Cache cache
) throws IOException
, ClassNotFoundException
, URISyntaxException
{
46 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= loadModelClassMap();
50 for(Map
.Entry
<String
, CdmModelFieldPropertyFromClass
> entry
: modelClassMap
.entrySet()) {
51 cache
.put(new Element(entry
.getKey(), entry
.getValue()));
55 public Map
<String
, CdmModelFieldPropertyFromClass
> loadModelClassMap() throws URISyntaxException
, IOException
, ClassNotFoundException
{
57 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
59 URL modelMapFileBundleURL
= bundle
.getEntry(CDM_MAP_SER_FILE_PATH
);
60 URL modelMapFileURL
= FileLocator
.resolve(modelMapFileBundleURL
);
61 String modelMapFilePath
= modelMapFileURL
.getFile();
63 FileInputStream fin
= new FileInputStream(modelMapFilePath
);
64 ObjectInputStream ois
= new ObjectInputStream(fin
);
65 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= (Map
<String
, CdmModelFieldPropertyFromClass
>) ois
.readObject();
71 public Map
<String
, CdmModelFieldPropertyFromClass
> generateModelClassMap() {
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
77 SessionFactory sessionFactory
= null;
78 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= new HashMap
<String
, CdmModelFieldPropertyFromClass
>();
80 // ConnectionProvider connectionProvider = registry.getService(ConnectionProvider.class);
81 // DatasourceConnectionProviderImpl providerImpl = registry.getService(DatasourceConnectionProviderImpl.class);
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();
90 for(ClassMetadata classMetaData
:classMetaDataMap
.values()) {
91 Class
<?
> mappedClass
= classMetaData
.getMappedClass();
93 String mappedClassName
= mappedClass
.getName();
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
);
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
);
110 return modelClassMap
;
114 public static Configuration
buildConfiguration(String hibernateConfigFilePath
) {
115 Configuration configuration
= new Configuration().configure(hibernateConfigFilePath
);
116 configuration
.buildMappings();
117 return configuration
;
120 private void addGetters(PersistentClass persistentClass
, CdmModelFieldPropertyFromClass cmgmfc
) {
121 if (persistentClass
!= null) {
122 Iterator propertyIt
= persistentClass
.getPropertyIterator();
124 while(propertyIt
.hasNext())
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();
131 //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
132 cmgmfc
.addGetMethods(field
.getName());
135 addGetters(persistentClass
.getSuperclass(), cmgmfc
);
141 public static void main(String argv
[]) {
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/";
148 CdmModelCacher cdmModelCacher
= new CdmModelCacher();
149 Map
<String
, CdmModelFieldPropertyFromClass
> modelClassMap
= cdmModelCacher
.generateModelClassMap();
152 FileOutputStream fout
= new FileOutputStream(CDM_MAP_SER_DIR
+ "cdm.map.ser");
153 ObjectOutputStream oos
= new ObjectOutputStream(fout
);
154 oos
.writeObject(modelClassMap
);
156 System
.out
.println("CDM Map serialized");
158 }catch(Exception ex
){
159 ex
.printStackTrace();