1
|
/**
|
2
|
* Copyright (C) 2017 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.common;
|
10
|
|
11
|
import java.io.BufferedWriter;
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.IOException;
|
16
|
import java.nio.file.Files;
|
17
|
import java.util.Properties;
|
18
|
|
19
|
/**
|
20
|
*
|
21
|
* @author a.kohlbecker
|
22
|
* @since May 8, 2017
|
23
|
*
|
24
|
*/
|
25
|
public class ConfigFileUtil {
|
26
|
|
27
|
/**
|
28
|
*
|
29
|
*/
|
30
|
public static final String CDM_CONFIGFILE_OVERRIDE = "cdm.configfile.override.";
|
31
|
|
32
|
private Properties props = null;
|
33
|
|
34
|
private String defaultContent = "";
|
35
|
|
36
|
public ConfigFileUtil(){
|
37
|
|
38
|
}
|
39
|
|
40
|
public ConfigFileUtil setDefaultContent(String content) {
|
41
|
if(content != null){
|
42
|
defaultContent = content;
|
43
|
}
|
44
|
return this;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Per default the <code>propertiesSet</code> is loaded from a file located in
|
49
|
* <code>~/.cdmLibrary/remote-webapp/{instanceName}/{propertiesSet}.properties</code>.
|
50
|
* <p>
|
51
|
* This behavior can be overwritten by setting the java System property
|
52
|
* <code>cdm.configfile.override.{propertiesSet}</code> to an alternative file location.
|
53
|
* This mechanism should only be used for unit and integration tests.
|
54
|
*
|
55
|
* @param instanceName the name of the cdm instance. This value can be retrieved from the
|
56
|
* Spring environment with the key DataSourceConfigurer.CDM_DATA_SOURCE_ID ("")
|
57
|
* @param propertiesSet
|
58
|
* The base name of the properties file to be loaded. This name is extended with
|
59
|
* ".properties" to form the actual filename
|
60
|
*
|
61
|
* @return
|
62
|
* The file containing the properties
|
63
|
*/
|
64
|
public File getPropertiesFile(String instanceName, String propertiesSet) {
|
65
|
|
66
|
if(propertiesSet == null){
|
67
|
throw new NullPointerException();
|
68
|
}
|
69
|
String override = System.getProperty(CDM_CONFIGFILE_OVERRIDE + propertiesSet);
|
70
|
if(override != null){
|
71
|
return new File(override);
|
72
|
} else {
|
73
|
File configFolder = CdmUtils.getCdmInstanceSubDir(CdmUtils.SUBFOLDER_WEBAPP, instanceName);
|
74
|
return new File(configFolder, propertiesSet + ".properties");
|
75
|
}
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Per default the <code>propertiesSet</code> is loaded from a file located in
|
80
|
* <code>~/.cdmLibrary/remote-webapp/{instanceName}/{propertiesSet}.properties</code>.
|
81
|
* <p>
|
82
|
* This behavior can be overwritten by setting the java System property
|
83
|
* <code>cdm.configfile.override.{propertiesSet}</code> to an alternative file location.
|
84
|
* This mechanism should only be used for unit and integration tests.
|
85
|
*
|
86
|
* @param instanceName the name of the cdm instance. This value can be retrieved from the
|
87
|
* Spring environment with the key DataSourceConfigurer.CDM_DATA_SOURCE_ID ("")
|
88
|
* @param propertiesSet
|
89
|
* The base name of the properties file to be loaded. This name is extended with
|
90
|
* ".properties" to form the actual filename
|
91
|
*
|
92
|
* @return
|
93
|
* The properties loaded from the file
|
94
|
*/
|
95
|
public Properties getProperties(String instanceName, String propertiesName) throws IOException {
|
96
|
|
97
|
if(instanceName == null){
|
98
|
throw new NullPointerException();
|
99
|
}
|
100
|
if(props == null){
|
101
|
props = new Properties();
|
102
|
File uiPropertiesFile = getPropertiesFile(instanceName, propertiesName);
|
103
|
if(!uiPropertiesFile.exists()){
|
104
|
BufferedWriter writer = Files.newBufferedWriter(uiPropertiesFile.toPath());
|
105
|
writer.write(defaultContent);
|
106
|
writer.close();
|
107
|
}
|
108
|
try {
|
109
|
props.load(new FileInputStream(uiPropertiesFile));
|
110
|
} catch (FileNotFoundException e) {
|
111
|
// must not happen since we checked before
|
112
|
}
|
113
|
|
114
|
}
|
115
|
return props;
|
116
|
}
|
117
|
}
|