|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package org.abraracourcix.alipes.alerters; |
|
6 |
| import java.io.File; |
|
7 |
| import java.io.FileFilter; |
|
8 |
| import java.io.FileInputStream; |
|
9 |
| import java.io.IOException; |
|
10 |
| import java.util.Properties; |
|
11 |
| |
|
12 |
| import org.abraracourcix.alipes.common.RegExpCreator; |
|
13 |
| import org.abraracourcix.alipes.common.RegExpFileFilter; |
|
14 |
| import org.abraracourcix.alipes.listeners.Listener; |
|
15 |
| import org.abraracourcix.alipes.listeners.email.SelectiveEmailListener; |
|
16 |
| import org.abraracourcix.alipes.listeners.filter.EventFilterListener; |
|
17 |
| import org.abraracourcix.alipes.monitors.file.DirectoryFileStateMonitor; |
|
18 |
| import org.abraracourcix.alipes.monitors.file.FileEvent; |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| public final class DirChangeEmailAlerter { |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| private static final org.apache.commons.logging.Log log = |
|
30 |
| org.apache.commons.logging.LogFactory.getLog( |
|
31 |
| DirChangeEmailAlerter.class); |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
0
| private DirChangeEmailAlerter() {
|
|
36 |
| } |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
0
| public static void main(final String[] args)
|
|
45 |
| throws IOException { |
|
46 |
0
| if (args.length !=2 && args.length!=3) {
|
|
47 |
0
| giveHelp();
|
|
48 |
0
| return;
|
|
49 |
| } |
|
50 |
0
| final String interval = args[0];
|
|
51 |
0
| final File dir = new File(args[1]);
|
|
52 |
0
| final String wildcard = args.length==2? "*" : args[2];
|
|
53 |
| |
|
54 |
| |
|
55 |
0
| log.debug("Monitoring files matching "+wildcard+" in "+dir+" with a stale interval of "+interval);
|
|
56 |
0
| if (!(dir.exists())) {
|
|
57 |
0
| throw new IOException("Directory " + dir + " does not exist");
|
|
58 |
| } |
|
59 |
0
| if (!(dir.isDirectory())) {
|
|
60 |
0
| throw new IOException(dir + " is not a directory");
|
|
61 |
| } |
|
62 |
0
| final FileFilter fileFilter =
|
|
63 |
| new RegExpFileFilter(RegExpCreator.getRegExp(wildcard), null); |
|
64 |
0
| int intervalSecs;
|
|
65 |
0
| try {
|
|
66 |
0
| intervalSecs = Integer.parseInt(interval);
|
|
67 |
| } catch (NumberFormatException nfe) { |
|
68 |
0
| throw new NumberFormatException("1st argument must be an integer number of seconds ");
|
|
69 |
| } |
|
70 |
0
| System.out.println("Ctrl-C when you're bored");
|
|
71 |
| |
|
72 |
| |
|
73 |
0
| Properties emailConfig= new Properties();
|
|
74 |
0
| emailConfig.load(new FileInputStream(new File("email.properties")));
|
|
75 |
0
| String smtpServer = emailConfig.getProperty("smtpServer");
|
|
76 |
0
| String user = emailConfig.getProperty("user");
|
|
77 |
0
| String password = emailConfig.getProperty("password");
|
|
78 |
0
| String returnAddress = emailConfig.getProperty("returnAddress");
|
|
79 |
0
| if (smtpServer==null || user ==null || password==null || returnAddress==null) {
|
|
80 |
0
| throw new IOException("You must set smtpServer, user, password and and returnAddress in email.properties ");
|
|
81 |
| } |
|
82 |
0
| final Listener lastListener = new SelectiveEmailListener(smtpServer,user,password,returnAddress);
|
|
83 |
| |
|
84 |
| |
|
85 |
0
| final FileEvent[] interestingEvents =
|
|
86 |
| { FileEvent.CREATED, FileEvent.DELETED, FileEvent.GONE_STALE }; |
|
87 |
| |
|
88 |
0
| final Listener filter =
|
|
89 |
| new EventFilterListener( |
|
90 |
| lastListener, |
|
91 |
| interestingEvents, |
|
92 |
| EventFilterListener.FilterType.INCLUDED); |
|
93 |
0
| final int pollFrequency = 300;
|
|
94 |
| |
|
95 |
0
| final DirectoryFileStateMonitor mon =
|
|
96 |
| new DirectoryFileStateMonitor( |
|
97 |
| filter, |
|
98 |
| pollFrequency, |
|
99 |
| dir, |
|
100 |
| fileFilter); |
|
101 |
0
| mon.setStaleInterval(intervalSecs);
|
|
102 |
0
| mon.start();
|
|
103 |
0
| while(true) {
|
|
104 |
0
| try {
|
|
105 |
0
| Thread.sleep(365 * 24 * 60 * 60 * 1000);
|
|
106 |
| |
|
107 |
| } catch (InterruptedException e) { |
|
108 |
| |
|
109 |
| } |
|
110 |
| } |
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
0
| private static void giveHelp() {
|
|
116 |
0
| System.out.println(
|
|
117 |
| "Usage:\n java -jar your_uber_jar_here.jar staleInt dir (wildcard)"); |
|
118 |
0
| System.out.println("where:");
|
|
119 |
0
| System.out.println(
|
|
120 |
| "wildcard is the usual file wildcard text\n" |
|
121 |
| + "staleInt is the time in seconds before a file is considered stale\n" |
|
122 |
| + "and dir is the directory you wish to monitor"); |
|
123 |
0
| System.out.println("You may need to enclose the wildcard in single quotes to" +
|
|
124 |
| "protext it from the operating system"); |
|
125 |
0
| System.out.println();
|
|
126 |
0
| System.out.println("Other things to know:\n");
|
|
127 |
0
| System.out.println("A file named email.properties is required, giving the config details of your email server.\n");
|
|
128 |
0
| System.out.println("The file email.txt uses standard file wildcards to direct emails to different recipients according to the filename.\n");
|
|
129 |
0
| System.out.println("You may change the text of the emails by editing the Velocity templates subject.vm and message.vm\n");
|
|
130 |
| } |
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |