001package ca.bc.webarts.tools;
002
003import ca.bc.webarts.widgets.ClassConstants;
004
005import java.util.Enumeration;
006import java.io.InputStream;
007import java.io.File;
008import java.io.FileInputStream;
009import java.io.IOException;
010import java.lang.Process;
011import java.lang.Runtime;
012import java.util.Arrays;
013import java.util.Properties;
014
015import java.util.Vector;
016
017/**
018 *  An application class that Runs applications at a their designated times &/or
019 *  intervals. <P>
020 *
021 *  It requires a directory of config files for each TimedApp that it will queue
022 *  up. This directory is specified on the commandline as a string arg.
023 *
024 * @author     Tom Gutwin
025 * @created    June 7, 2001
026 */
027public class TimedAppRunner
028{
029  /**
030   *  Description of the Field
031   *
032   * @since
033   */
034  public final static String[] CONSTNAMES = { "SECONDS_INTERVAL",
035                                              "MINUTES_INTERVAL",
036                                              "HOURLY_INTERVAL",
037                                              "DAILY_INTERVAL",
038                                              "WEEKLY_INTERVAL",
039                                              "MONTLY_INTERVAL",
040                                              "YEARLY_INTERVAL"};
041  /**
042   *  Description of the Field
043   *
044   * @since
045   */
046  public final static String usage_ = "Usage:\njava " +
047      "ca.bc.webarts.tools.TimedAppRunner {timedAppPropertiesDir}\n";
048
049
050  /**
051   *  Description of the Field
052   *
053   * @since
054   */
055  public final static ClassConstants CLASS_CONSTANTS =
056      new ClassConstants(CONSTNAMES);
057
058  /**
059   *  Description of the Field
060   *
061   * @since
062   */
063  private static String  timedAppDirectory_ = File.separator + "timedApps";
064
065  /**
066   *  A Vector of the timed apps being controlled.
067   *
068   * @since
069   */
070  private static Vector  timedApps_ = new Vector();
071
072
073  /**
074   *  The filenames of the apps being timed.
075   *
076   * @since
077   */
078  private static Vector  fileName_ = new Vector();
079
080
081  /**
082   *  Flags if the timedApp config directory is recursed.
083   *
084   * @since
085   */
086  private static boolean recursedDirectory_ = false;
087
088  /**
089   *  The directory(File) for the Timed App Config files.
090   *
091   * @since
092   */
093  private static File    dirFile_ = null;
094
095
096  /**
097   *  The Initail start Time for this Run.
098   *
099   * @since
100   */
101  private static long    initTime_ = 0l;
102
103
104  /**
105   *  Constructor for the TimedAppRunner object
106   *
107   * @since
108   */
109  public TimedAppRunner()
110  {
111  }
112
113
114  /**
115   *  The main program for the TimedAppRunner class
116   *
117   * @param  args  The command line arguments
118   * @since
119   */
120  public static void main(String[] args)
121  {
122    // get the directory file
123    if (args.length == 1)
124    {
125      timedAppDirectory_ = args[0];
126      initDir(timedAppDirectory_, false);
127      System.out.println("Finished TimedApp Init:" + timedApps_.size());
128      executeLoop();
129    }
130    else
131    {
132      System.out.println(usage_);
133    }
134  }
135
136
137  /**
138  * Loops through all the TimedApps that have been instantiated and thread
139  * off a new Thread for each to handle their timing/waiting and executing.
140  *
141  **/
142  static private void executeLoop()
143  {
144    initTime_ = System.currentTimeMillis();
145    for (Enumeration myEnum = timedApps_.elements();
146         myEnum.hasMoreElements();)
147         ((TimedApp)myEnum.nextElement()).run();
148
149  }
150
151
152  /**
153   *  Goes through the properties files in the directory spec'd by tthe
154   * passed in dir filename and instantiates the TimedApps.
155   *
156   * @param  dirFileName  the filename of the dir tolook in for the
157   *                      properties files
158   * @param  recurse  Description of Parameter
159   * @return          Description of the Returned Value
160   * @since
161   */
162  public static boolean initDir(String dirFileName, boolean recurse)
163  {
164    boolean retVal = initDirFile(dirFileName);
165    if (retVal)
166    {
167      String[] files = dirFile_.list();
168      fileName_.addAll(new Vector(Arrays.asList(files)));
169      File tempFile;
170      String tempFileName;
171      for (int i = 0; i < files.length; i++)
172      {
173        tempFileName = dirFileName + File.separator + files[i];
174        tempFile = new File(tempFileName);
175        if (!tempFile.isDirectory() && tempFileName.endsWith("prop"))
176        {
177          try
178          {
179            timedApps_.add(new TimedApp(timedAppDirectory_ +
180                                        File.separator +
181                                        files[i]));
182          }
183          catch (Exception ex)
184          {
185            System.out.println("ERROR: Unable to add TimedApp " + files[i]);
186          }
187        }
188        else if (recurse)
189        {
190          if (recurseInitDir(tempFileName, true) && !recursedDirectory_)
191          {
192            recursedDirectory_ = true;
193          }
194        }
195      }
196    }
197    return retVal;
198  }
199
200
201  /**
202   *  Initializes the dirFile field with a new File represented by the passed
203   *  in String.
204   *
205   * @param  s  Description of Parameter
206   * @return    Description of the Returned Value
207   * @since
208   */
209  private static boolean initDirFile(String s)
210  {
211    boolean retVal = false;
212    dirFile_ = new File(s);
213    if (dirFile_.isDirectory() && dirFile_.canRead())
214    {
215      retVal = true;
216    }
217    return retVal;
218  }
219
220
221  /**
222   *  Description of the Method
223   *
224   * @param  s        Description of Parameter
225   * @param  recurse  Description of Parameter
226   * @return          Description of the Returned Value
227   * @since
228   */
229  private static boolean recurseInitDir(String s, boolean recurse)
230  {
231    return true;
232  }
233
234
235
236}
237