View Javadoc

1   /*
2    * $Id: DirChangeEmailAlerter.java,v 1.7 2004/07/19 14:03:25 johndavidtaylor Exp $
3    * johndavidtaylor Exp $ Created on Mar 10, 2004 by John Taylor jdt@roe.ac.uk .
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   * Takes a directory, a filter, a stale interval, and watches those files to
21   * see if they're deleted/stale/created or change. Sends an email if so.
22   * 
23   * @author jdt
24   */
25  public final class DirChangeEmailAlerter {
26      /***
27       * Commons logger
28       */
29      private static final org.apache.commons.logging.Log log =
30          org.apache.commons.logging.LogFactory.getLog(
31              DirChangeEmailAlerter.class);
32      /***
33       * Hide public Constructor
34       */
35      private DirChangeEmailAlerter() {
36      }
37      /***
38       * Set the wheels in motion
39       * 
40       * @param args staletime(seconds) directory wildcards
41       * @throws InterruptedException if you hit ctrl-C
42       * @throws IOException if there's a prob finding the files
43       */
44      public static void main(final String[] args)
45          throws  IOException {
46          if (args.length !=2 && args.length!=3) {
47              giveHelp();
48              return;
49          }
50          final String interval = args[0];
51          final File dir = new File(args[1]);
52          final String wildcard = args.length==2? "*" : args[2];
53          
54          
55          log.debug("Monitoring files matching "+wildcard+" in "+dir+" with a stale interval of "+interval);
56          if (!(dir.exists())) {
57              throw new IOException("Directory " + dir + " does not exist");
58          }
59          if (!(dir.isDirectory())) {
60              throw new IOException(dir + " is not a directory");
61          }
62          final FileFilter fileFilter =
63              new RegExpFileFilter(RegExpCreator.getRegExp(wildcard), null);
64          int intervalSecs;
65          try {
66              intervalSecs = Integer.parseInt(interval);
67          } catch (NumberFormatException nfe) {
68              throw new NumberFormatException("1st argument must be an integer number of seconds ");
69          }
70          System.out.println("Ctrl-C when you're bored");
71          
72          // The final destination for any events
73          Properties emailConfig= new Properties();
74          emailConfig.load(new FileInputStream(new File("email.properties")));
75          String smtpServer = emailConfig.getProperty("smtpServer");
76          String user = emailConfig.getProperty("user");
77          String password = emailConfig.getProperty("password");
78          String returnAddress = emailConfig.getProperty("returnAddress");
79          if (smtpServer==null || user ==null || password==null || returnAddress==null) {
80              throw new IOException("You must set smtpServer, user, password and and returnAddress in email.properties ");
81          }
82          final Listener lastListener = new SelectiveEmailListener(smtpServer,user,password,returnAddress);
83           
84          //final Listener lastListener = new ConsoleListener();
85          final FileEvent[] interestingEvents =
86              { FileEvent.CREATED, FileEvent.DELETED, FileEvent.GONE_STALE };
87          //after they've been filtered
88          final Listener filter =
89              new EventFilterListener(
90                  lastListener,
91                  interestingEvents,
92                  EventFilterListener.FilterType.INCLUDED);
93          final int pollFrequency = 300; //in seconds
94          //and their source
95          final DirectoryFileStateMonitor mon =
96              new DirectoryFileStateMonitor(
97                  filter,
98                  pollFrequency,
99                  dir,
100                 fileFilter);
101         mon.setStaleInterval(intervalSecs);
102         mon.start();
103         while(true) {
104 	        try {
105 				Thread.sleep(365 * 24 * 60 * 60 * 1000);
106 				//wait forever, well a year anyway
107 			} catch (InterruptedException e) {
108 				//	do nothing
109 			}
110         }
111     }
112     /***
113      *  
114      */
115     private static void giveHelp() {
116         System.out.println(
117             "Usage:\n java -jar your_uber_jar_here.jar  staleInt dir (wildcard)");
118         System.out.println("where:");
119         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         System.out.println("You may need to enclose the wildcard in single quotes to" +
124                 "protext it from the operating system");
125         System.out.println();
126         System.out.println("Other things to know:\n");
127         System.out.println("A file named email.properties is required, giving the config details of your email server.\n");
128         System.out.println("The file email.txt uses standard file wildcards to direct emails to different recipients according to the filename.\n");
129         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  * $Log: DirChangeEmailAlerter.java,v $
134  * Revision 1.7  2004/07/19 14:03:25  johndavidtaylor
135  * try to ensure it doesn't just stop of its own accord
136  *
137  * Revision 1.6  2004/06/19 13:04:44  johndavidtaylor
138  * Brought build up to scratch
139  *
140  * Revision 1.5  2004/06/03 16:52:47  johndavidtaylor
141  * Now gets email config from a properties file.
142  *
143  * Revision 1.4  2004/06/03 16:31:13  johndavidtaylor
144  * Enabled email rather than console
145  *
146  * Revision 1.3  2004/06/03 15:50:25  johndavidtaylor
147  * Refactored DirChangeFileStateMonitor's behaviour when 
148  * the directory it monitors disappears.  Wired up the DirChangeEmailAlerter.
149  * Revision 1.2 2004/05/04 11:36:19
150  * johndavidtaylor *** empty log message ***
151  */