-
Notifications
You must be signed in to change notification settings - Fork 110
/
PropertiesLoader.java
107 lines (87 loc) · 3.27 KB
/
PropertiesLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.jenkinsci.plugins.envinject.service;
import hudson.Util;
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.plugins.envinject.util.SortedProperties;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author Gregory Boissinot
*/
public class PropertiesLoader implements Serializable {
/**
* Get a map environment variables from a properties file path
*
* @param propertiesFile the properties file
* @return a map containing all the file properties content
* @throws EnvInjectException
*/
public Map<String, String> getVarsFromPropertiesFile(File propertiesFile, Map<String, String> currentEnvVars) throws EnvInjectException {
if (propertiesFile == null) {
throw new NullPointerException("The properties file object must be set.");
}
if (!propertiesFile.exists()) {
throw new NullPointerException("The properties file object must be exist.");
}
Map<String, String> result = new LinkedHashMap<String, String>();
SortedProperties properties = new SortedProperties();
try {
String fileContent = Util.loadFile(propertiesFile);
String fileContentResolved = Util.replaceMacro(fileContent, currentEnvVars);
fileContentResolved = processPath(fileContentResolved);
properties.load(new StringReader(fileContentResolved));
} catch (IOException ioe) {
throw new EnvInjectException("Problem occurs on loading content", ioe);
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
result.put(processProperty(entry.getKey()), processProperty(entry.getValue()));
}
return result;
}
/**
* Get a map environment variables from the content
*
* @param content
* @return
* @throws EnvInjectException
*/
public Map<String, String> getVarsFromPropertiesContent(String content) throws EnvInjectException {
if (content == null) {
return null;
}
if (content.trim().length() == 0) {
return null;
}
content = processPath(content);
Map<String, String> result = new LinkedHashMap<String, String>();
StringReader stringReader = new StringReader(content);
SortedProperties properties = new SortedProperties();
try {
properties.load(stringReader);
} catch (IOException ioe) {
throw new EnvInjectException("Problem occurs on loading content", ioe);
} finally {
stringReader.close();
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
result.put(processProperty(entry.getKey()), processProperty(entry.getValue()));
}
return result;
}
private String processProperty(Object prop) {
if (prop == null) {
return null;
}
return String.valueOf(prop).trim();
}
private String processPath(String content) {
if (content == null) {
return null;
}
content = content.replace("\\", "\\\\");
return content.replace("\\\\\n", "\\\n");
}
}