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