View Javadoc

1   /*
2    * $Id: FileShowAllConsoleAlerter.java,v 1.3 2004/03/09 22:29:32
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.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.monitors.file.FileEvent;
13  import org.abraracourcix.alipes.monitors.file.FileStateMonitor;
14  /***
15   * Takes a list of filenames, a stale interval, and watches those files to see
16   * if they're deleted/stale/created or change. Sends an email  if so.
17   * 
18   * @author jdt
19   */
20  public final class FileChangeEmailAlerter {
21      /***
22       * Hide public Constructor
23       */
24      private FileChangeEmailAlerter() {
25      }
26      /***
27       * Set the wheels in motion
28       * 
29       * @param args staletime(seconds) files
30       * @throws InterruptedException if you hit ctrl-C
31       * @throws IOException if there's a prob finding the files
32       */
33      public static void main(final String[] args)
34          throws InterruptedException, IOException {
35          if (args.length < 2) {
36              giveHelp();
37              return;
38          }
39          final String interval = args[0];
40          final File[] files = new File[args.length - 1];
41          for (int i = 0; i < args.length - 1; ++i) {
42              files[i] = new File(args[i + 1]);
43          }
44          final int maxFiles2Show = 5;
45          System.out.println("Monitoring " + files.length + " files:");
46          for (int i = 0; i < Math.min(files.length, maxFiles2Show); ++i) {
47              System.out.println(files[i]);
48          }
49          System.out.println("...");
50          int intervalSecs;
51          try {
52              intervalSecs = Integer.parseInt(interval);
53          } catch (NumberFormatException nfe) {
54              throw new NumberFormatException("3rd argument must be an integer number of seconds ");
55          }
56          System.out.println("Ctrl-C when you're bored");
57          //final Listener listener = new SimpleEmailListener("cvs.astrogrid.org","maven","","jdt@roe.ac.uk","jdt@roe.ac.uk");
58          final Listener listener = new SelectiveEmailListener("cvs.astrogrid.org","maven","","jdt@roe.ac.uk");
59          //final Listener listener = new SelectiveEmailListener("127.0.0.1","jdt","1loubieG1","jdt@roe.ac.uk");
60          //final Listener listener = new SelectiveEmailListener("smtp.mail.yahoo.co.uk","johndavid_taylor","2loubieG2","johndavid_taylor@yahoo.co.uk");
61          final FileEvent[] interestingEvents =
62              {
63                  FileEvent.CREATED,
64                  FileEvent.DELETED,
65                  FileEvent.GONE_STALE};
66          final Listener filter =
67              new EventFilterListener(
68                  listener,
69                  interestingEvents,
70                  EventFilterListener.FilterType.INCLUDED);
71          final int pollFrequency = 30; //in seconds
72          final FileStateMonitor mon =
73              new FileStateMonitor(filter, pollFrequency);
74          mon.addFiles(files);
75          mon.setStaleInterval(intervalSecs);
76          mon.start();
77          Thread.sleep(7*24 * 60 * 60 * 1000); //wait forever, well a week anyway
78      }
79      /***
80       *  
81       */
82      private static void giveHelp() {
83          System.out.println(
84              "Usage:\n java -jar your_uber_jar_here.jar staleInt file1 file2 ...");
85          System.out.println("where:");
86          System.out.println(
87              "staleInt is the time in seconds before a file is considered stale");
88          System.out.println(
89              "file1 etc are the files you want to watch (can use wildcard expressions in Windows and maybe unix");
90      }
91  }
92  /*
93   * $Log: FileChangeEmailAlerter.java,v $
94   * Revision 1.1  2004/04/27 10:57:51  johndavidtaylor
95   * changed package name
96   *
97   * Revision 1.1  2004/03/17 21:16:51  johndavidtaylor
98   * Copied across from incubation in beanpeeler
99   *
100  * Revision 1.4  2004/03/17 20:53:08  johndavidtaylor
101  * changed polling frequency
102  *
103  * Revision 1.3  2004/03/15 11:22:35  johndavidtaylor
104  * Now up the stage where it can be used.  Things still to do: 
105  * change the way email.txt is picked up so it can be modified mid-flight,
106  * remove the annoying logging.
107  *
108  * Revision 1.2  2004/03/14 18:46:21  johndavidtaylor
109  * refactored email
110  *
111  * Revision 1.1  2004/03/14 00:36:46  johndavidtaylor
112  * Added send email capability
113  * Revision 1.3 2004/03/11 15:46:34
114  * johndavidtaylor Applied coding standards Revision 1.2 2004/03/11 11:33:46
115  * johndavidtaylor Refactored the way that the PollingMonitor worked.
116  * Previously it got started in its ctor, but this allowed access to subclass
117  * resources that hadn't yet been initialised. Bad. Now needs to be started
118  * explicitly post-construction. Revision 1.1 2004/03/11 09:49:22
119  * johndavidtaylor Loads of updates
120  */