|
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 |
| import org.qedeq.kernel.trace.Trace; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| final class ConfigAccess { |
|
43 |
| |
|
44 |
| |
|
45 |
| private final File configDirectory; |
|
46 |
| |
|
47 |
| |
|
48 |
| private final String configFileName; |
|
49 |
| |
|
50 |
| |
|
51 |
| private Properties properties = new Properties(); |
|
52 |
| |
|
53 |
| |
|
54 |
| private final String description; |
|
55 |
| |
|
56 |
| |
|
57 |
| private boolean noTrace = true; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
2
| public ConfigAccess(final File configDirectory, final String configFileName,
|
|
68 |
| final String description) throws IOException { |
|
69 |
2
| this.configDirectory = configDirectory;
|
|
70 |
2
| this.configFileName = configFileName;
|
|
71 |
2
| this.description = description;
|
|
72 |
2
| try {
|
|
73 |
2
| load(new FileInputStream(new File(configDirectory, configFileName)));
|
|
74 |
| } catch (IOException e) { |
|
75 |
1
| Trace.info(this, "ConfigAccess(File, String, String)",
|
|
76 |
| "no config file found, using default values"); |
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
2
| setString("startDirectory", configDirectory.getCanonicalPath());
|
|
86 |
2
| setString("configFileLocation", new File(configDirectory, configFileName)
|
|
87 |
| .getCanonicalPath()); |
|
88 |
2
| noTrace = false;
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
2
| public final File getConfigDirectory() {
|
|
97 |
2
| return configDirectory;
|
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
2
| public final String getFileName() {
|
|
106 |
2
| return configFileName;
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
2
| public final File getConfigFile() {
|
|
115 |
2
| return new File(getConfigDirectory(), getFileName());
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
2
| public final String getConfigDescription() {
|
|
124 |
2
| return description;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
344
| private final Properties getProperties() {
|
|
133 |
344
| return properties;
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
1
| private final void load(final InputStream inStream) throws IOException {
|
|
144 |
1
| getProperties().load(inStream);
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
0
| private final void load(final String resource, final Class resourceRequestor)
|
|
157 |
| throws IOException { |
|
158 |
0
| final InputStream stream
|
|
159 |
| = resourceRequestor.getResourceAsStream(resource); |
|
160 |
0
| if (stream == null) {
|
|
161 |
0
| throw new IOException("ressource was not found: " + resource);
|
|
162 |
| } |
|
163 |
0
| try {
|
|
164 |
0
| getProperties().load(stream);
|
|
165 |
| } catch (IllegalArgumentException e) { |
|
166 |
0
| throw new IOException(e.toString());
|
|
167 |
| } |
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
2
| public final void store() throws IOException {
|
|
176 |
2
| store(getConfigFile(), getConfigDescription());
|
|
177 |
| } |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
2
| private final void store(final File resource, final String header)
|
|
187 |
| throws IOException { |
|
188 |
| |
|
189 |
2
| OutputStream stream = null;
|
|
190 |
2
| try {
|
|
191 |
2
| stream = new FileOutputStream(resource);
|
|
192 |
2
| getProperties().store(stream, header);
|
|
193 |
| } finally { |
|
194 |
2
| if (stream != null) {
|
|
195 |
2
| try {
|
|
196 |
2
| stream.close();
|
|
197 |
| } catch (Exception e) { |
|
198 |
| |
|
199 |
| } |
|
200 |
| } |
|
201 |
| } |
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
43
| public final String getString(final String name) {
|
|
211 |
43
| return getProperties().getProperty(name);
|
|
212 |
| } |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
2
| public final String getString(final String name, final String defaultValue) {
|
|
222 |
2
| final String method = "getString(String, String)";
|
|
223 |
2
| try {
|
|
224 |
2
| Trace.begin(this, method);
|
|
225 |
2
| Trace.param(this, method, "name", name);
|
|
226 |
2
| Trace.param(this, method, "defaultValue", defaultValue);
|
|
227 |
2
| final String value = getProperties().getProperty(name);
|
|
228 |
2
| if (value == null) {
|
|
229 |
1
| setString(name, defaultValue);
|
|
230 |
1
| return defaultValue;
|
|
231 |
| } else { |
|
232 |
1
| return value;
|
|
233 |
| } |
|
234 |
| } finally { |
|
235 |
2
| Trace.end(this, method);
|
|
236 |
| } |
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
144
| public final void setString(final String name, final String value) {
|
|
247 |
144
| final String method = "setString(String, String)";
|
|
248 |
144
| try {
|
|
249 |
144
| if (!noTrace) {
|
|
250 |
140
| Trace.begin(this, method);
|
|
251 |
140
| Trace.param(this, method, "name", name);
|
|
252 |
140
| Trace.param(this, method, "value", value);
|
|
253 |
| } |
|
254 |
144
| getProperties().setProperty(name, value);
|
|
255 |
| } finally { |
|
256 |
144
| if (!noTrace) {
|
|
257 |
140
| Trace.end(this, method);
|
|
258 |
| } |
|
259 |
| } |
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
5
| public final String[] getStringProperties(final String namePrefix) {
|
|
276 |
5
| final List list = new ArrayList();
|
|
277 |
5
| final Enumeration keys = getProperties().keys();
|
|
278 |
5
| final List keyList = Collections.list(keys);
|
|
279 |
5
| Collections.sort(keyList);
|
|
280 |
5
| for (int i = 0; i < keyList.size(); i++) {
|
|
281 |
50
| final String key = (String) keyList.get(i);
|
|
282 |
50
| if (key.startsWith(namePrefix)) {
|
|
283 |
16
| list.add(getProperties().get(key));
|
|
284 |
| } |
|
285 |
| } |
|
286 |
5
| return (String []) list.toArray(new String[] {});
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
0
| public final void setInteger(final String name, final int value) {
|
|
296 |
0
| setString(name, "" + value);
|
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
0
| public final int getInteger(final String name) {
|
|
309 |
0
| final String intPropAsString = getProperties().getProperty(name);
|
|
310 |
0
| if (intPropAsString != null) {
|
|
311 |
0
| try {
|
|
312 |
0
| return Integer.parseInt(intPropAsString);
|
|
313 |
| } catch (NumberFormatException ex) { |
|
314 |
0
| throw new IllegalArgumentException(
|
|
315 |
| "int property " + intPropAsString + " has invalid format"); |
|
316 |
| } |
|
317 |
| } else { |
|
318 |
0
| throw new NullPointerException("property \"" + name + "\" not found");
|
|
319 |
| } |
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
0
| public final int getInteger(final String name, final int defaultValue) {
|
|
331 |
0
| final String intPropAsString = getProperties().getProperty(name);
|
|
332 |
0
| if (intPropAsString != null) {
|
|
333 |
0
| try {
|
|
334 |
0
| return Integer.parseInt(intPropAsString);
|
|
335 |
| } catch (NumberFormatException ex) { |
|
336 |
0
| throw new IllegalArgumentException(
|
|
337 |
| "Integer-Property " + intPropAsString + " has invalid format"); |
|
338 |
| } |
|
339 |
| } else { |
|
340 |
0
| setInteger(name, defaultValue);
|
|
341 |
0
| return defaultValue;
|
|
342 |
| } |
|
343 |
| } |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
0
| public final void removeProperty(final String name) {
|
|
351 |
0
| getProperties().remove(name);
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
17
| public final void removeProperties(final String namePrefix) {
|
|
368 |
17
| final Enumeration keys = getProperties().keys();
|
|
369 |
17
| while (keys.hasMoreElements()) {
|
|
370 |
185
| final String key = (String) keys.nextElement();
|
|
371 |
185
| if (key.startsWith(namePrefix)) {
|
|
372 |
114
| getProperties().remove(key);
|
|
373 |
| } |
|
374 |
| } |
|
375 |
| } |
|
376 |
| |
|
377 |
| } |