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
41
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
57
58
59
60 public ConfigAccess(final File configFile, final String description) throws IOException {
61 this.configFile = configFile;
62 this.description = description;
63 FileInputStream stream = null;
64 try {
65 stream = new FileInputStream(configFile);
66 load(stream);
67 } catch (IOException e) {
68 System.out.println("no config file found, using default values");
69 } finally {
70 IoUtility.close(stream);
71 }
72 setString("configFileLocation", configFile.getCanonicalPath());
73 }
74
75
76
77
78
79
80 public final synchronized File getConfigFile() {
81 return configFile;
82 }
83
84
85
86
87
88
89 public final synchronized String getConfigDescription() {
90 return description;
91 }
92
93
94
95
96
97
98 private final Properties getProperties() {
99 return properties;
100 }
101
102
103
104
105
106
107
108
109 private final void load(final InputStream inStream) throws IOException {
110 getProperties().load(inStream);
111 }
112
113
114
115
116
117
118 public final synchronized void store() throws IOException {
119 OutputStream out = null;
120 try {
121 final File file = getConfigFile();
122 IoUtility.createNecessaryDirectories(file);
123 out = new FileOutputStream(file);
124 getProperties().store(out, getConfigDescription());
125 } finally {
126 if (out != null) {
127 try {
128 out.close();
129 } catch (IOException e) {
130 throw e;
131 } catch (Exception e) {
132 throw new IOException(e.toString());
133 }
134 }
135 }
136 }
137
138
139
140
141
142
143
144 public final synchronized String getString(final String name) {
145 return getProperties().getProperty(name);
146 }
147
148
149
150
151
152
153
154
155 public final synchronized String getString(final String name, final String defaultValue) {
156 final String value = getProperties().getProperty(name);
157 if (value == null) {
158 setString(name, defaultValue);
159 return defaultValue;
160 }
161 return value;
162 }
163
164
165
166
167
168
169
170 public final synchronized void setString(final String name, final String value) {
171 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
193
194
195 public final synchronized String[] getStringProperties(final String namePrefix) {
196 final List list = new ArrayList();
197 final Enumeration keys = getProperties().keys();
198 final List keyList = Collections.list(keys);
199 Collections.sort(keyList);
200 for (int i = 0; i < keyList.size(); i++) {
201 final String key = (String) keyList.get(i);
202 if (key.startsWith(namePrefix)) {
203 list.add(getProperties().get(key));
204 }
205 }
206 return (String []) list.toArray(new String[list.size()]);
207 }
208
209
210
211
212
213
214
215
216 public final synchronized Map getProperties(final String namePrefix) {
217 final Map result = new HashMap();
218 Iterator i = getProperties().entrySet().iterator();
219 while (i.hasNext()) {
220 Map.Entry entry = (Map.Entry) i.next();
221 final String name = String.valueOf(entry.getKey());
222 if (name.startsWith(namePrefix)) {
223 result.put(name.substring(namePrefix.length()), entry.getValue());
224 }
225 }
226 return result;
227 }
228
229
230
231
232
233
234
235 public final synchronized void setInteger(final String name, final int value) {
236 setString(name, "" + value);
237 }
238
239
240
241
242
243
244
245
246
247
248 public final synchronized int getInteger(final String name) {
249 final String intPropAsString = getProperties().getProperty(name);
250 if (intPropAsString == null) {
251 throw new NullPointerException("property \"" + name + "\" not found");
252 }
253 try {
254 return Integer.parseInt(intPropAsString);
255 } catch (NumberFormatException ex) {
256 throw new IllegalArgumentException(
257 "int property " + intPropAsString + " has invalid format");
258 }
259 }
260
261
262
263
264
265
266
267
268
269 public final synchronized int getInteger(final String name, final int defaultValue) {
270 final String intPropAsString = getProperties().getProperty(name);
271 if (intPropAsString == null || intPropAsString.length() == 0) {
272 setInteger(name, defaultValue);
273 return defaultValue;
274 }
275 try {
276 return Integer.parseInt(intPropAsString);
277 } catch (NumberFormatException ex) {
278 throw new IllegalArgumentException(
279 "Integer-Property " + intPropAsString + " has invalid format");
280 }
281 }
282
283
284
285
286
287
288 public final synchronized void removeProperty(final String name) {
289 getProperties().remove(name);
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 public final synchronized void removeProperties(final String namePrefix) {
306 final Enumeration keys = getProperties().keys();
307 while (keys.hasMoreElements()) {
308 final String key = (String) keys.nextElement();
309 if (key.startsWith(namePrefix)) {
310 getProperties().remove(key);
311 }
312 }
313 }
314
315 }