1
2
3
4
5 package org.abraracourcix.alipes.alerters;
6 import java.io.File;
7 import java.io.IOException;
8 import org.abraracourcix.alipes.listeners.Listener;
9 import org.abraracourcix.alipes.listeners.filter.EventFilterListener;
10 import org.abraracourcix.alipes.listeners.logging.ConsoleListener;
11 import org.abraracourcix.alipes.monitors.file.FileEvent;
12 import org.abraracourcix.alipes.monitors.file.FileStateMonitor;
13 /***
14 * Takes a list of filenames, a stale interval, and watches those files to see
15 * if they're deleted/stale/created or change
16 *
17 * @author jdt
18 */
19 public final class FileChangeConsoleAlerter {
20 /***
21 * Hide public Constructor
22 *
23 */
24 private FileChangeConsoleAlerter() {
25 }
26 /***
27 * Set the wheels in motion
28 *
29 * @param args
30 * staletime(seconds) files
31 * @throws InterruptedException
32 * if you hit ctrl-C
33 * @throws IOException
34 * if there's a prob finding the files
35 */
36 public static void main(final String[] args)
37 throws InterruptedException, IOException {
38 if (args.length < 2) {
39 giveHelp();
40 return;
41 }
42 final String interval = args[0];
43 final File[] files = new File[args.length - 1];
44 for (int i = 0; i < args.length - 1; ++i) {
45 files[i] = new File(args[i + 1]);
46 }
47 final int maxFiles2Show = 5;
48 System.out.println("Monitoring " + files.length + " files:");
49 for (int i = 0; i < Math.min(files.length, maxFiles2Show); ++i) {
50 System.out.println(files[i]);
51 }
52 System.out.println("...");
53 int intervalSecs;
54 try {
55 intervalSecs = Integer.parseInt(interval);
56 } catch (NumberFormatException nfe) {
57 throw new NumberFormatException("3rd argument must be an integer number of seconds ");
58 }
59 System.out.println("Ctrl-C when you're bored");
60 final ConsoleListener listener = new ConsoleListener();
61 final FileEvent[] interestingEvents =
62 {
63 FileEvent.CREATED,
64 FileEvent.DELETED,
65 FileEvent.GONE_STALE,
66 FileEvent.UPDATED };
67 final Listener filter =
68 new EventFilterListener(
69 listener,
70 interestingEvents,
71 EventFilterListener.FilterType.INCLUDED);
72 final int pollFrequency = 10;
73 final FileStateMonitor mon =
74 new FileStateMonitor(filter, pollFrequency);
75 mon.addFiles(files);
76 mon.setStaleInterval(intervalSecs);
77 mon.start();
78 Thread.sleep(24 * 60 * 60 * 1000);
79 }
80 /***
81 *
82 */
83 private static void giveHelp() {
84 System.out.println(
85 "Usage:\n java -jar your_uber_jar_here.jar staleInt file1 file2 ...");
86 System.out.println("where:");
87 System.out.println(
88 "staleInt is the time in seconds before a file is considered stale");
89 System.out.println(
90 "file1 etc are the files you want to watch (can use wildcard expressions in Windows and maybe unix");
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112