001    package org.hackystat.tickertape.server;
002    
003    import java.io.File;
004    import java.util.ArrayList;
005    import java.util.List;
006    import java.util.Timer;
007    import java.util.logging.Logger;
008    import org.hackystat.tickertape.tickerlingua.TickerLingua;
009    import org.hackystat.tickertape.tickerlingua.Tickertape;
010    import org.hackystat.utilities.logger.HackystatLogger;
011    
012    /**
013     * The main point of entry for this system. 
014     * Reads in the tickertape.xml file, sets up the timer-based processes. 
015     * @author Philip Johnson
016     */
017    public class Server {
018      /** The logger for this service. */
019      private Logger logger = HackystatLogger.getLogger("org.hackystat.tickertape", "tickertape");
020      /** The tickertape.xml config file. */
021      private File configFile = null;
022      /** The timers associated with the tickers. */
023      List<Timer> timers = new ArrayList<Timer>();
024      
025      
026      /**
027       * Instantiates a new Tickertape server.
028       * Uses either the passed configFile, or the default in ~/.hackystat/tickertape/tickertape.xml
029       * if null is passed.
030       * @param configFile The configFile to use, or null if the default should be used. 
031       */
032      public Server(String configFile) {
033        // Create the tickertape server.
034        File dotTickerTape = new File(System.getProperty("user.home"), 
035            ".hackystat/tickertape/tickertape.xml");
036        this.configFile = (configFile == null) ? dotTickerTape : new File(configFile);
037        if (!this.configFile.exists()) {
038          System.out.printf("%s not found.", configFile);
039        }
040      }
041      
042      /**
043       * Starts up the Tickertape instances. 
044       * @throws Exception If problems occur. 
045       */
046      public void start() throws Exception {
047        System.out.println("Configuring system from: " + configFile.getAbsolutePath());
048        TickerLingua tickerLingua = new TickerLingua(configFile.getAbsolutePath());
049        String loggingLevel = tickerLingua.getLoggingLevel();
050        System.out.println("Setting logging level to: " + loggingLevel);
051        HackystatLogger.setLoggingLevel(this.logger, loggingLevel);
052        
053        // Start up all enabled tickertapes. 
054        for (Tickertape tickertape : tickerLingua.getTickertapes()) {
055          // Sleep for a few seconds between startups.  
056          String id = tickertape.getId();
057          if (tickertape.enabled()) {
058            Timer timer = new Timer();
059            this.timers.add(timer);
060            double wakeupIntervalHours = tickertape.getIntervalHours();
061            logger.info(String.format("Starting tickertape %s to wakeup every %s hours.", id, 
062                wakeupIntervalHours)); 
063            timer.schedule(new TickertapeTask(tickertape, tickerLingua, logger), 0, 
064                (long)(1000L * 60 * 60 * wakeupIntervalHours));
065            // Sleep for a few seconds to let this task complete its setup.
066            Thread.sleep(2 * 1000);
067          }
068          else {
069            logger.info(String.format("Tickertape %s not enabled.  Skipping.", id));
070          }
071        }
072        System.out.println("Finished starting up all of the Tickertape instances.");
073      }
074    
075      /**
076       * Stops all timers that have been started.
077       */
078      public void stop() {
079        logger.info("Cancelling all timers.");
080        for (Timer timer : timers) {
081          timer.cancel();
082        }
083      }
084      
085      
086      /**
087       * Starts up this tickertape instance. 
088       * Reads in the tickerlingua file and sets up timer tasks for each defined and enabled tickertape.
089       * Looks in ~/.hackystat/tickertape/tickerlingua.xml by default. 
090       * @param args If provided, the location of the tickerlingua.xml file. 
091       * @throws Exception if problems occur.
092       */
093      public static void main(String[] args) throws Exception {
094        String configFile = (args.length == 0) ? null : args[0];
095        Server server = new Server(configFile);
096        try {
097          server.start();
098          System.out.println("Tickertape is running. Press return to stop server.");
099          while (System.in.available() == 0) {
100            Thread.sleep(5000);
101          }
102          server.stop();
103        }
104        catch (Exception e) {
105          e.printStackTrace();
106        }
107      }
108    }