Project

General

Profile

« Previous | Next » 

Revision 1a1e2ca4

Added by Andreas Müller almost 2 years ago

ref #6696 create CdmModelCache during maven and remove cached model from git

View differences:

cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CdmModelCacher.java
15 15
import java.io.ObjectInputStream;
16 16
import java.io.ObjectOutputStream;
17 17
import java.lang.reflect.Field;
18
import java.net.URISyntaxException;
19 18
import java.net.URL;
19
import java.util.Collection;
20 20
import java.util.HashMap;
21 21
import java.util.Iterator;
22 22
import java.util.Map;
23 23

  
24 24
import org.apache.logging.log4j.LogManager;
25 25
import org.apache.logging.log4j.Logger;
26
import org.hibernate.SessionFactory;
27 26
import org.hibernate.boot.Metadata;
28 27
import org.hibernate.boot.MetadataSources;
29 28
import org.hibernate.boot.registry.StandardServiceRegistry;
......
31 30
import org.hibernate.cfg.Configuration;
32 31
import org.hibernate.mapping.PersistentClass;
33 32
import org.hibernate.mapping.Property;
34
import org.hibernate.metadata.ClassMetadata;
35 33
import org.hibernate.property.access.spi.Getter;
36 34

  
37 35
import net.sf.ehcache.Cache;
38
import net.sf.ehcache.Element;
39 36

  
40 37
/**
41 38
 * This class is serializing and deserializing the CDM model for performance purposes.
......
55 52
    public static final String CDM_MAP_SER_FOLDER = "/eu/etaxonomy/cdm/mappings/";
56 53
    public static final String CDM_MAP_SER_FILE_PATH = CDM_MAP_SER_FOLDER + CDM_MAP_SER_FILE;
57 54

  
58
    public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException, URISyntaxException {
55
    public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException {
59 56
        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = loadModelClassMap();
60 57

  
61 58
        cache.removeAll();
62 59

  
63 60
        for(Map.Entry<String, CdmModelFieldPropertyFromClass> entry : modelClassMap.entrySet()) {
64
            cache.put(new Element(entry.getKey(), entry.getValue()));
61
            cache.put(new net.sf.ehcache.Element(entry.getKey(), entry.getValue()));
65 62
        }
66 63
    }
67 64

  
68
    public Map<String, CdmModelFieldPropertyFromClass> loadModelClassMap() throws URISyntaxException, IOException, ClassNotFoundException  {
65
    public Map<String, CdmModelFieldPropertyFromClass> loadModelClassMap() throws IOException, ClassNotFoundException  {
69 66

  
70
        // ============== Eclpipse specific ============== //
71
        /*Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
72 67

  
73
        URL modelMapFileBundleURL = bundle.getEntry(CDM_MAP_SER_FILE_PATH);
74
        URL modelMapFileURL = FileLocator.resolve(modelMapFileBundleURL);
75
        String modelMapFilePath = modelMapFileURL.getFile();
76
        FileInputStream fin = new FileInputStream(modelMapFilePath);
77
        */
78 68
        InputStream fin = this.getClass().getResourceAsStream(CDM_MAP_SER_FILE_PATH);
79
        // ==============000000000000000 ============== //
80

  
81 69
        ObjectInputStream ois = new ObjectInputStream(fin);
82 70
        @SuppressWarnings("unchecked")
83 71
		Map<String, CdmModelFieldPropertyFromClass> modelClassMap = (Map<String, CdmModelFieldPropertyFromClass>) ois.readObject();
......
92 80
    	final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
93 81
    			.configure(hibernateConfigFile) // configures settings from hibernate.cfg.xml
94 82
    			.build();
95
    	SessionFactory sessionFactory = null;
83

  
96 84
    	Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<>();
