13181975068e0233f122411942d49e1ab319fab8
[utils] / support / general / src / main / java / org / wamblee / io / DirectoryMonitor.java
1 /*
2  * Copyright 2005-2010 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 package org.wamblee.io;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.io.File;
22 import java.io.FileFilter;
23
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29
30 /**
31  * Monitors a directory for changes.
32  * The currernt implementation only checks files not directories and does not check for 
33  * modifications in subdirectories. 
34  * 
35  * @author Erik Brakkee
36  */
37 public class DirectoryMonitor {
38     private static final Log LOG = LogFactory.getLog(DirectoryMonitor.class);
39
40     private File directory;
41
42     private FileFilter filter;
43
44     private Listener listener;
45
46     private Map<File, Date> contents;
47
48     /**
49      * Creates a new DirectoryMonitor object.
50      * 
51      */
52     public DirectoryMonitor(File aDirectory, FileFilter aFilefilter,
53         Listener aListener) {
54         directory = aDirectory;
55
56         if (!directory.isDirectory()) {
57             throw new IllegalArgumentException("Directory '" + directory +
58                 "' does not exist");
59         }
60
61         filter = aFilefilter;
62         listener = aListener;
63         contents = new HashMap<File, Date>();
64     }
65
66     /**
67      * Polls the directory for changes and notifies the listener of any changes.
68      * In case of any exceptions thrown by the listener while handling the
69      * changes, the next call to this method will invoked the listeners again
70      * for the same changes.
71      */
72     public void poll() {
73         LOG.debug("Polling " + directory);
74
75         Map<File, Date> newContents = new HashMap<File, Date>();
76         File[] files = directory.listFiles(filter);
77
78         // Check deleted files.
79         Set<File> deletedFiles = new HashSet<File>(contents.keySet());
80
81         for (File file : files) {
82             if (file.isFile()) {
83                 if (contents.containsKey(file)) {
84                     deletedFiles.remove(file);
85                 }
86             }
87         }
88
89         for (File file : deletedFiles) {
90             listener.fileDeleted(file);
91         }
92
93         for (File file : files) {
94             if (file.isFile()) {
95                 if (contents.containsKey(file)) {
96                     Date oldDate = contents.get(file);
97
98                     if (file.lastModified() != oldDate.getTime()) {
99                         listener.fileChanged(file);
100                     } else {
101                         // No change.
102                     }
103
104                     newContents.put(file, new Date(file.lastModified()));
105                 } else {
106                     listener.fileCreated(file);
107                     newContents.put(file, new Date(file.lastModified()));
108                 }
109             }
110         }
111
112         contents = newContents;
113     }
114
115     /**
116      * Listener interface to be provided by users of the directory monitor to get notified of
117      * changes. 
118      * 
119      * @author Erik Brakkee
120      */
121     public static interface Listener {
122         /**
123          * @param aFile File that has changed. 
124          */
125         void fileChanged(File aFile);
126
127         /**
128          * @param aFile File that was created. 
129          */
130         void fileCreated(File aFile);
131
132         /**
133          * @param aFile File that was deleted.
134          */
135         void fileDeleted(File aFile);
136     }
137 }