KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > FileWatcher


1 package org.jzonic.jlo;
2
3 import org.jzonic.jlo.events.FileListener;
4 import org.jzonic.jlo.events.FileListenerEvent;
5
6 import java.io.File JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Vector JavaDoc;
11 /**
12  * A class which implements an event dispatching mechanism to all
13  * classes supporting the FileListener interface. This class will
14  * notify all FileListeners when the configuration changes. Once
15  * the FileWatcher has been shutdown, the class needs to be
16  * reinstanciated and restarted.
17  *
18  * @author Andreas Mecky <andreas.mecky@xcom.de>
19  * @author Terry R. Dye <terry.dye@xcom.de>
20  */

21 public class FileWatcher extends Thread JavaDoc {
22     
23     private HashMap JavaDoc fileMap;
24     private HashMap JavaDoc fileListenerList;
25     private HashMap JavaDoc configMap;
26     private volatile Thread JavaDoc watcher;
27     private int interval = 1000;
28     private boolean running = true;
29         
30     /**
31      * Creates a new instance of FileWatcher by calling the FileWatcher( File )
32      * Constructor.
33      *
34      * @param filename A String representing the path to the file to be watched.
35      */

36     public FileWatcher() {
37         fileMap = new HashMap JavaDoc();
38         configMap = new HashMap JavaDoc();
39         fileListenerList = new HashMap JavaDoc();
40         setDaemon(true);
41         start();
42     }
43     
44     public void addFile(String JavaDoc fileName) {
45         File JavaDoc file = new File JavaDoc(fileName);
46         if ( file.exists() ) {
47             long lastModified = file.lastModified();
48             fileMap.put(fileName,new Long JavaDoc(lastModified));
49         }
50     }
51     /**
52      * Adds FileListener
53      *
54      * @param fileListener The FileListener
55      */

56     public void addFileListener( FileListener fileListener,String JavaDoc configurationName ) {
57         if ( !configMap.containsValue(configurationName)) {
58             List JavaDoc all = (List JavaDoc)fileListenerList.get(configurationName);
59             if ( all == null ) {
60                 all = new Vector JavaDoc();
61             }
62             all.add(fileListener);
63             fileListenerList.put(configurationName, all );
64             String JavaDoc fn = configurationName;
65             if ( configurationName.equals("Default")) {
66                 fn = "jlo_logging.xml";
67             }
68             else {
69                 fn += "_logging.xml";
70             }
71             ClassLoader JavaDoc cl = getClass().getClassLoader();
72             java.net.URL JavaDoc url = cl.getResource(fn);
73             if (url != null) {
74                 File JavaDoc file = new File JavaDoc(url.getFile());
75                 addFile(file.getAbsolutePath()) ;
76                 configMap.put(file,configurationName);
77             }
78         }
79     }
80     
81     /**
82      * Set the timer interval. The default is 10 seconds
83      *
84      * @param seconds The number of seconds to set the interval when
85      * to check for the changes to the file.
86      */

87     public void setInterval( int seconds ) {
88         this.interval = seconds*1000;
89     }
90     
91     /**
92      * Tell thread to stop watching. Currently once a Thread is started
93      * and stopped, a new FileWatcher will be required.
94      */

95     public void stopWatching() {
96         this.watcher = null;
97     }
98     
99     /**
100      * Start the Thread on its journey to scan for changes to the
101      * file it is watching.
102      */

103     public void start() {
104         watcher = new Thread JavaDoc( this );
105         watcher.setDaemon(true);
106         watcher.start();
107     }
108     
109     /**
110      * Start the thread to call checkFile()
111      */

112     public void run() {
113         Thread JavaDoc thisThread = Thread.currentThread();
114         //while (thisThread == watcher) {
115
while ( running ) {
116             try {
117                 Thread.sleep(interval);
118             } catch (InterruptedException JavaDoc e){
119                 // can't do much from here with Exception
120
watcher = null;
121             }
122             Iterator JavaDoc it = fileMap.keySet().iterator();
123             while ( it.hasNext() ) {
124                 String JavaDoc fileName = (String JavaDoc)it.next();
125                 long lastModified = ((Long JavaDoc)fileMap.get(fileName)).longValue();
126                 lastModified = checkFile(fileName,lastModified);
127                 fileMap.put(fileName,new Long JavaDoc(lastModified));
128             }
129         }
130     }
131     
132     /**
133      * Retrieve an array of FileListeners.
134      *
135      * @return FileListeners as array of FileListener
136      */

137     public FileListener[] getFileListeners() {
138         //return (FileListener[])fileListenerList.toArray();
139
//FIXME: fix the bug and get all listeners
140
return null;
141     }
142     
143     private long checkFile(String JavaDoc fileName,long lm) {
144         File JavaDoc newFile = new File JavaDoc(fileName);
145         if( newFile.lastModified() > lm ) {
146             lm = newFile.lastModified();
147             String JavaDoc configName = (String JavaDoc)configMap.get(newFile);
148             List JavaDoc all = (List JavaDoc)fileListenerList.get(configName);
149             Iterator JavaDoc iterator = all.iterator();
150             while( iterator.hasNext() ) {
151                 FileListener listener = (FileListener)iterator.next();
152                 listener.fileChanged( new FileListenerEvent( newFile, configName) );
153             }
154         }
155         return lm;
156     }
157 }
158
Popular Tags