1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStreamReader; |
6 |
|
import java.io.UnsupportedEncodingException; |
7 |
|
import java.nio.file.Files; |
8 |
|
import java.nio.file.Paths; |
9 |
|
import java.util.HashMap; |
10 |
|
import java.util.List; |
11 |
|
import java.util.Map; |
12 |
|
import java.util.stream.Collectors; |
13 |
|
import java.util.stream.Stream; |
14 |
|
import org.europa.together.business.Logger; |
15 |
|
import org.europa.together.business.PropertyReader; |
16 |
|
import org.europa.together.domain.LogLevel; |
17 |
|
import org.europa.together.exceptions.MisconfigurationException; |
18 |
|
import org.springframework.context.ApplicationContext; |
19 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
20 |
|
import org.springframework.stereotype.Repository; |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
@Repository |
|
|
| 98.5% |
Uncovered Elements: 2 (137) |
Complexity: 30 |
Complexity Density: 0.29 |
|
26 |
|
public class PropertyFileReader implements PropertyReader { |
27 |
|
|
28 |
|
private static final long serialVersionUID = 4L; |
29 |
|
private static final Logger LOGGER = new LogbackLogger(PropertyFileReader.class); |
30 |
|
|
31 |
|
private Map<String, String> propertyList = new HashMap<>(); |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
36 |
22 |
public PropertyFileReader() {... |
37 |
22 |
LOGGER.log("instance class", LogLevel.INFO); |
38 |
|
} |
39 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
40 |
5 |
@Override... |
41 |
|
public boolean addProperty(final String key, final String value) { |
42 |
5 |
boolean success = false; |
43 |
5 |
try { |
44 |
5 |
this.lookupForPropertyKey(key); |
45 |
|
} catch (Exception ex) { |
46 |
4 |
propertyList.put(key, value); |
47 |
4 |
LOGGER.catchException(ex); |
48 |
4 |
success = true; |
49 |
|
} |
50 |
5 |
return success; |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
53 |
5 |
@Override... |
54 |
|
public boolean addPropertyList(final Map<String, String> resource) { |
55 |
5 |
boolean success = false; |
56 |
5 |
int sizeOrginalList = propertyList.size(); |
57 |
5 |
int sizeNewList = resource.size(); |
58 |
5 |
int size = sizeOrginalList + sizeNewList; |
59 |
5 |
propertyList.putAll(resource); |
60 |
5 |
if (size > sizeOrginalList) { |
61 |
4 |
success = true; |
62 |
4 |
LOGGER.log(sizeNewList + " Properties appended.", LogLevel.DEBUG); |
63 |
|
} else { |
64 |
1 |
LOGGER.log("Could not append Properties. Size Original List: " |
65 |
|
+ sizeOrginalList + " : Size New List: " + sizeNewList, LogLevel.WARN); |
66 |
|
} |
67 |
5 |
return success; |
68 |
|
} |
69 |
|
|
|
|
| 92.9% |
Uncovered Elements: 2 (28) |
Complexity: 5 |
Complexity Density: 0.23 |
|
70 |
52 |
@Override... |
71 |
|
public boolean appendPropertiesFromClasspath(final String resource) |
72 |
|
throws UnsupportedEncodingException, IOException { |
73 |
52 |
boolean success = false; |
74 |
52 |
ApplicationContext context = new ClassPathXmlApplicationContext(); |
75 |
52 |
BufferedReader reader = new BufferedReader( |
76 |
|
new InputStreamReader( |
77 |
|
context.getResource(resource).getInputStream(), "UTF8") |
78 |
|
); |
79 |
49 |
String line; |
80 |
49 |
int count = 1; |
81 |
? |
while ((line = reader.readLine()) != null) { |
82 |
1104 |
String test = line; |
83 |
1104 |
if (test.isEmpty() || test.charAt(0) == '#') { |
84 |
299 |
continue; |
85 |
|
} |
86 |
805 |
String[] parts = test.split("="); |
87 |
805 |
String key = parts[0]; |
88 |
805 |
String value = ""; |
89 |
805 |
if (parts.length == 2) { |
90 |
720 |
value = parts[1]; |
91 |
|
} |
92 |
805 |
propertyList.put(key, value); |
93 |
805 |
count++; |
94 |
|
} |
95 |
49 |
reader.close(); |
96 |
49 |
String logMsg = "readPropertyFromClasspath(" + resource + ") " |
97 |
|
+ count + " Properties read."; |
98 |
49 |
LOGGER.log(logMsg, LogLevel.DEBUG); |
99 |
49 |
success = true; |
100 |
49 |
this.printPropertyList(); |
101 |
49 |
return success; |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 2 |
Complexity Density: 0.12 |
|
104 |
11 |
@Override... |
105 |
|
public boolean appendPropertiesFromFile(final String resource) |
106 |
|
throws IOException { |
107 |
11 |
boolean success = false; |
108 |
11 |
Stream<String> stream = Files.lines(Paths.get(resource)); |
109 |
3 |
List<String> content = stream |
110 |
|
.filter(line -> !line.startsWith("#")) |
111 |
|
.filter(line -> !line.isEmpty()) |
112 |
|
.collect(Collectors.toList()); |
113 |
3 |
int count = 1; |
114 |
3 |
for (String entry : content) { |
115 |
52 |
String[] parts = entry.split("="); |
116 |
52 |
String key = parts[0]; |
117 |
52 |
String value = ""; |
118 |
52 |
if (parts.length == 2) { |
119 |
47 |
value = parts[1]; |
120 |
|
} |
121 |
52 |
propertyList.put(key, value); |
122 |
52 |
count++; |
123 |
|
} |
124 |
3 |
String logMsg = "readPropertyFromFile(" + resource + ") " |
125 |
|
+ count + " Properties read."; |
126 |
3 |
LOGGER.log(logMsg, LogLevel.DEBUG); |
127 |
3 |
success = true; |
128 |
3 |
this.printPropertyList(); |
129 |
3 |
return success; |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
132 |
25 |
@Override... |
133 |
|
public boolean clear() { |
134 |
25 |
boolean success = false; |
135 |
25 |
if (propertyList.isEmpty()) { |
136 |
3 |
LOGGER.log("PropertyList is EMPTY, nothing to remove.", LogLevel.WARN); |
137 |
|
} else { |
138 |
22 |
propertyList.clear(); |
139 |
22 |
success = true; |
140 |
22 |
LOGGER.log("PropertyList cleaned.", LogLevel.TRACE); |
141 |
|
} |
142 |
25 |
return success; |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
145 |
3 |
@Override... |
146 |
|
public boolean removeProperty(final String key) { |
147 |
3 |
boolean success = false; |
148 |
3 |
try { |
149 |
3 |
this.lookupForPropertyKey(key); |
150 |
1 |
propertyList.remove(key); |
151 |
1 |
success = true; |
152 |
1 |
LOGGER.log(key + " successful removed.", LogLevel.DEBUG); |
153 |
|
} catch (Exception ex) { |
154 |
2 |
LOGGER.catchException(ex); |
155 |
|
} |
156 |
3 |
return success; |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
159 |
3 |
@Override... |
160 |
|
public boolean updateProperty(final String key, final String value) { |
161 |
3 |
try { |
162 |
3 |
this.lookupForPropertyKey(key); |
163 |
2 |
propertyList.put(key, value); |
164 |
2 |
LOGGER.log("Entry " + key + " will be updated.", LogLevel.INFO); |
165 |
|
} catch (Exception ex) { |
166 |
1 |
LOGGER.catchException(ex); |
167 |
1 |
propertyList.put(key, value); |
168 |
|
} |
169 |
3 |
return true; |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
23 |
@Override... |
173 |
|
public int count() { |
174 |
23 |
return propertyList.size(); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
177 |
8 |
@Override... |
178 |
|
public Boolean getPropertyAsBoolean(final String key) |
179 |
|
throws MisconfigurationException { |
180 |
8 |
this.lookupForPropertyKey(key); |
181 |
6 |
if (propertyList.get(key).matches("true|false|TRUE|FALSE|0")) { |
182 |
3 |
return Boolean.parseBoolean(propertyList.get(key)); |
183 |
3 |
} else if (propertyList.get(key).equals("1")) { |
184 |
1 |
return true; |
185 |
|
} else { |
186 |
2 |
throw new MisconfigurationException(key + " is not a Boolean value. (" |
187 |
|
+ propertyList.get(key) + ")"); |
188 |
|
} |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
191 |
8 |
@Override... |
192 |
|
public Double getPropertyAsDouble(final String key) |
193 |
|
throws MisconfigurationException { |
194 |
8 |
this.lookupForPropertyKey(key); |
195 |
7 |
return Double.parseDouble(propertyList.get(key)); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
198 |
11 |
@Override... |
199 |
|
public Float getPropertyAsFloat(final String key) |
200 |
|
throws MisconfigurationException { |
201 |
11 |
this.lookupForPropertyKey(key); |
202 |
9 |
return Float.parseFloat(propertyList.get(key)); |
203 |
|
} |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
205 |
8 |
@Override... |
206 |
|
public Integer getPropertyAsInt(final String key) |
207 |
|
throws MisconfigurationException { |
208 |
8 |
this.lookupForPropertyKey(key); |
209 |
7 |
return Integer.parseInt(propertyList.get(key)); |
210 |
|
} |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
212 |
107 |
@Override... |
213 |
|
public String getPropertyAsString(final String key) |
214 |
|
throws MisconfigurationException { |
215 |
107 |
this.lookupForPropertyKey(key); |
216 |
105 |
return propertyList.get(key); |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
8 |
@Override... |
220 |
|
public Map<String, String> getPropertyList() { |
221 |
8 |
return Map.copyOf(propertyList); |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
52 |
private void printPropertyList() {... |
225 |
52 |
LOGGER.log(propertyList.toString(), LogLevel.TRACE); |
226 |
|
} |
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
228 |
153 |
private boolean lookupForPropertyKey(final String key)... |
229 |
|
throws MisconfigurationException { |
230 |
153 |
boolean success = false; |
231 |
153 |
if (propertyList.containsKey(key)) { |
232 |
138 |
success = true; |
233 |
|
} else { |
234 |
15 |
LOGGER.log("Property Entry " + key + " don't exist.", LogLevel.WARN); |
235 |
15 |
throw new MisconfigurationException( |
236 |
|
"Configuration entry in property file not found."); |
237 |
|
} |
238 |
138 |
return success; |
239 |
|
} |
240 |
|
} |