|
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 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| public final class FileChangeConsoleAlerter { |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
0
| private FileChangeConsoleAlerter() {
|
|
25 |
| } |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
0
| public static void main(final String[] args)
|
|
37 |
| throws InterruptedException, IOException { |
|
38 |
0
| if (args.length < 2) {
|
|
39 |
0
| giveHelp();
|
|
40 |
0
| return;
|
|
41 |
| } |
|
42 |
0
| final String interval = args[0];
|
|
43 |
0
| final File[] files = new File[args.length - 1];
|
|
44 |
0
| for (int i = 0; i < args.length - 1; ++i) {
|
|
45 |
0
| files[i] = new File(args[i + 1]);
|
|
46 |
| } |
|
47 |
0
| final int maxFiles2Show = 5;
|
|
48 |
0
| System.out.println("Monitoring " + files.length + " files:");
|
|
49 |
0
| for (int i = 0; i < Math.min(files.length, maxFiles2Show); ++i) {
|
|
50 |
0
| System.out.println(files[i]);
|
|
51 |
| } |
|
52 |
0
| System.out.println("...");
|
|
53 |
0
| int intervalSecs;
|
|
54 |
0
| try {
|
|
55 |
0
| intervalSecs = Integer.parseInt(interval);
|
|
56 |
| } catch (NumberFormatException nfe) { |
|
57 |
0
| throw new NumberFormatException("3rd argument must be an integer number of seconds ");
|
|
58 |
| } |
|
59 |
0
| System.out.println("Ctrl-C when you're bored");
|
|
60 |
0
| final ConsoleListener listener = new ConsoleListener();
|
|
61 |
0
| final FileEvent[] interestingEvents =
|
|
62 |
| { |
|
63 |
| FileEvent.CREATED, |
|
64 |
| FileEvent.DELETED, |
|
65 |
| FileEvent.GONE_STALE, |
|
66 |
| FileEvent.UPDATED }; |
|
67 |
0
| final Listener filter =
|
|
68 |
| new EventFilterListener( |
|
69 |
| listener, |
|
70 |
| interestingEvents, |
|
71 |
| EventFilterListener.FilterType.INCLUDED); |
|
72 |
0
| final int pollFrequency = 10;
|
|
73 |
0
| final FileStateMonitor mon =
|
|
74 |
| new FileStateMonitor(filter, pollFrequency); |
|
75 |
0
| mon.addFiles(files);
|
|
76 |
0
| mon.setStaleInterval(intervalSecs);
|
|
77 |
0
| mon.start();
|
|
78 |
0
| Thread.sleep(24 * 60 * 60 * 1000);
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
0
| private static void giveHelp() {
|
|
84 |
0
| System.out.println(
|
|
85 |
| "Usage:\n java -jar your_uber_jar_here.jar staleInt file1 file2 ..."); |
|
86 |
0
| System.out.println("where:");
|
|
87 |
0
| System.out.println(
|
|
88 |
| "staleInt is the time in seconds before a file is considered stale"); |
|
89 |
0
| 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 |
| |