Removed DOCUMENT ME comments that were generated and applied source code
[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 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  * 
33  * @author Erik Brakkee
34  */
35 public class DirectoryMonitor {
36     private static final Log LOG = LogFactory.getLog(DirectoryMonitor.class);
37
38     private File directory;
39
40     private FileFilter filter;
41
42     private Listener listener;
43
44     private Map<File, Date> contents;
45
46     /**
47      * Creates a new DirectoryMonitor object.
48      * 
49      */
50     public DirectoryMonitor(File aDirectory, FileFilter aFilefilter,
51         Listener aListener) {
52         directory = aDirectory;
53
54         if (!directory.isDirectory()) {
55             throw new IllegalArgumentException("Directory '" + directory +
56                 "' does not exist");
57         }
58
59         filter = aFilefilter;
60         listener = aListener;
61         contents = new HashMap<File, Date>();
62     }
63
64     /**
65      * Polls the directory for changes and notifies the listener of any changes.
66      * In case of any exceptions thrown by the listener while handling the
67      * changes, the next call to this method will invoked the listeners again
68      * for the same changes.
69      */
70     public void poll() {
71         LOG.debug("Polling " + directory);
72
73         Map<File, Date> newContents = new HashMap<File, Date>();
74         File[] files = directory.listFiles(filter);
75
76         // Check deleted files.
77         Set<File> deletedFiles = new HashSet<File>(contents.keySet());
78
79         for (File file : files) {
80             if (file.isFile()) {
81                 if (contents.containsKey(file)) {
82                     deletedFiles.remove(file);
83                 }
84             }
85         }
86
87         for (File file : deletedFiles) {
88             listener.fileDeleted(file);
89         }
90
91         for (File file : files) {
92             if (file.isFile()) {
93                 if (contents.containsKey(file)) {
94                     Date oldDate = contents.get(file);
95
96                     if (file.lastModified() != oldDate.getTime()) {
97                         listener.fileChanged(file);
98                     } else {
99                         // No change.
100                     }
101
102                     newContents.put(file, new Date(file.lastModified()));
103                 } else {
104                     listener.fileCreated(file);
105                     newContents.put(file, new Date(file.lastModified()));
106                 }
107             }
108         }
109
110         contents = newContents;
111     }
112
113     public static interface Listener {
114         void fileChanged(File aFile);
115
116         void fileCreated(File aFile);
117
118         void fileDeleted(File aFile);
119     }
120 }