X-Git-Url: https://dev.e-taxonomy.eu/gitweb/taxeditor.git/blobdiff_plain/4b94c9cfe0df4ec8f83395b88d8420c5791a93b4..d9e8e24916cbf078aca33e4f74441e94b0b67c55:/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java index 2f3d49be6..997526936 100644 --- a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java +++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java @@ -1,190 +1,162 @@ package eu.etaxonomy.taxeditor.remoting.cache; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; import java.util.Iterator; -import java.util.List; +import java.util.Map; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; -import org.apache.log4j.Logger; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Platform; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; -import org.hibernate.property.Getter; +import org.hibernate.metadata.ClassMetadata; +import org.hibernate.property.access.spi.Getter; +import org.osgi.framework.Bundle; + +import org.hibernate.boot.Metadata; public class CdmModelCacher { - private static final Logger logger = Logger.getLogger(CdmModelCacher.class); - private final List cmgmfcList = new ArrayList(); - public void cacheGetters() { + public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml"; + + public static final String CDM_MAP_SER_FILE_PATH = "resources/cdm.map.ser"; + + + + + public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException, URISyntaxException { + Map modelClassMap = loadModelClassMap(); + + cache.removeAll(); + + for(Map.Entry entry : modelClassMap.entrySet()) { + cache.put(new Element(entry.getKey(), entry.getValue())); + } + } + + public Map loadModelClassMap() throws URISyntaxException, IOException, ClassNotFoundException { + + Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib"); + + URL modelMapFileBundleURL = bundle.getEntry(CDM_MAP_SER_FILE_PATH); + URL modelMapFileURL = FileLocator.resolve(modelMapFileBundleURL); + String modelMapFilePath = modelMapFileURL.getFile(); + + FileInputStream fin = new FileInputStream(modelMapFilePath); + ObjectInputStream ois = new ObjectInputStream(fin); + Map modelClassMap = (Map) ois.readObject(); + ois.close(); + return modelClassMap; + } + + + public Map generateModelClassMap() { + + // A SessionFactory is set up once for an application! + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .configure(HB_CONFIG_FILE_PATH) // configures settings from hibernate.cfg.xml + .build(); + SessionFactory sessionFactory = null; + Map modelClassMap = new HashMap(); + try { + sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); + Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH); + Map classMetaDataMap = sessionFactory.getAllClassMetadata(); + Metadata metadata = new MetadataSources( registry ).getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ).build(); + + + for(ClassMetadata classMetaData :classMetaDataMap.values()) { + Class mappedClass = classMetaData.getMappedClass(); + + String mappedClassName = mappedClass.getName(); + + PersistentClass persistentClass =metadata.getEntityBinding(mappedClassName); + CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); + System.out.println("Adding class : " + mappedClassName + " to cache"); + addGetters(persistentClass, cmgmfc); + modelClassMap.put(mappedClassName, cmgmfc); + } + } + catch (Exception e) { + // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory + // so destroy it manually. + StandardServiceRegistryBuilder.destroy( registry ); + } + + + return modelClassMap; + } + + + public static Configuration buildConfiguration(String hibernateConfigFilePath) { + Configuration configuration = new Configuration().configure(hibernateConfigFilePath); + configuration.buildMappings(); + return configuration; + } - Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml"); - configuration.buildMappings(); - Iterator classMappingIterator = configuration.getClassMappings(); + private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) { + if (persistentClass != null) { + Iterator propertyIt = persistentClass.getPropertyIterator(); - Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache(); - cache.removeAll(); + while(propertyIt.hasNext()) + { + Property property = (Property)propertyIt.next(); + Getter getter = property.getGetter(persistentClass.getMappedClass()); + if(getter != null && getter.getMember() != null) { + Field field = (Field)getter.getMember(); - while(classMappingIterator.hasNext()) { - PersistentClass persistentClass = classMappingIterator.next(); - Class mappedClass = persistentClass.getMappedClass(); - String mappedClassName = mappedClass.getName(); - - CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); - Iterator propertyIt = persistentClass.getPropertyIterator(); - - logger.info("Adding class : " + mappedClassName + " to cache"); - - while(propertyIt.hasNext()) - { - Property property = (Property)propertyIt.next(); - Getter getter = property.getGetter(mappedClass); - if(getter != null && getter.getMember() != null) { - Field field = (Field)getter.getMember(); - String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName()); - logger.info(" - getMethod : " + getMethod + " for type " + field.getType().getName()); - cmgmfc.addGetMethods(getMethod); - } - } - cache.put(new Element(mappedClassName, cmgmfc)); - - } - cache.flush(); - } - - public void cacheGetterFields() { - - Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml"); - configuration.buildMappings(); - Iterator classMappingIterator = configuration.getClassMappings(); - - Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache(); - cache.removeAll(); - - while(classMappingIterator.hasNext()) { - PersistentClass persistentClass = classMappingIterator.next(); - Class mappedClass = persistentClass.getMappedClass(); - String mappedClassName = mappedClass.getName(); - - CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); - addGetters(persistentClass, cmgmfc); - logger.info("Adding class : " + mappedClassName + " to cache"); - - cache.put(new Element(mappedClassName, cmgmfc)); - - } - cache.flush(); - } - - private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) { - if (persistentClass != null) { - Iterator propertyIt = persistentClass.getPropertyIterator(); - - while(propertyIt.hasNext()) - { - Property property = (Property)propertyIt.next(); - Getter getter = property.getGetter(persistentClass.getMappedClass()); - if(getter != null && getter.getMember() != null) { - Field field = (Field)getter.getMember(); - //String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName()); - logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'"); - cmgmfc.addGetMethods(field.getName()); - } - } - addGetters(persistentClass.getSuperclass(), cmgmfc); - } - } - - public void checkGetterMethods() { - - Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml"); - configuration.buildMappings(); - Iterator classMappingIterator = configuration.getClassMappings(); - - Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache(); - cache.removeAll(); - - while(classMappingIterator.hasNext()) { - PersistentClass persistentClass = classMappingIterator.next(); - Class mappedClass = persistentClass.getMappedClass(); - String mappedClassName = mappedClass.getName(); - - Iterator propertyIt = persistentClass.getPropertyIterator(); - - Method[] methods = mappedClass.getMethods(); - - while(propertyIt.hasNext()) - { - Property property = (Property)propertyIt.next(); - Getter getter = property.getGetter(mappedClass); - if(getter != null && getter.getMember() != null) { - Field field = (Field)getter.getMember(); - String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName()); - - boolean foundMethod = false; - for(Method method : methods) { - if(method.getName().equals(getMethod)) { - foundMethod = true; - break; - } - } - if(!foundMethod) { - logger.info("Inferred method " + getMethod + " does not exist in class " + mappedClassName); - //throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName); - } - } - } - - - } - - } - - public List getCdmModelGetMethodFromClassList() { - cmgmfcList.clear(); - Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml"); - configuration.buildMappings(); - Iterator classMappingIterator = configuration.getClassMappings(); - - while(classMappingIterator.hasNext()) { - PersistentClass persistentClass = classMappingIterator.next(); - Class mappedClass = persistentClass.getMappedClass(); - String mappedClassName = mappedClass.getName(); - - CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); - Iterator propertyIt = persistentClass.getPropertyIterator(); - - while(propertyIt.hasNext()) - { - Property property = (Property)propertyIt.next(); - Getter getter = property.getGetter(mappedClass); - if(getter != null && getter.getMember() != null) { - Field field = (Field)getter.getMember(); - String getMethod = getMethodNameFromFieldName(getter.getMember().getName(),field.getType().getName()); - cmgmfc.addGetMethods(getMethod); - } - } - cmgmfcList.add(cmgmfc); - } - return cmgmfcList; - - } - - public static String getMethodNameFromFieldName(String fieldName, String type) { - String prefix = type != null && type.toLowerCase().endsWith("boolean") ? "is" : "get"; - String getMethod = prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); - return getMethod; - } + //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'"); + cmgmfc.addGetMethods(field.getName()); + } + } + addGetters(persistentClass.getSuperclass(), cmgmfc); + } + } + public static void main(String argv[]) { + + // To re-create the serialised cdm map run, + // mvn exec:java -Dexec.mainClass="eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher" + // in the eu.etaxonomy.taxeditor.cdmlib project root dir + String CDM_MAP_SER_DIR = "resources/"; + + CdmModelCacher cdmModelCacher = new CdmModelCacher(); + Map modelClassMap = cdmModelCacher.generateModelClassMap(); + try{ + + FileOutputStream fout = new FileOutputStream(CDM_MAP_SER_DIR + "cdm.map.ser"); + ObjectOutputStream oos = new ObjectOutputStream(fout); + oos.writeObject(modelClassMap); + oos.close(); + System.out.println("CDM Map serialized"); + + }catch(Exception ex){ + ex.printStackTrace(); + } + + } }