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