1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File PropertyFileReader.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

18
102
17
1
240
213
30
0.29
6
17
1.76

Classes

Class Line # Actions
PropertyFileReader 26 102 0% 30 2
0.9854014598.5%
 

Contributing tests

This file is covered by 49 tests. .

Source view

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    * Implementation of the Property Reader.
24    */
25    @Repository
 
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    * Constructor.
35    */
 
36  22 toggle public PropertyFileReader() {
37  22 LOGGER.log("instance class", LogLevel.INFO);
38    }
39   
 
40  5 toggle @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   
 
53  5 toggle @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   
 
70  52 toggle @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   
 
104  11 toggle @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   
 
132  25 toggle @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   
 
145  3 toggle @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   
 
159  3 toggle @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   
 
172  23 toggle @Override
173    public int count() {
174  23 return propertyList.size();
175    }
176   
 
177  8 toggle @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   
 
191  8 toggle @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   
 
198  11 toggle @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   
 
205  8 toggle @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   
 
212  107 toggle @Override
213    public String getPropertyAsString(final String key)
214    throws MisconfigurationException {
215  107 this.lookupForPropertyKey(key);
216  105 return propertyList.get(key);
217    }
218   
 
219  8 toggle @Override
220    public Map<String, String> getPropertyList() {
221  8 return Map.copyOf(propertyList);
222    }
223   
 
224  52 toggle private void printPropertyList() {
225  52 LOGGER.log(propertyList.toString(), LogLevel.TRACE);
226    }
227   
 
228  153 toggle 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    }