1 /**
2 * Mar 14, 20119:35:23 AM
3 * @author gaoyb(www.mspring.org)
4 */
5 package org.mspring.core;
6
7 import java.io.File;
8 import java.lang.ref.WeakReference;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Date;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import java.util.Timer;
16 import java.util.TimerTask;
17
18 /**
19 * @author gaoyb
20 *
21 */
22 public class Test {
23 public static void main(String args[]) {
24 FileMonitor monitor = new FileMonitor(1000);
25 monitor.addFile(new File("E:\\test.log"));
26 monitor.addListener(new FileListenerImpl());
27 while (!false);
28 }
29 }
30
31 interface FileListener{
32 void fileChanged(File file);
33 }
34
35 class FileListenerImpl implements FileListener{
36
37 /* (non-Javadoc)
38 * @see org.mspring.org.FileListener#fileChanged(java.io.File)
39 */
40 public void fileChanged(File file) {
41 // TODO Auto-generated method stub
42 System.out.println("File [" + file.getName() + "] changed at " + new Date());
43 }
44
45 }
46
47 class FileMonitor{
48 private Timer timer;
49 private Map fileMap;
50 private Collection listeners;
51
52 public FileMonitor(long pollingInterval){
53 fileMap = new HashMap();
54 listeners = new ArrayList();
55 timer = new Timer(true);
56 timer.schedule(new FileMonitorNotifier(),0,pollingInterval);
57 }
58
59 /**
60 * 停止监听
61 */
62 public void stop(){
63 timer.cancel();
64 }
65
66 /**
67 * 添加监听文件
68 * @param file
69 */
70 public void addFile(File file){
71 if(!fileMap.containsKey(file)){
72 long modifiedTime = file.exists() ? file.lastModified() : -1;
73 fileMap.put(file, new Long(modifiedTime));
74 }
75 }
76
77 /**
78 * 移除监听文件
79 * @param file
80 */
81 public void removeFile(File file){
82 fileMap.remove(file);
83 }
84
85 /**
86 * 添加文件监听器
87 * @param fileListener
88 */
89 public void addListener(FileListener fileListener){
90 for(Iterator i = listeners.iterator(); i.hasNext();){
91 WeakReference reference = (WeakReference) i.next();
92 FileListener listener = (FileListener) reference.get();
93 if(listener == fileListener)
94 return;
95 }
96 listeners.add(new WeakReference(fileListener));
97 }
98
99 /**
100 * 移除文件监听器
101 * @param fileListener
102 */
103 public void removeListener(FileListener fileListener) {
104 for (Iterator i = listeners.iterator(); i.hasNext();) {
105 WeakReference reference = (WeakReference) i.next();
106 FileListener listener = (FileListener) reference.get();
107 if (listener == fileListener) {
108 i.remove();
109 break;
110 }
111 }
112 }
113
114 /**
115 * TimerTask
116 * @author gaoyb
117 *
118 */
119 private class FileMonitorNotifier extends TimerTask{
120
121 /* (non-Javadoc)
122 * @see java.util.TimerTask#run()
123 */
124 public void run() {
125 // TODO Auto-generated method stub
126 Collection files = new ArrayList(fileMap.keySet());
127
128 for (Iterator i = files.iterator(); i.hasNext();) {
129 File file = (File) i.next();
130 long lastModifiedTime = ((Long) fileMap.get(file)).longValue();
131 long newModifiedTime = file.exists() ? file.lastModified() : -1;
132
133 if (newModifiedTime != lastModifiedTime) {
134
135 fileMap.put(file, new Long(newModifiedTime));
136
137 for (Iterator j = listeners.iterator(); j.hasNext();) {
138 WeakReference reference = (WeakReference) j.next();
139 FileListener listener = (FileListener) reference.get();
140
141 // Remove from list if the back-end object has been GC'd
142 if (listener == null)
143 j.remove();
144 else
145 listener.fileChanged(file);
146 }
147 }
148 }
149 }
150 }
151 }