Project

General

Profile

Download (3.17 KB) Statistics
| Branch: | Tag: | Revision:
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
 */
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
    @Autowired
47
    ConfigFileUtil configFileUtil;
48

    
49
    Map<String, Properties> configurations = new HashMap<>();
50

    
51
    public String getProperty(ApplicationConfigurationFile configFile, String key){
52
        Properties props = loadPropertiesFile(configFile);
53
        return props.getProperty(key);
54
    }
55

    
56
    /**
57
     * @param currentDataSourceId
58
     * @throws IOException
59
     */
60
    protected Properties loadPropertiesFile(ApplicationConfigurationFile configFile) {
61

    
62
        String currentDataSourceId = env.getProperty(CdmConfigurationKeys.CDM_DATA_SOURCE_ID);
63
        if(!configurations.containsKey(configFile.getFileName())){
64
            Properties props;
65
            try {
66
                if(env.getProperty(TEST_MODE) == null){
67
                    // PRODUCTION MODE
68
                    props = configFileUtil
69
                            .setDefaultContent(configFile.getDefaultContet())
70
                            .getProperties(currentDataSourceId, configFile.getFileName());
71
                } else {
72
                    // TEST MODE
73
                    InputStream is = ApplicationConfiguration.class.getClassLoader().getResourceAsStream(
74
                            configFile.getFileName());
75
                    if (is == null) {
76
                        throw new RuntimeException("Can't find configuration file '" + configFile.getFileName() + ".properties in the test resoures.");
77
                    }
78
                    props = new Properties();
79
                    props.load(is);
80
                }
81
                if(props != null){
82
                    configurations.put(configFile.getFileName(), props);
83
                }
84
            } catch (IOException e) {
85
                throw new RuntimeException("Error reading the configuration file '" + configFile.getFileName() + ".properties'. File corrupted?", e);
86
            }
87
        }
88
        return configurations.get(configFile.getFileName());
89
    }
90

    
91

    
92

    
93

    
94

    
95
}
(1-1/6)