ref #7651 #7622 relaxing requirement for an available registration minter
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / config / ApplicationConfiguration.java
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.common.ConfigFileUtil;
22
23 /**
24 * @author a.kohlbecker
25 * @since Feb 16, 2018
26 *
27 */
28 @Component
29 public class ApplicationConfiguration {
30
31 /**
32 * Annotate your integration test class with
33 * {@code @TestPropertySource(properties = {"testmode = true"})} to
34 * load the configuration files from the test resources.
35 * <p>
36 * <b>NOTE:</b> This will not work with current implementation of the
37 * {@link eu.etaxonomy.cdm.test.integration.CdmIntegrationTest CdmIntegrationTests}.
38 * For these tests please use the {@link unitils.AlternativeUnitilsJUnit4TestClassRunner}.
39 *
40 */
41 public static final String TEST_MODE = "testmode";
42
43 @Autowired
44 Environment env;
45
46 Map<String, Properties> configurations = new HashMap<>();
47
48 public String getProperty(ApplicationConfigurationFile configFile, String key){
49 Properties props = loadPropertiesFile(configFile);
50 return props.getProperty(key);
51 }
52
53 /**
54 * @param currentDataSourceId
55 * @throws IOException
56 */
57 protected Properties loadPropertiesFile(ApplicationConfigurationFile configFile) {
58
59 String currentDataSourceId = env.getProperty(CdmConfigurationKeys.CDM_DATA_SOURCE_ID);
60 if(!configurations.containsKey(configFile.getFileName())){
61 Properties props;
62 try {
63 if(env.getProperty(TEST_MODE) == null){
64 // PRODUCTION MODE
65 props = new ConfigFileUtil()
66 .setDefaultContent(configFile.getDefaultContet())
67 .getProperties(currentDataSourceId, configFile.getFileName());
68 } else {
69 // TEST MODE
70 InputStream is = ApplicationConfiguration.class.getClassLoader().getResourceAsStream(
71 configFile.getFileName());
72 if (is == null) {
73 throw new RuntimeException("Can't find configuration file '" + configFile.getFileName() + ".properties in the test resoures.");
74 }
75 props = new Properties();
76 props.load(is);
77 }
78 if(props != null){
79 configurations.put(configFile.getFileName(), props);
80 }
81 } catch (IOException e) {
82 throw new RuntimeException("Error reading the configuration file '" + configFile.getFileName() + ".properties'. File corrupted?", e);
83 }
84 }
85 return configurations.get(configFile.getFileName());
86 }
87
88
89
90
91
92 }