1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.qedeq.kernel.config; |
19 |
|
|
20 |
|
import java.io.File; |
21 |
|
import java.io.FileInputStream; |
22 |
|
import java.io.FileOutputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.OutputStream; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.Enumeration; |
29 |
|
import java.util.List; |
30 |
|
import java.util.Properties; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@author |
39 |
|
|
|
|
| 48,8% |
Uncovered Elements: 44 (86) |
Complexity: 28 |
Complexity Density: 0,51 |
|
40 |
|
final class ConfigAccess { |
41 |
|
|
42 |
|
|
43 |
|
private final File configFile; |
44 |
|
|
45 |
|
|
46 |
|
private Properties properties = new Properties(); |
47 |
|
|
48 |
|
|
49 |
|
private final String description; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@param |
55 |
|
@param |
56 |
|
@throws |
57 |
|
|
|
|
| 83,3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
|
58 |
39
|
public ConfigAccess(final File configFile, final String description) throws IOException {... |
59 |
39
|
this.configFile = configFile; |
60 |
39
|
this.description = description; |
61 |
39
|
try { |
62 |
39
|
load(new FileInputStream(configFile)); |
63 |
|
} catch (IOException e) { |
64 |
0
|
System.out.println("no config file found, using default values"); |
65 |
|
} |
66 |
39
|
setString("configFileLocation", configFile.getCanonicalPath()); |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@return |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
39
|
public final File getConfigFile() {... |
75 |
39
|
return configFile; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@return |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
39
|
public final String getConfigDescription() {... |
84 |
39
|
return description; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@return |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
697
|
private final Properties getProperties() {... |
93 |
697
|
return properties; |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@param |
101 |
|
@throws |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
39
|
private final void load(final InputStream inStream) throws IOException {... |
104 |
39
|
getProperties().load(inStream); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
@throws |
111 |
|
|
|
|
| 72,7% |
Uncovered Elements: 3 (11) |
Complexity: 4 |
Complexity Density: 0,44 |
|
112 |
39
|
public final void store() throws IOException {... |
113 |
39
|
OutputStream out = null; |
114 |
39
|
try { |
115 |
39
|
out = new FileOutputStream(getConfigFile()); |
116 |
39
|
getProperties().store(out, getConfigDescription()); |
117 |
|
} finally { |
118 |
39
|
if (out != null) { |
119 |
39
|
try { |
120 |
39
|
out.close(); |
121 |
|
} catch (IOException e) { |
122 |
0
|
throw e; |
123 |
|
} catch (Exception e) { |
124 |
0
|
throw new IOException(e.toString()); |
125 |
|
} |
126 |
|
} |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@return |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
230
|
public final String getString(final String name) {... |
137 |
230
|
return getProperties().getProperty(name); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
@param |
144 |
|
@param |
145 |
|
@return |
146 |
|
|
|
|
| 57,1% |
Uncovered Elements: 3 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
147 |
39
|
public final String getString(final String name, final String defaultValue) {... |
148 |
39
|
final String value = getProperties().getProperty(name); |
149 |
39
|
if (value == null) { |
150 |
0
|
setString(name, defaultValue); |
151 |
0
|
return defaultValue; |
152 |
|
} else { |
153 |
39
|
return value; |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
@param |
161 |
|
@param |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
195
|
public final void setString(final String name, final String value) {... |
164 |
195
|
getProperties().setProperty(name, value); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@param |
178 |
|
@return |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0,33 |
|
180 |
0
|
public final String[] getStringProperties(final String namePrefix) {... |
181 |
0
|
final List list = new ArrayList(); |
182 |
0
|
final Enumeration keys = getProperties().keys(); |
183 |
0
|
final List keyList = Collections.list(keys); |
184 |
0
|
Collections.sort(keyList); |
185 |
0
|
for (int i = 0; i < keyList.size(); i++) { |
186 |
0
|
final String key = (String) keyList.get(i); |
187 |
0
|
if (key.startsWith(namePrefix)) { |
188 |
0
|
list.add(getProperties().get(key)); |
189 |
|
} |
190 |
|
} |
191 |
0
|
return (String []) list.toArray(new String[] {}); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param |
199 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
200 |
0
|
public final void setInteger(final String name, final int value) {... |
201 |
0
|
setString(name, "" + value); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
@param |
209 |
|
@return |
210 |
|
@throws |
211 |
|
@throws |
212 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
213 |
0
|
public final int getInteger(final String name) {... |
214 |
0
|
final String intPropAsString = getProperties().getProperty(name); |
215 |
0
|
if (intPropAsString != null) { |
216 |
0
|
try { |
217 |
0
|
return Integer.parseInt(intPropAsString); |
218 |
|
} catch (NumberFormatException ex) { |
219 |
0
|
throw new IllegalArgumentException( |
220 |
|
"int property " + intPropAsString + " has invalid format"); |
221 |
|
} |
222 |
|
} else { |
223 |
0
|
throw new NullPointerException("property \"" + name + "\" not found"); |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
@param |
232 |
|
@return |
233 |
|
@throws |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,43 |
|
235 |
0
|
public final int getInteger(final String name, final int defaultValue) {... |
236 |
0
|
final String intPropAsString = getProperties().getProperty(name); |
237 |
0
|
if (intPropAsString != null) { |
238 |
0
|
try { |
239 |
0
|
return Integer.parseInt(intPropAsString); |
240 |
|
} catch (NumberFormatException ex) { |
241 |
0
|
throw new IllegalArgumentException( |
242 |
|
"Integer-Property " + intPropAsString + " has invalid format"); |
243 |
|
} |
244 |
|
} else { |
245 |
0
|
setInteger(name, defaultValue); |
246 |
0
|
return defaultValue; |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
0
|
public final void removeProperty(final String name) {... |
256 |
0
|
getProperties().remove(name); |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
@param |
271 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
272 |
39
|
public final void removeProperties(final String namePrefix) {... |
273 |
39
|
final Enumeration keys = getProperties().keys(); |
274 |
233
|
while (keys.hasMoreElements()) { |
275 |
194
|
final String key = (String) keys.nextElement(); |
276 |
194
|
if (key.startsWith(namePrefix)) { |
277 |
116
|
getProperties().remove(key); |
278 |
|
} |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
} |