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