|
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.common.RegExpCreator; |
|
10 |
| import org.abraracourcix.alipes.listeners.Listener; |
|
11 |
| import org.abraracourcix.alipes.listeners.filter.EventFilterListener; |
|
12 |
| import org.abraracourcix.alipes.listeners.logging.ConsoleListener; |
|
13 |
| import org.abraracourcix.alipes.monitors.file.FileEvent; |
|
14 |
| import org.abraracourcix.alipes.monitors.file.FileStateMonitor; |
|
15 |
| import org.abraracourcix.alipes.monitors.file.FileStateMonitorFactory; |
|
16 |
| import org.apache.commons.logging.Log; |
|
17 |
| import org.apache.commons.logging.LogFactory; |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| public final class FileWildCardChangeConsoleAlerter { |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
0
| private FileWildCardChangeConsoleAlerter() {
|
|
31 |
| } |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| private static Log log = |
|
36 |
| LogFactory.getLog(FileWildCardChangeConsoleAlerter.class); |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| public static void main(final String[] args)
|
|
48 |
| throws InterruptedException, IOException { |
|
49 |
0
| if (args.length != 3) {
|
|
50 |
0
| giveHelp();
|
|
51 |
0
| return;
|
|
52 |
| } |
|
53 |
0
| final String dirName = args[0];
|
|
54 |
0
| final String wildCards = args[1];
|
|
55 |
0
| final String interval = args[2];
|
|
56 |
0
| log.info("dirName=" + dirName);
|
|
57 |
0
| log.info("wildCards=" + wildCards);
|
|
58 |
0
| log.info("interval=" + interval);
|
|
59 |
0
| final File root = new File(dirName);
|
|
60 |
0
| if (!(root.exists())) {
|
|
61 |
0
| throw new IOException("Directory " + dirName + " doesn't exist");
|
|
62 |
| } |
|
63 |
0
| if (!root.isDirectory()) {
|
|
64 |
0
| throw new IOException(dirName + " isn't a directory");
|
|
65 |
| } |
|
66 |
0
| int intervalSecs;
|
|
67 |
0
| try {
|
|
68 |
0
| intervalSecs = Integer.parseInt(interval);
|
|
69 |
| } catch (NumberFormatException nfe) { |
|
70 |
0
| throw new NumberFormatException("3rd argument must be an integer number of seconds ");
|
|
71 |
| } |
|
72 |
0
| System.out.println("Ctrl-C when you're bored");
|
|
73 |
0
| final String regExp = RegExpCreator.getRegExp(wildCards);
|
|
74 |
0
| final ConsoleListener listener = new ConsoleListener();
|
|
75 |
0
| final FileEvent[] interestingEvents =
|
|
76 |
| { |
|
77 |
| FileEvent.CREATED, |
|
78 |
| FileEvent.DELETED, |
|
79 |
| FileEvent.GONE_STALE, |
|
80 |
| FileEvent.UPDATED }; |
|
81 |
0
| final Listener filter =
|
|
82 |
| new EventFilterListener( |
|
83 |
| listener, |
|
84 |
| interestingEvents, |
|
85 |
| EventFilterListener.FilterType.INCLUDED); |
|
86 |
0
| final int pollFrequency = 10;
|
|
87 |
0
| final FileStateMonitor mon =
|
|
88 |
| FileStateMonitorFactory.getFileStateMonitor( |
|
89 |
| root, |
|
90 |
| regExp, |
|
91 |
| filter, |
|
92 |
| pollFrequency); |
|
93 |
0
| mon.setStaleInterval(intervalSecs);
|
|
94 |
0
| Thread.sleep(24 * 60 * 60 * 1000);
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
0
| private static void giveHelp() {
|
|
100 |
0
| System.out.println(
|
|
101 |
| "Usage:\n java -jar your_uber_jar_here.jar dir files staleInt"); |
|
102 |
0
| System.out.println("where:\ndir is the root directory e.g. /tmp");
|
|
103 |
0
| System.out.println("files is a wildcard expression e.g. *.txt");
|
|
104 |
0
| System.out.println(
|
|
105 |
| "and staleInt is the time in seconds before a file is considered stale"); |
|
106 |
| } |
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |