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