KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > FileMonitor


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.util;
19
20 import freecs.Server;
21 import freecs.interfaces.IReloadable;
22 import java.util.Vector JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.io.File JavaDoc;
25
26 public class FileMonitor extends Thread JavaDoc {
27     private Vector JavaDoc watchlist;
28     private static final FileMonitor fm = new FileMonitor();
29
30     public FileMonitor () {
31         watchlist = new Vector JavaDoc ();
32     }
33
34     public static FileMonitor getFileMonitor () {
35         if (!fm.isAlive()) {
36             fm.setName("FileMonitor");
37             fm.setPriority (MIN_PRIORITY);
38             fm.start ();
39         }
40         return fm;
41     }
42
43     public void addReloadable (IReloadable r) {
44         if (!watchlist.contains(r))
45             watchlist.addElement (r);
46     }
47
48     public void run () {
49         long lastMessage=0;
50         while (Server.srv.isRunning ()) {
51             if (Server.DEBUG || lastMessage + 5000 > System.currentTimeMillis()) {
52                 Server.log (this, "loopstart", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
53                 lastMessage = System.currentTimeMillis();
54             }
55             for (Enumeration JavaDoc e = watchlist.elements (); e.hasMoreElements (); ) {
56                 IReloadable cr = (IReloadable) e.nextElement ();
57                 File JavaDoc cf = cr.getFile ();
58                 if (cf == null) {
59                     StringBuffer JavaDoc tsb = new StringBuffer JavaDoc (": IReloadable has no file! "). append (cr.toString ());
60                     Server.log (this, tsb.toString (), Server.MSG_ERROR, Server.LVL_MAJOR);
61                     watchlist.remove (cr);
62                     continue;
63                 }
64                 boolean wasPresent = cr.filePresent();
65                 if (wasPresent && !cf.exists()) try {
66                     cr.removed();
67                     removeMonitor(cr);
68                     continue;
69                 } catch (Exception JavaDoc ex) {
70                     Server.debug (this, "remove for " + cf.getName() + " caused exception", ex, Server.MSG_ERROR, Server.LVL_MAJOR);
71                 }
72                 if (!wasPresent && cf.exists()) try {
73                     cr.created();
74                     continue;
75                 } catch (Exception JavaDoc ex) {
76                     Server.debug (this, "created for " + cf.getName() + " caused exception", ex, Server.MSG_ERROR, Server.LVL_MAJOR);
77                 }
78                 if (cf.lastModified () != cr.lastModified ()) try {
79                     cr.changed ();
80                 } catch (Exception JavaDoc ex) {
81                     Server.debug (this, "changed for " + cf.getName() + " caused exception", ex, Server.MSG_ERROR, Server.LVL_MAJOR);
82                 }
83             }
84             try {
85                 Thread.sleep (Server.srv.FILE_CHECK_INTERVAL);
86             } catch (Exception JavaDoc e) { }
87         }
88     }
89     
90     public String JavaDoc toString() {
91         return "[FileMonitor]";
92     }
93
94     /**
95      * @param set
96      */

97     public void removeMonitor(Object JavaDoc obj) {
98         this.watchlist.remove(obj);
99     }
100 }
Popular Tags