1
|
/**
|
2
|
* Copyright (C) 2018 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.config;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
import java.util.Properties;
|
16
|
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.core.env.Environment;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.config.ConfigFileUtil;
|
22
|
|
23
|
/**
|
24
|
* @author a.kohlbecker
|
25
|
* @since Feb 16, 2018
|
26
|
*/
|
27
|
@Component
|
28
|
public class ApplicationConfiguration {
|
29
|
|
30
|
/**
|
31
|
* Annotate your integration test class with
|
32
|
* {@code @TestPropertySource(properties = {"testmode = true"})} to
|
33
|
* load the configuration files from the test resources.
|
34
|
* <p>
|
35
|
* <b>NOTE:</b> This will not work with current implementation of the
|
36
|
* {@link eu.etaxonomy.cdm.test.integration.CdmIntegrationTest CdmIntegrationTests}.
|
37
|
* For these tests please use the {@link unitils.AlternativeUnitilsJUnit4TestClassRunner}.
|
38
|
*
|
39
|
*/
|
40
|
public static final String TEST_MODE = "testmode";
|
41
|
|
42
|
@Autowired
|
43
|
private Environment env;
|
44
|
|
45
|
@Autowired
|
46
|
private ConfigFileUtil configFileUtil;
|
47
|
|
48
|
private Map<String, Properties> configurations = new HashMap<>();
|
49
|
|
50
|
public String getProperty(ApplicationConfigurationFile configFile, String key){
|
51
|
Properties props = loadPropertiesFile(configFile);
|
52
|
return props.getProperty(key);
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* @param currentDataSourceId
|
57
|
* @throws IOException
|
58
|
*/
|
59
|
protected Properties loadPropertiesFile(ApplicationConfigurationFile configFile) {
|
60
|
|
61
|
String currentDataSourceId = env.getProperty(CdmConfigurationKeys.CDM_DATA_SOURCE_ID);
|
62
|
if(!configurations.containsKey(configFile.getFileName())){
|
63
|
Properties props;
|
64
|
try {
|
65
|
if(env.getProperty(TEST_MODE) == null){
|
66
|
// PRODUCTION MODE
|
67
|
props = configFileUtil
|
68
|
.setDefaultContent(configFile.getDefaultContet())
|
69
|
.getProperties(currentDataSourceId, configFile.getFileName());
|
70
|
} else {
|
71
|
// TEST MODE
|
72
|
InputStream is = ApplicationConfiguration.class.getClassLoader().getResourceAsStream(
|
73
|
configFile.getFileName());
|
74
|
if (is == null) {
|
75
|
throw new RuntimeException("Can't find configuration file '" + configFile.getFileName() + ".properties in the test resoures.");
|
76
|
}
|
77
|
props = new Properties();
|
78
|
props.load(is);
|
79
|
}
|
80
|
if(props != null){
|
81
|
configurations.put(configFile.getFileName(), props);
|
82
|
}
|
83
|
} catch (IOException e) {
|
84
|
throw new RuntimeException("Error reading the configuration file '" + configFile.getFileName() + ".properties'. File corrupted?", e);
|
85
|
}
|
86
|
}
|
87
|
return configurations.get(configFile.getFileName());
|
88
|
}
|
89
|
|
90
|
|
91
|
|
92
|
|
93
|
|
94
|
}
|