|
1 |
| package org.jconfig.handler; |
|
2 |
| |
|
3 |
| import java.io.File; |
|
4 |
| |
|
5 |
| import org.jconfig.ConfigurationManagerException; |
|
6 |
| import org.jconfig.FileWatcher; |
|
7 |
| import org.jconfig.event.FileListener; |
|
8 |
| import org.jconfig.event.FileListenerEvent; |
|
9 |
| import org.jconfig.utils.ConfigurationUtils; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| public abstract class AbstractHandler implements ConfigurationHandler, FileListener { |
|
15 |
| |
|
16 |
| private FileWatcher watcher = null; |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
0
| public void fileChanged(FileListenerEvent event) {
|
|
22 |
0
| try {
|
|
23 |
| |
|
24 |
0
| load(null);
|
|
25 |
| } catch (ConfigurationManagerException e) { |
|
26 |
| |
|
27 |
| } |
|
28 |
| } |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
37
| public void addFileListener( FileListener fileListener ) {
|
|
35 |
37
| String val = ConfigurationUtils.getConfigProperty("jconfig.filewatcher","true");
|
|
36 |
37
| if ( val.equalsIgnoreCase("true") ) {
|
|
37 |
37
| watcher = new FileWatcher( getFile() );
|
|
38 |
37
| watcher.addFileListener( fileListener );
|
|
39 |
37
| watcher.start();
|
|
40 |
| } |
|
41 |
| } |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| public void fireFireChangedEvent( FileListenerEvent event ) {
|
|
48 |
0
| if( watcher == null ) {
|
|
49 |
0
| throw new IllegalStateException("FileWatcher has not been set");
|
|
50 |
| } else { |
|
51 |
0
| FileListener[] listener = watcher.getFileListeners();
|
|
52 |
0
| int num = listener.length;
|
|
53 |
0
| for( int i = 0 ; i < num ; i++ ) {
|
|
54 |
0
| listener[i].fileChanged( event );
|
|
55 |
| } |
|
56 |
| } |
|
57 |
| } |
|
58 |
| |
|
59 |
| public abstract File getFile(); |
|
60 |
| } |