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