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.common.RegExpCreator;
10  import org.abraracourcix.alipes.listeners.Listener;
11  import org.abraracourcix.alipes.listeners.filter.EventFilterListener;
12  import org.abraracourcix.alipes.listeners.logging.ConsoleListener;
13  import org.abraracourcix.alipes.monitors.file.FileEvent;
14  import org.abraracourcix.alipes.monitors.file.FileStateMonitor;
15  import org.abraracourcix.alipes.monitors.file.FileStateMonitorFactory;
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  /***
19   * Takes a filename wildcard pattern, a stale interval, and watches those files
20   * to see if they're deleted/stale/created or change @TODO sort out the
21   * difficulty of entering wildcards on the commandline!
22   * 
23   * @author jdt
24   */
25  public final class FileWildCardChangeConsoleAlerter {
26      /***
27       * Hide public Constructor
28       *  
29       */
30      private FileWildCardChangeConsoleAlerter() {
31      }
32      /***
33       * Log
34       */
35      private static Log log =
36          LogFactory.getLog(FileWildCardChangeConsoleAlerter.class);
37      /***
38       * Set the wheels in motion
39       * 
40       * @param args
41       *            rootfolder wildcard staletime(seconds)
42       * @throws InterruptedException
43       *             if you hit ctrl-C
44       * @throws IOException
45       *             if there's a prob finding the files
46       */
47      public static void main(final String[] args)
48          throws InterruptedException, IOException {
49          if (args.length != 3) {
50              giveHelp();
51              return;
52          }
53          final String dirName = args[0];
54          final String wildCards = args[1];
55          final String interval = args[2];
56          log.info("dirName=" + dirName);
57          log.info("wildCards=" + wildCards);
58          log.info("interval=" + interval);
59          final File root = new File(dirName);
60          if (!(root.exists())) {
61              throw new IOException("Directory " + dirName + " doesn't exist");
62          }
63          if (!root.isDirectory()) {
64              throw new IOException(dirName + " isn't a directory");
65          }
66          int intervalSecs;
67          try {
68              intervalSecs = Integer.parseInt(interval);
69          } catch (NumberFormatException nfe) {
70              throw new NumberFormatException("3rd argument must be an integer number of seconds ");
71          }
72          System.out.println("Ctrl-C when you're bored");
73          final String regExp = RegExpCreator.getRegExp(wildCards);
74          final ConsoleListener listener = new ConsoleListener();
75          final FileEvent[] interestingEvents =
76              {
77                  FileEvent.CREATED,
78                  FileEvent.DELETED,
79                  FileEvent.GONE_STALE,
80                  FileEvent.UPDATED };
81          final Listener filter =
82              new EventFilterListener(
83                  listener,
84                  interestingEvents,
85                  EventFilterListener.FilterType.INCLUDED);
86          final int pollFrequency = 10; //in seconds
87          final FileStateMonitor mon =
88              FileStateMonitorFactory.getFileStateMonitor(
89                  root,
90                  regExp,
91                  filter,
92                  pollFrequency);
93          mon.setStaleInterval(intervalSecs);
94          Thread.sleep(24 * 60 * 60 * 1000); //wait forever, well a day anyway
95      }
96      /***
97       *  
98       */
99      private static void giveHelp() {
100         System.out.println(
101             "Usage:\n java -jar your_uber_jar_here.jar dir files staleInt");
102         System.out.println("where:\ndir is the root directory e.g. /tmp");
103         System.out.println("files is a wildcard expression e.g. *.txt");
104         System.out.println(
105             "and staleInt is the time in seconds before a file is considered stale");
106     }
107 }
108 /*
109  * $Log: FileWildCardChangeConsoleAlerter.java,v $
110  * Revision 1.1  2004/04/27 10:57:51  johndavidtaylor
111  * changed package name
112  *
113  * Revision 1.1  2004/03/17 21:16:51  johndavidtaylor
114  * Copied across from incubation in beanpeeler
115  *
116  * Revision 1.2  2004/03/11 15:46:34  johndavidtaylor
117  * Applied coding standards
118  *
119  * Revision 1.1  2004/03/11 09:49:22  johndavidtaylor
120  * Loads of updates
121  *
122  *  
123  */