1 10 11 package org.mule.util.monitor; 12 13 import java.beans.ExceptionListener ; 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.lang.ref.WeakReference ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Timer ; 24 import java.util.TimerTask ; 25 26 34 public class FileMonitor 35 { 36 private Timer timer; 37 private Map files; 38 private List listeners; 39 private long pollingInterval; 40 41 46 public FileMonitor(long pollingInterval) 47 { 48 files = new HashMap (); 49 listeners = new ArrayList (); 50 timer = new Timer (true); 51 this.pollingInterval = pollingInterval; 52 } 53 54 57 public void stop() 58 { 59 timer.cancel(); 60 } 61 62 public void start() 63 { 64 timer.schedule(new FileMonitorNotifier(), 0, pollingInterval); 65 } 66 67 75 public void addFile(File file) 76 { 77 if (!files.containsKey(file)) 78 { 79 long modifiedTime = file.exists() ? file.lastModified() : -1; 80 files.put(file, new Long (modifiedTime)); 81 } 82 } 83 84 89 public void removeFile(File file) 90 { 91 files.remove(file); 92 } 93 94 99 public void addListener(FileListener fileListener) 100 { 101 for (Iterator i = listeners.iterator(); i.hasNext();) 103 { 104 WeakReference reference = (WeakReference )i.next(); 105 FileListener listener = (FileListener)reference.get(); 106 if (listener == fileListener) 107 { 108 return; 109 } 110 } 111 112 listeners.add(new WeakReference (fileListener)); 115 } 116 117 122 public void removeListener(FileMonitor fileListener) 123 { 124 for (Iterator i = listeners.iterator(); i.hasNext();) 125 { 126 WeakReference reference = (WeakReference )i.next(); 127 FileMonitor listener = (FileMonitor)reference.get(); 128 if (listener == fileListener) 129 { 130 i.remove(); 131 break; 132 } 133 } 134 } 135 136 140 public class FileMonitorNotifier extends TimerTask 141 { 142 private ExceptionListener exceptionListener; 143 144 public FileMonitorNotifier() 145 { 146 super(); 147 } 148 149 public FileMonitorNotifier(ExceptionListener exceptionListener) 150 { 151 this.exceptionListener = exceptionListener; 152 } 153 154 public void run() 155 { 156 Collection fileKeys = new ArrayList (files.keySet()); 160 161 for (Iterator i = fileKeys.iterator(); i.hasNext();) 162 { 163 File file = (File )i.next(); 164 long lastModifiedTime = ((Long )files.get(file)).longValue(); 165 long newModifiedTime = file.exists() ? file.lastModified() : -1; 166 167 if (newModifiedTime != lastModifiedTime) 169 { 170 files.put(file, new Long (newModifiedTime)); 172 173 for (Iterator j = listeners.iterator(); j.hasNext();) 175 { 176 WeakReference reference = (WeakReference )j.next(); 177 FileListener listener = (FileListener)reference.get(); 178 179 if (listener == null) 181 { 182 j.remove(); 183 } 184 else 185 { 186 try 187 { 188 listener.fileChanged(file); 189 } 190 catch (IOException e) 191 { 192 if (exceptionListener != null) 193 { 194 exceptionListener.exceptionThrown(e); 195 } 196 } 197 } 198 } 199 } 200 } 201 } 202 } 203 } 204 | Popular Tags |