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 ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Vector ; 11 21 public class FileWatcher extends Thread { 22 23 private HashMap fileMap; 24 private HashMap fileListenerList; 25 private HashMap configMap; 26 private volatile Thread watcher; 27 private int interval = 1000; 28 private boolean running = true; 29 30 36 public FileWatcher() { 37 fileMap = new HashMap (); 38 configMap = new HashMap (); 39 fileListenerList = new HashMap (); 40 setDaemon(true); 41 start(); 42 } 43 44 public void addFile(String fileName) { 45 File file = new File (fileName); 46 if ( file.exists() ) { 47 long lastModified = file.lastModified(); 48 fileMap.put(fileName,new Long (lastModified)); 49 } 50 } 51 56 public void addFileListener( FileListener fileListener,String configurationName ) { 57 if ( !configMap.containsValue(configurationName)) { 58 List all = (List )fileListenerList.get(configurationName); 59 if ( all == null ) { 60 all = new Vector (); 61 } 62 all.add(fileListener); 63 fileListenerList.put(configurationName, all ); 64 String fn = configurationName; 65 if ( configurationName.equals("Default")) { 66 fn = "jlo_logging.xml"; 67 } 68 else { 69 fn += "_logging.xml"; 70 } 71 ClassLoader cl = getClass().getClassLoader(); 72 java.net.URL url = cl.getResource(fn); 73 if (url != null) { 74 File file = new File (url.getFile()); 75 addFile(file.getAbsolutePath()) ; 76 configMap.put(file,configurationName); 77 } 78 } 79 } 80 81 87 public void setInterval( int seconds ) { 88 this.interval = seconds*1000; 89 } 90 91 95 public void stopWatching() { 96 this.watcher = null; 97 } 98 99 103 public void start() { 104 watcher = new Thread ( this ); 105 watcher.setDaemon(true); 106 watcher.start(); 107 } 108 109 112 public void run() { 113 Thread thisThread = Thread.currentThread(); 114 while ( running ) { 116 try { 117 Thread.sleep(interval); 118 } catch (InterruptedException e){ 119 watcher = null; 121 } 122 Iterator it = fileMap.keySet().iterator(); 123 while ( it.hasNext() ) { 124 String fileName = (String )it.next(); 125 long lastModified = ((Long )fileMap.get(fileName)).longValue(); 126 lastModified = checkFile(fileName,lastModified); 127 fileMap.put(fileName,new Long (lastModified)); 128 } 129 } 130 } 131 132 137 public FileListener[] getFileListeners() { 138 return null; 141 } 142 143 private long checkFile(String fileName,long lm) { 144 File newFile = new File (fileName); 145 if( newFile.lastModified() > lm ) { 146 lm = newFile.lastModified(); 147 String configName = (String )configMap.get(newFile); 148 List all = (List )fileListenerList.get(configName); 149 Iterator 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 |