|
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 |
| |
|
38 |
| |
|
39 |
| |
|
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 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
27
| public ConfigAccess(final File configFile, final String description) throws IOException {
|
|
59 |
27
| this.configFile = configFile;
|
|
60 |
27
| this.description = description;
|
|
61 |
27
| try {
|
|
62 |
27
| load(new FileInputStream(configFile));
|
|
63 |
| } catch (IOException e) { |
|
64 |
0
| System.out.println("no config file found, using default values");
|
|
65 |
| } |
|
66 |
27
| setString("configFileLocation", configFile.getCanonicalPath());
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
27
| public final File getConfigFile() {
|
|
75 |
27
| return configFile;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
27
| public final String getConfigDescription() {
|
|
84 |
27
| return description;
|
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
1905
| private final Properties getProperties() {
|
|
93 |
1905
| return properties;
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
27
| private final void load(final InputStream inStream) throws IOException {
|
|
104 |
27
| getProperties().load(inStream);
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
27
| public final void store() throws IOException {
|
|
113 |
27
| OutputStream out = null;
|
|
114 |
27
| try {
|
|
115 |
27
| out = new FileOutputStream(getConfigFile());
|
|
116 |
27
| getProperties().store(out, getConfigDescription());
|
|
117 |
| } finally { |
|
118 |
27
| if (out != null) {
|
|
119 |
27
| try {
|
|
120 |
27
| 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 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
293
| public final String getString(final String name) {
|
|
137 |
293
| return getProperties().getProperty(name);
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
463
| public final String getString(final String name, final String defaultValue) {
|
|
148 |
463
| final String value = getProperties().getProperty(name);
|
|
149 |
463
| if (value == null) {
|
|
150 |
0
| setString(name, defaultValue);
|
|
151 |
0
| return defaultValue;
|
|
152 |
| } else { |
|
153 |
463
| return value;
|
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
265
| public final void setString(final String name, final String value) {
|
|
164 |
265
| getProperties().setProperty(name, value);
|
|
165 |
| } |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
57
| public final String[] getStringProperties(final String namePrefix) {
|
|
181 |
57
| final List list = new ArrayList();
|
|
182 |
57
| final Enumeration keys = getProperties().keys();
|
|
183 |
57
| final List keyList = Collections.list(keys);
|
|
184 |
57
| Collections.sort(keyList);
|
|
185 |
57
| for (int i = 0; i < keyList.size(); i++) {
|
|
186 |
1643
| final String key = (String) keyList.get(i);
|
|
187 |
1643
| if (key.startsWith(namePrefix)) {
|
|
188 |
500
| list.add(getProperties().get(key));
|
|
189 |
| } |
|
190 |
| } |
|
191 |
57
| return (String []) list.toArray(new String[] {});
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
0
| public final void setInteger(final String name, final int value) {
|
|
201 |
0
| setString(name, "" + value);
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
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 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
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 |
| |
|
254 |
| |
|
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 |
| |
|
271 |
| |
|
272 |
28
| public final void removeProperties(final String namePrefix) {
|
|
273 |
28
| final Enumeration keys = getProperties().keys();
|
|
274 |
28
| while (keys.hasMoreElements()) {
|
|
275 |
806
| final String key = (String) keys.nextElement();
|
|
276 |
806
| if (key.startsWith(namePrefix)) {
|
|
277 |
245
| getProperties().remove(key);
|
|
278 |
| } |
|
279 |
| } |
|
280 |
| } |
|
281 |
| |
|
282 |
| } |