• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 15205088843

23 May 2025 07:43AM UTC coverage: 65.083% (+0.01%) from 65.069%
15205088843

push

github

rkorytkowski
TRUNK-6300: Adding Windows test, cleaning up logs, adjusting variable name

2 of 2 new or added lines in 2 files covered. (100.0%)

336 existing lines in 18 files now uncovered.

23379 of 35922 relevant lines covered (65.08%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.01
/api/src/main/java/org/openmrs/api/db/hibernate/HibernateSessionFactoryBean.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.api.db.hibernate;
11

12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.Collections;
17
import java.util.HashMap;
18
import java.util.HashSet;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Map.Entry;
22
import java.util.Properties;
23
import java.util.Set;
24

25
import org.hibernate.HibernateException;
26
import org.hibernate.Interceptor;
27
import org.hibernate.boot.Metadata;
28
import org.hibernate.engine.spi.SessionFactoryImplementor;
29
import org.hibernate.integrator.spi.Integrator;
30
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
31
import org.openmrs.api.context.Context;
32
import org.openmrs.module.Module;
33
import org.openmrs.module.ModuleFactory;
34
import org.openmrs.util.OpenmrsUtil;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37
import org.slf4j.MarkerFactory;
38
import org.springframework.beans.factory.annotation.Autowired;
39
import org.springframework.beans.factory.annotation.Value;
40
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
41

42
public class HibernateSessionFactoryBean extends LocalSessionFactoryBean implements Integrator {
1✔
43
        
44
        private static final Logger log = LoggerFactory.getLogger(HibernateSessionFactoryBean.class);
1✔
45
        
46
        protected Set<String> mappingResources = new HashSet<>();
1✔
47
        
48
        /**
49
         * @since 1.9.2, 1.10
50
         */
51
        protected Set<String> packagesToScan = new HashSet<>();
1✔
52
        
53
        // @since 1.6.3, 1.7.2, 1.8.0, 1.9
54
        protected ChainingInterceptor chainingInterceptor = new ChainingInterceptor();
1✔
55
        
56
        // @since 1.6.3, 1.7.2, 1.8.0, 1.9
57
        // This will be sorted on keys before being used
58
        @Autowired(required = false)
1✔
59
        public Map<String, Interceptor> interceptors = new HashMap<>();
60

61
        @Value("${cache.type:local}")
62
        private String cacheType;
63
        
64
        private Metadata metadata;
65
        
66
        /**
67
         * Collect the mapping resources for future use because the mappingResources object is defined
68
         * as 'private' instead of 'protected'
69
         */
70
        @Override
71
        public void setMappingResources(String... mappingResources) {
72
                Collections.addAll(this.mappingResources, mappingResources);
1✔
73
                
74
                super.setMappingResources(this.mappingResources.toArray(new String[] {}));
1✔
75
        }
1✔
76
        
77
        /**
78
         * Collect packages to scan that are set in core and for tests in modules.
79
         * <p>
80
         * It adds to the set instead of overwriting it with each call.
81
         */
82
        @Override
83
        public void setPackagesToScan(String... packagesToScan) {
84
                this.packagesToScan.addAll(Arrays.asList(packagesToScan));
1✔
85
                
86
                super.setPackagesToScan(this.packagesToScan.toArray(new String[0]));
1✔
87
        }
1✔
88
        
89
        public Set<String> getModuleMappingResources() {
90
                for (Module mod : ModuleFactory.getStartedModules()) {
1✔
UNCOV
91
                        mappingResources.addAll(mod.getMappingFiles());
×
UNCOV
92
                }
×
93
                return mappingResources;
1✔
94
        }
95
        
96
        /**
97
         * Gets packages with mapped classes from all modules.
98
         *
99
         * @return the set of packages with mapped classes
100
         * @since 1.9.2, 1.10
101
         */
102
        public Set<String> getModulePackagesWithMappedClasses() {
103
                Set<String> packages = new HashSet<>();
1✔
104
                for (Module module : ModuleFactory.getStartedModules()) {
1✔
UNCOV
105
                        packages.addAll(module.getPackagesWithMappedClasses());
×
UNCOV
106
                }
×
107
                return packages;
1✔
108
        }
109
        
110
        /**
111
         * Overridden to populate mappings from modules.
112
         */
113
        @Override
114
        public void afterPropertiesSet() throws IOException {
115
                log.debug("Configuring hibernate sessionFactory properties");
1✔
116
                Properties config = getHibernateProperties();
1✔
117
                
118
                Properties moduleProperties = Context.getConfigProperties();
1✔
119
                
120
                // override or initialize config properties with module-provided ones
121
                for (Map.Entry<Object, Object> entry : moduleProperties.entrySet()) {
1✔
122
                        Object key = entry.getKey();
×
123
                        String prop = (String) key;
×
124
                        String value = (String) entry.getValue();
×
UNCOV
125
                        log.trace("Setting module property: " + prop + ":" + value);
×
126
                        config.setProperty(prop, value);
×
UNCOV
127
                        if (!prop.startsWith("hibernate")) {
×
UNCOV
128
                                config.setProperty("hibernate." + prop, value);
×
129
                        }
UNCOV
130
                }
×
131
                
132
                Properties properties = Context.getRuntimeProperties();
1✔
133
                
134
                // loop over runtime properties and override each in the configuration
135
                for (Map.Entry<Object, Object> entry : properties.entrySet()) {
1✔
136
                        Object key = entry.getKey();
1✔
137
                        String prop = (String) key;
1✔
138
                        String value = (String) entry.getValue();
1✔
139
                        log.trace("Setting property: " + prop + ":" + value);
1✔
140
                        config.setProperty(prop, value);
1✔
141
                        if (!prop.startsWith("hibernate")) {
1✔
142
                                config.setProperty("hibernate." + prop, value);
1✔
143
                        }
144
                }
1✔
145
                
146
                // load in the default hibernate properties
147
                try {
148
                        InputStream propertyStream = getClass().getResourceAsStream("/hibernate.default.properties");
1✔
149
                        Properties props = new Properties();
1✔
150
                        
151
                        OpenmrsUtil.loadProperties(props, propertyStream);
1✔
152
                        propertyStream.close();
1✔
153
                        
154
                        // Load infinispan config based on selected cache type
155
                        String local = "local".equalsIgnoreCase(cacheType.trim()) ? "-local" : "";
1✔
156
                        props.put("hibernate.cache.infinispan.cfg", 
1✔
157
                                "org/infinispan/hibernate/cache/commons/builder/infinispan-configs" + local + ".xml");
158
                        
159
                        // Only load in the default properties if they don't exist
160
                        for (Entry<Object, Object> prop : props.entrySet()) {
1✔
161
                                if (!config.containsKey(prop.getKey())) {
1✔
162
                                        config.put(prop.getKey(), prop.getValue());
1✔
163
                                }
164
                        }
1✔
165
                        
166
                }
UNCOV
167
                catch (IOException e) {
×
UNCOV
168
                        log.error(MarkerFactory.getMarker("FATAL"), "Unable to load default hibernate properties", e);
×
169
                }
1✔
170
                
171
                log.debug("Replacing variables in hibernate properties");
1✔
172
                final String applicationDataDirectory = OpenmrsUtil.getApplicationDataDirectory();
1✔
173
                for (Entry<Object, Object> entry : config.entrySet()) {
1✔
174
                        String value = (String) entry.getValue();
1✔
175
                        
176
                        value = value.replace("%APPLICATION_DATA_DIRECTORY%", applicationDataDirectory);
1✔
177
                        entry.setValue(value);
1✔
178
                }
1✔
179
                
180
                log.debug("Setting global Hibernate Session Interceptor for SessionFactory, Interceptor: " + chainingInterceptor);
1✔
181
                
182
                // make sure all autowired interceptors are put onto our chaining interceptor
183
                // sort on the keys so that the devs/modules have some sort of control over the order of the interceptors 
184
                List<String> keys = new ArrayList<>(interceptors.keySet());
1✔
185
                Collections.sort(keys);
1✔
186
                for (String key : keys) {
1✔
187
                        chainingInterceptor.addInterceptor(interceptors.get(key));
1✔
188
                }
1✔
189
                
190
                setEntityInterceptor(chainingInterceptor);
1✔
191
                
192
                //Adding each module's mapping file to the list of mapping resources
193
                setMappingResources(getModuleMappingResources().toArray(new String[0]));
1✔
194
                
195
                setPackagesToScan(getModulePackagesWithMappedClasses().toArray(new String[0]));
1✔
196
                
197
                setHibernateIntegrators(this);
1✔
198
                
199
                super.afterPropertiesSet();
1✔
200
        }
1✔
201
        
202
        /**
203
         * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#destroy()
204
         */
205
        @Override
206
        public void destroy() throws HibernateException {
UNCOV
207
                super.destroy();
×
UNCOV
208
        }
×
209

210
        @Override
211
        public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
212
                        SessionFactoryServiceRegistry serviceRegistry) {
213
                this.metadata = metadata;
1✔
214
        }
1✔
215

216
        @Override
217
        public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
218
                
UNCOV
219
        }
×
220

221
        /**
222
         * @since 2.4
223
         */
224
        public Metadata getMetadata() {
225
                return metadata;
1✔
226
        }
227
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc