KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > filemonitor > ObserverThread


1 /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1
3   (the "License"); you may not use this file except in compliance with the
4   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5   
6   Software distributed under the License is distributed on an "AS IS" basis,
7   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8   for the specific language governing rights and
9   limitations under the License.
10
11   The Original Code is "The Columba Project"
12   
13   The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
14   Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15   
16   All Rights Reserved.
17 */

18 package org.columba.core.filemonitor;
19
20 /*
21   TODO
22     - make it possible to monitor a directory
23     - make it possible to stop monitoring a file
24 */

25
26 import java.io.File JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * @author Celso Pinto <cpinto@yimports.com>
34  */

35 public class ObserverThread
36     extends Thread JavaDoc
37 {
38
39   static final int DEFAULT_POLLING_INTERVAL = 60;
40
41   private final Map JavaDoc<File JavaDoc,Set JavaDoc> watchedFiles;
42
43   private long lastExecution;
44   private int pollingInterval;
45   private boolean terminate = false;
46
47   public ObserverThread(int interval)
48   {
49
50     watchedFiles = new HashMap JavaDoc<File JavaDoc,Set JavaDoc>();
51     lastExecution = System.currentTimeMillis();
52     pollingInterval = interval;
53
54   }
55
56   public void terminate()
57   {
58     terminate = true;
59   }
60
61   public void monitorFile(File JavaDoc file, FileObserver observer)
62   {
63
64     synchronized(watchedFiles)
65     {
66
67       Set JavaDoc<FileObserver> observerSet;
68
69       observerSet = watchedFiles.get(file);
70
71       if (observerSet == null)
72       {
73
74         observerSet = new HashSet JavaDoc<FileObserver>();
75         watchedFiles.put(file,observerSet);
76
77       }
78       else
79       {
80
81         if (observerSet.contains(observer))
82           return;
83
84       }
85
86       observerSet.add(observer);
87
88     }
89
90   }
91
92   public void run()
93   {
94
95     while(!terminate)
96     {
97
98       performCheck();
99
100       try
101       {
102         sleep(pollingInterval);
103       }
104       catch(InterruptedException JavaDoc ex)
105       {}
106
107     }
108
109   }
110
111   private void performCheck()
112   {
113
114     synchronized(watchedFiles)
115     {
116
117       File JavaDoc file;
118       Set JavaDoc<FileObserver> observers;
119
120       for(Map.Entry JavaDoc<File JavaDoc,Set JavaDoc> entry : watchedFiles.entrySet())
121       {
122
123         file = entry.getKey();
124         observers = entry.getValue();
125
126         if (!file.exists())
127         {
128
129           for(FileObserver observer : observers)
130             observer.fileChanged(file,FileObserver.FILE_REMOVED);
131
132           watchedFiles.remove(file);
133           continue;
134
135         }
136
137         if (file.lastModified() <= lastExecution)
138           continue;
139
140         for(FileObserver observer : observers)
141           observer.fileChanged(file,FileObserver.FILE_CHANGED);
142
143       }
144
145     }
146
147     lastExecution = System.currentTimeMillis();
148
149   }
150 }
151
Popular Tags