1 18 package freecs.util; 19 20 import freecs.Server; 21 import freecs.interfaces.IReloadable; 22 import java.util.Vector ; 23 import java.util.Enumeration ; 24 import java.io.File ; 25 26 public class FileMonitor extends Thread { 27 private Vector watchlist; 28 private static final FileMonitor fm = new FileMonitor(); 29 30 public FileMonitor () { 31 watchlist = new Vector (); 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 e = watchlist.elements (); e.hasMoreElements (); ) { 56 IReloadable cr = (IReloadable) e.nextElement (); 57 File cf = cr.getFile (); 58 if (cf == null) { 59 StringBuffer tsb = new StringBuffer (": 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 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 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 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 e) { } 87 } 88 } 89 90 public String toString() { 91 return "[FileMonitor]"; 92 } 93 94 97 public void removeMonitor(Object obj) { 98 this.watchlist.remove(obj); 99 } 100 } | Popular Tags |