97 85
    	try {
98 86
//    		ConnectionProvider connectionProvider = registry.getService(ConnectionProvider.class);
99 87
//    		DatasourceConnectionProviderImpl providerImpl = registry.getService(DatasourceConnectionProviderImpl.class);
100 88

  
101 89
    		Metadata metadata = new MetadataSources( registry ).buildMetadata();
102
    		sessionFactory = metadata.buildSessionFactory();
103
//    		Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH);
104

  
105
        	Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata();
106
//        	Metadata metadata = new MetadataSources( registry ).getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ).build();
107

  
108
            for(ClassMetadata classMetaData : classMetaDataMap.values()) {
109
            	Class<?> mappedClass = classMetaData.getMappedClass();
110

  
111
                String mappedClassName = mappedClass.getName();
112

  
113
                PersistentClass persistentClass = metadata.getEntityBinding(mappedClassName);
114
                CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
115
                logger.warn("Adding class : " + mappedClassName + " to cache");
116
                addGetters(persistentClass, cmgmfc);
117
                modelClassMap.put(mappedClassName, cmgmfc);
90
    		Collection<PersistentClass> entityBindings = metadata.getEntityBindings();
91
    		for (PersistentClass persistentClass : entityBindings) {
92
                Class<?> mappedClass = persistentClass.getMappedClass();
93
                if (mappedClass != null) {
94
                    handleEntityClass(modelClassMap, metadata, mappedClass);
95
                }
118 96
            }
97

  
98
//    		Class<?> epc = entityBindings.iterator().next().getMappedClass();
99

  
100
    		//SessionFactory sessionFactory = metadata.buildSessionFactory();
101
//    		Metamodel metaModel = sessionFactory.getMetamodel();
102
//    		Set<EntityType<?>> entityTypes = metaModel.getEntities();
103
//    		for (EntityType<?> entityType : entityTypes) {
104
//    		    Class<?> mappedClass = entityType.getJavaType();
105
//                handleEntityClass(modelClassMap, metadata, mappedClass);
106
//    		}
107
//    		sessionFactory.close();
108

  
109
//    		// *************** OLD ***********************/
110
//
111
//        	Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata();
112
////        	Metadata metadata = new MetadataSources( registry ).getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ).build();
113
//
114
//            for(ClassMetadata classMetaData : classMetaDataMap.values()) {
115
//            	Class<?> mappedClass = classMetaData.getMappedClass();
116
//
117
//                String mappedClassName = mappedClass.getName();
118
//
119
//                PersistentClass persistentClass = metadata.getEntityBinding(mappedClassName);
120
//                CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
121
//                logger.warn("Adding class : " + mappedClassName + " to cache");
122
//                addGetters(persistentClass, cmgmfc);
123
//                modelClassMap.put(mappedClassName, cmgmfc);
124
//            }
119 125
    	}
120 126
    	catch (Exception e) {
121 127
    		// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
......
126 132
        return modelClassMap;
127 133
    }
128 134

  
135
    private void handleEntityClass(Map<String, CdmModelFieldPropertyFromClass> modelClassMap, Metadata metadata,
136
            Class<?> mappedClass) {
137
        String mappedClassName = mappedClass.getName();
138
        PersistentClass persistentClass = metadata.getEntityBinding(mappedClassName);
139
        CdmModelFieldPropertyFromClass fieldProperties = new CdmModelFieldPropertyFromClass(mappedClassName);
140
        logger.warn("Adding class : " + mappedClassName + " to cache");
141
        addGetters(persistentClass, fieldProperties);
142
        modelClassMap.put(mappedClassName, fieldProperties);
143
    }
144

  
129 145
    public static Configuration buildConfiguration(String hibernateConfigFilePath) {
130 146
        Configuration configuration = new Configuration().configure(hibernateConfigFilePath);
131
        configuration.buildMappings();
132 147
        return configuration;
133 148
    }
134 149

  
......
159 174
    	// See also https://dev.e-taxonomy.eu/redmine/projects/edit/wiki/TaxonomicEditorDevelopersGuide#Model-Change-Actions
160 175
    	//Note AM: does not fully work for me, but running the main from the IDE works.
161 176

  
177
        System.out.println("Start CdmModelCacher main.");
162 178
        CdmModelCacher cdmModelCacher = new CdmModelCacher();
163 179
        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = cdmModelCacher.generateModelClassMap();
164 180
        try{
181
            System.out.println("Model created.");
165 182
        	if (!modelClassMap.isEmpty()){
166 183
        	    File outFile = new File("src/main/resources/" + CDM_MAP_SER_FILE_PATH);
167 184
        	    System.out.println("writing to " + outFile.getAbsolutePath());
......
170 187
        		oos.writeObject(modelClassMap);
171 188
        		oos.close();
172 189
        		System.out.println("CDM Map serialized");
190
                System.exit(0);
173 191
        	}else{
174 192
        		String message = "CDM Map was empty. Model cache update NOT successful";
175 193
        		System.out.println(message);
176 194
        	}
177

  
178 195
        }catch(Exception ex){
179 196
            ex.printStackTrace();
180 197
        }
198

  
199
        System.exit(1);
181 200
    }
182
}
201
}

Also available in: Unified diff