1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package no.feide.moria.servlet;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24
25 /***
26 *
27 * Based (in part) on ConfigurationManager#FileListenerTask
28 *
29 * @author Eva Indal
30 * @version $Revision: 1.1 $
31 */
32 public class FileMonitor {
33
34 /***
35 * The monitored file
36 */
37 private final File monitoredFile;
38
39 /***
40 * Modification time of the watched file.
41 */
42 private long lastModified;
43
44 /***
45 * Constructor.
46 *
47 * @param fileURI The file to monitor
48 * @throws FileNotFoundException
49 */
50 public FileMonitor(final String fileURI) throws FileNotFoundException {
51 monitoredFile = fileForURI(fileURI);
52 this.lastModified = monitoredFile.lastModified();
53 }
54
55 /***
56 * Returns <code>true</code> if the monitored file has changed.
57 *
58 * @return <code>true</code> if file has changed
59 */
60 public boolean hasChanged() {
61 return monitoredFile.lastModified() != lastModified;
62 }
63
64 /***
65 * Resolves a fileURI to a <code>File</code> object.
66 * @param fileURI
67 * Reference to the file (full path or relative within the
68 * classpath).
69 * @return A <code>File</code> object referenced by the fileURI.
70 * @throws FileNotFoundException
71 * If the fileURI cannot be resolved to a readable file.
72 */
73 private static File fileForURI(final String fileURI) throws FileNotFoundException {
74
75 if (fileURI == null || fileURI.equals("")) { throw new FileNotFoundException("File reference cannot be null."); }
76
77 final File file = new File(fileURI);
78 return file;
79 }
80
81
82 }