1
2
3
4
5
6
7 package org.abraracourcix.alipes.monitors.file;
8 import java.io.File;
9 import java.io.FileFilter;
10 import java.io.IOException;
11 import java.util.Set;
12
13 import org.abraracourcix.alipes.listeners.Listener;
14 /***
15 * Like a FileStateMonitor, but monitors a specifig directory, rather than a
16 * specific collection of files. Why? We want to allow for new files being
17 * created. @TODO we're going to need to allow filters though.
18 *
19 * @author jdt
20 */
21 public class DirectoryFileStateMonitor extends AbstractFileStateMonitor {
22 /***
23 * Directory we're monitoring.
24 */
25 private File dir;
26 /***
27 * Constructor
28 *
29 * @param listener listener
30 * @param pollingFrequency frequency of polling (see PollingMonitor)
31 * @param dir directory to monitor
32 */
33 public DirectoryFileStateMonitor(
34 final Listener listener,
35 final long pollingFrequency,
36 final File dir) {
37 super(listener, pollingFrequency);
38 this.dir = dir;
39 updateFileList(true);
40 }
41 /***
42 * Constructor
43 *
44 * @param listener listener
45 * @param pollingFrequency frequency of polling (see PollingMonitor)
46 * @param dir directory to monitor
47 * @param fileFilter filter to apply to the files in the directory
48 * @throws IOException if the directory does not exist
49 */
50 public DirectoryFileStateMonitor(
51 final Listener listener,
52 final int pollingFrequency,
53 final File dir,
54 final FileFilter fileFilter) {
55 this(listener, pollingFrequency, dir);
56 this.filter = fileFilter;
57 }
58 /***
59 * Commons-logging logger
60 */
61 private static org.apache.commons.logging.Log log =
62 org.apache.commons.logging.LogFactory.getLog(FileStateMonitor.class);
63 private FileFilter filter = null;
64 /***
65 * Check the directory to see if any new files have appeared
66 * This checks the root folder, and includes any folders within that folder, but it
67 * is not recursive, so it does not drill down into subfolders. The root folder itself
68 * is also included in the list. Note that overtime this list could bloat and bloat
69 * as there is no mechanism for removing files once they're in.
70 *
71 * @param treatAsUnknown sets the initial state you want for the file - if
72 * the program has started up, you probably want it to be set to
73 * UNKNOWN, so set this argument to true, otherwise will be as
74 * newly created
75 */
76 private void updateFileList(final boolean treatAsUnknown) {
77 assert dir != null;
78
79 final Set existingFiles = getMonitoredFiles();
80 assert existingFiles != null;
81 final File[] files = dir.listFiles(filter);
82 if (files!=null ) {
83
84
85
86 for (int i = 0; i < files.length; ++i) {
87 final File candidate = files[i];
88 if (!(existingFiles.contains(candidate))) {
89 addFile(
90 candidate,
91 treatAsUnknown
92 ? FileState.UNKNOWN
93 : FileState.FILE_NO_EXIST);
94 }
95 }
96 }
97
98 final File candidate = dir;
99 if (!(existingFiles.contains(candidate))) {
100 addFile(
101 candidate,
102 treatAsUnknown
103 ? FileState.UNKNOWN
104 : FileState.FILE_NO_EXIST);
105 }
106 }
107 /***
108 * Check the file list and respond acccordingly @TODO deal with exception
109 * properly
110 *
111 * @see org.abraracourcix.alipes.monitors.PollingMonitor#pole()
112 */
113 public final void pole() {
114 updateFileList(false);
115
116 checkTheFiles();
117 }
118 }
119
120
121
122
123
124
125
126
127