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