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