1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.commons.configuration; |
19 | |
|
20 | |
import java.io.Reader; |
21 | |
import java.io.Writer; |
22 | |
import java.io.File; |
23 | |
import java.io.InputStream; |
24 | |
import java.io.OutputStream; |
25 | |
import java.net.URL; |
26 | |
import java.util.Collection; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
|
30 | |
import org.apache.commons.configuration.event.ConfigurationEvent; |
31 | |
import org.apache.commons.configuration.event.ConfigurationListener; |
32 | |
import org.apache.commons.configuration.reloading.ReloadingStrategy; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public abstract class AbstractHierarchicalFileConfiguration |
47 | |
extends HierarchicalConfiguration |
48 | |
implements FileConfiguration, ConfigurationListener |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
private FileConfigurationDelegate delegate; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
protected AbstractHierarchicalFileConfiguration() |
60 | 746 | { |
61 | 746 | initialize(); |
62 | 746 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
protected AbstractHierarchicalFileConfiguration(HierarchicalConfiguration c) |
73 | |
{ |
74 | 8 | super(c); |
75 | 8 | initialize(); |
76 | 8 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public AbstractHierarchicalFileConfiguration(String fileName) throws ConfigurationException |
85 | |
{ |
86 | 7 | this(); |
87 | |
|
88 | 7 | delegate.setFileName(fileName); |
89 | |
|
90 | |
|
91 | 7 | load(); |
92 | 7 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public AbstractHierarchicalFileConfiguration(File file) throws ConfigurationException |
101 | |
{ |
102 | 132 | this(); |
103 | |
|
104 | 132 | setFile(file); |
105 | |
|
106 | |
|
107 | 132 | if (file.exists()) |
108 | |
{ |
109 | 131 | load(); |
110 | |
} |
111 | 132 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public AbstractHierarchicalFileConfiguration(URL url) throws ConfigurationException |
120 | |
{ |
121 | 1 | this(); |
122 | |
|
123 | 1 | setURL(url); |
124 | |
|
125 | |
|
126 | 1 | load(); |
127 | 1 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private void initialize() |
133 | |
{ |
134 | 754 | delegate = createDelegate(); |
135 | 754 | initDelegate(delegate); |
136 | 754 | } |
137 | |
|
138 | |
protected void addPropertyDirect(String key, Object obj) |
139 | |
{ |
140 | 3131 | super.addPropertyDirect(key, obj); |
141 | 3131 | delegate.possiblySave(); |
142 | 3131 | } |
143 | |
|
144 | |
public void clearProperty(String key) |
145 | |
{ |
146 | 639 | super.clearProperty(key); |
147 | 639 | delegate.possiblySave(); |
148 | 639 | } |
149 | |
|
150 | |
public void clearTree(String key) |
151 | |
{ |
152 | 1 | super.clearTree(key); |
153 | 1 | delegate.possiblySave(); |
154 | 1 | } |
155 | |
|
156 | |
public void setProperty(String key, Object value) |
157 | |
{ |
158 | 594 | super.setProperty(key, value); |
159 | 594 | delegate.possiblySave(); |
160 | 594 | } |
161 | |
|
162 | |
public void load() throws ConfigurationException |
163 | |
{ |
164 | 348 | delegate.load(); |
165 | 334 | } |
166 | |
|
167 | |
public void load(String fileName) throws ConfigurationException |
168 | |
{ |
169 | 2 | delegate.load(fileName); |
170 | 2 | } |
171 | |
|
172 | |
public void load(File file) throws ConfigurationException |
173 | |
{ |
174 | 9 | delegate.load(file); |
175 | 7 | } |
176 | |
|
177 | |
public void load(URL url) throws ConfigurationException |
178 | |
{ |
179 | 3 | delegate.load(url); |
180 | 3 | } |
181 | |
|
182 | |
public void load(InputStream in) throws ConfigurationException |
183 | |
{ |
184 | 0 | delegate.load(in); |
185 | 0 | } |
186 | |
|
187 | |
public void load(InputStream in, String encoding) throws ConfigurationException |
188 | |
{ |
189 | 1 | delegate.load(in, encoding); |
190 | 1 | } |
191 | |
|
192 | |
public void save() throws ConfigurationException |
193 | |
{ |
194 | 17 | delegate.save(); |
195 | 17 | } |
196 | |
|
197 | |
public void save(String fileName) throws ConfigurationException |
198 | |
{ |
199 | 7 | delegate.save(fileName); |
200 | 7 | } |
201 | |
|
202 | |
public void save(File file) throws ConfigurationException |
203 | |
{ |
204 | 22 | delegate.save(file); |
205 | 21 | } |
206 | |
|
207 | |
public void save(URL url) throws ConfigurationException |
208 | |
{ |
209 | 1 | delegate.save(url); |
210 | 1 | } |
211 | |
|
212 | |
public void save(OutputStream out) throws ConfigurationException |
213 | |
{ |
214 | 1 | delegate.save(out); |
215 | 1 | } |
216 | |
|
217 | |
public void save(OutputStream out, String encoding) throws ConfigurationException |
218 | |
{ |
219 | 1 | delegate.save(out, encoding); |
220 | 1 | } |
221 | |
|
222 | |
public String getFileName() |
223 | |
{ |
224 | 6 | return delegate.getFileName(); |
225 | |
} |
226 | |
|
227 | |
public void setFileName(String fileName) |
228 | |
{ |
229 | 104 | delegate.setFileName(fileName); |
230 | 104 | } |
231 | |
|
232 | |
public String getBasePath() |
233 | |
{ |
234 | 56 | return delegate.getBasePath(); |
235 | |
} |
236 | |
|
237 | |
public void setBasePath(String basePath) |
238 | |
{ |
239 | 53 | delegate.setBasePath(basePath); |
240 | 53 | } |
241 | |
|
242 | |
public File getFile() |
243 | |
{ |
244 | 4 | return delegate.getFile(); |
245 | |
} |
246 | |
|
247 | |
public void setFile(File file) |
248 | |
{ |
249 | 235 | delegate.setFile(file); |
250 | 235 | } |
251 | |
|
252 | |
public URL getURL() |
253 | |
{ |
254 | 1 | return delegate.getURL(); |
255 | |
} |
256 | |
|
257 | |
public void setURL(URL url) |
258 | |
{ |
259 | 7 | delegate.setURL(url); |
260 | 7 | } |
261 | |
|
262 | |
public void setAutoSave(boolean autoSave) |
263 | |
{ |
264 | 5 | delegate.setAutoSave(autoSave); |
265 | 5 | } |
266 | |
|
267 | |
public boolean isAutoSave() |
268 | |
{ |
269 | 2 | return delegate.isAutoSave(); |
270 | |
} |
271 | |
|
272 | |
public ReloadingStrategy getReloadingStrategy() |
273 | |
{ |
274 | 3 | return delegate.getReloadingStrategy(); |
275 | |
} |
276 | |
|
277 | |
public void setReloadingStrategy(ReloadingStrategy strategy) |
278 | |
{ |
279 | 15 | delegate.setReloadingStrategy(strategy); |
280 | 15 | } |
281 | |
|
282 | |
public void reload() |
283 | |
{ |
284 | 6466 | setDetailEvents(false); |
285 | |
try |
286 | |
{ |
287 | 6466 | delegate.reload(); |
288 | |
} |
289 | |
finally |
290 | |
{ |
291 | 6466 | setDetailEvents(true); |
292 | 6466 | } |
293 | 6466 | } |
294 | |
|
295 | |
public String getEncoding() |
296 | |
{ |
297 | 61 | return delegate.getEncoding(); |
298 | |
} |
299 | |
|
300 | |
public void setEncoding(String encoding) |
301 | |
{ |
302 | 3 | delegate.setEncoding(encoding); |
303 | 3 | } |
304 | |
|
305 | |
public boolean containsKey(String key) |
306 | |
{ |
307 | 841 | reload(); |
308 | 841 | return super.containsKey(key); |
309 | |
} |
310 | |
|
311 | |
public Iterator getKeys(String prefix) |
312 | |
{ |
313 | 5 | reload(); |
314 | 5 | return super.getKeys(prefix); |
315 | |
} |
316 | |
|
317 | |
public Object getProperty(String key) |
318 | |
{ |
319 | 2098 | reload(); |
320 | 2098 | return super.getProperty(key); |
321 | |
} |
322 | |
|
323 | |
public boolean isEmpty() |
324 | |
{ |
325 | 10 | reload(); |
326 | 10 | return super.isEmpty(); |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public void addNodes(String key, Collection nodes) |
338 | |
{ |
339 | 3 | super.addNodes(key, nodes); |
340 | 3 | delegate.possiblySave(); |
341 | 3 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
protected List fetchNodeList(String key) |
351 | |
{ |
352 | 3512 | reload(); |
353 | 3512 | return super.fetchNodeList(key); |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
protected void subnodeConfigurationChanged(ConfigurationEvent event) |
364 | |
{ |
365 | 4 | delegate.possiblySave(); |
366 | 4 | super.subnodeConfigurationChanged(event); |
367 | 4 | } |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
protected FileConfigurationDelegate createDelegate() |
379 | |
{ |
380 | 385 | return new FileConfigurationDelegate(); |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
private void initDelegate(FileConfigurationDelegate del) |
389 | |
{ |
390 | 754 | del.addConfigurationListener(this); |
391 | 754 | } |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
public void configurationChanged(ConfigurationEvent event) |
401 | |
{ |
402 | |
|
403 | 56 | setDetailEvents(true); |
404 | |
try |
405 | |
{ |
406 | 56 | fireEvent(event.getType(), event.getPropertyName(), event |
407 | |
.getPropertyValue(), event.isBeforeUpdate()); |
408 | |
} |
409 | |
finally |
410 | |
{ |
411 | 56 | setDetailEvents(false); |
412 | 56 | } |
413 | 56 | } |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
protected FileConfigurationDelegate getDelegate() |
421 | |
{ |
422 | 319 | return delegate; |
423 | |
} |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
protected void setDelegate(FileConfigurationDelegate delegate) |
430 | |
{ |
431 | 2 | this.delegate = delegate; |
432 | 2 | } |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | 756 | protected class FileConfigurationDelegate extends AbstractFileConfiguration |
440 | |
{ |
441 | |
public void load(Reader in) throws ConfigurationException |
442 | |
{ |
443 | 63 | AbstractHierarchicalFileConfiguration.this.load(in); |
444 | 63 | } |
445 | |
|
446 | |
public void save(Writer out) throws ConfigurationException |
447 | |
{ |
448 | 57 | AbstractHierarchicalFileConfiguration.this.save(out); |
449 | 56 | } |
450 | |
|
451 | |
public void clear() |
452 | |
{ |
453 | 28 | AbstractHierarchicalFileConfiguration.this.clear(); |
454 | 28 | } |
455 | |
} |
456 | |
} |