(no commit message)
[utils] / support / general / src / main / java / org / wamblee / io / DirectoryMonitor.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.io;
18
19 import java.io.File;
20 import java.io.FileFilter;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * Monitors a directory for changes.
32  * 
33  * @author Erik Brakkee
34  */
35 public class DirectoryMonitor {
36
37         private static final Log LOG = LogFactory.getLog(DirectoryMonitor.class);
38
39         public static interface Listener {
40                 
41                 void fileChanged(File aFile);
42
43                 void fileCreated(File aFile);
44
45                 void fileDeleted(File aFile);
46         };
47
48         private File _directory;
49         private FileFilter _filter;
50         private Listener _listener;
51         private Map<File, Date> _contents;
52
53         public DirectoryMonitor(File aDirectory, FileFilter aFilefilter,
54                         Listener aListener) {
55                 _directory = aDirectory;
56                 if (!_directory.isDirectory()) {
57                         throw new IllegalArgumentException("Directory '" + _directory
58                                         + "' does not exist");
59                 }
60                 _filter = aFilefilter;
61                 _listener = aListener;
62                 _contents = new HashMap<File, Date>();
63         }
64
65         /**
66          * Polls the directory for changes and notifies the listener of any changes. 
67          * In case of any exceptions thrown by the listener while handling the changes, 
68          * the next call to this method will invoked the listeners again for the same changes. 
69          */
70         public void poll() {
71                 LOG.debug("Polling " + _directory);
72                 Map<File, Date> newContents = new HashMap<File, Date>();
73                 File[] files = _directory.listFiles(_filter);
74
75                 // Check deleted files. 
76                 Set<File> deletedFiles = new HashSet<File>(_contents.keySet());
77                 for (File file : files) {
78                         if (file.isFile()) {
79                                 if (_contents.containsKey(file)) {
80                                         deletedFiles.remove(file);
81                                 }
82                         }
83                 }
84                 for (File file : deletedFiles) {
85                         _listener.fileDeleted(file);
86                 }
87                 
88                 for (File file : files) {
89                         if (file.isFile()) {
90                                 if (_contents.containsKey(file)) {
91                                         Date oldDate = _contents.get(file);
92                                         if (file.lastModified() != oldDate.getTime()) {
93                                                 _listener.fileChanged(file);
94                                         } else {
95                                                 // No change.
96                                         }
97                                         newContents.put(file, new Date(file.lastModified()));
98                                 } else {
99                                         _listener.fileCreated(file);
100                                         newContents.put(file, new Date(file.lastModified()));
101                                 }
102                         }
103                 }
104                 
105                 _contents = newContents;
106         }
107
108 }