001    package org.hackystat.telemetry.service.prefetch;
002    
003    import java.util.TimerTask;
004    import java.util.logging.Logger;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    
008    import org.hackystat.telemetry.service.client.TelemetryClient;
009    import org.hackystat.telemetry.service.prefetch.jaxb.PrefetchChart;
010    import org.hackystat.telemetry.service.prefetch.jaxb.TelemetryPrefetch;
011    import org.hackystat.utilities.tstamp.Tstamp;
012    
013    /**
014     * A TimerTask that encapsulates a TelemetryPrefetch instance. 
015     * When this timer's run() method is called, it will construct a set of calls to the Telemetry 
016     * analysis service from the information in the TelemetryPrefetch instance.  
017     * @author Philip Johnson
018     *
019     */
020    public class PrefetchTask extends TimerTask {
021      
022      private TelemetryPrefetch telemetryPrefetch;
023      private Logger logger;
024      private String host; 
025      
026      /**
027       * Create a PrefetchTask, which will run all of the charts in this TelemetryPrefetch instance.
028       * @param telemetryPrefetch The Telemetry Prefetch instance. 
029       * @param logger The logger.
030       * @param host The telemetry service host to be contacted.  
031       */
032      public PrefetchTask(TelemetryPrefetch telemetryPrefetch, Logger logger, String host) {
033        this.telemetryPrefetch = telemetryPrefetch;
034        this.logger = logger;
035        this.host = host;
036      }
037    
038      /**
039       * Invoked automatically by the associated DailyTimer task or by the PrefetchManager on 
040       * startup if RunOnStartup is true.
041       */
042      @Override
043      public void run() {
044        String prefetchName = this.telemetryPrefetch.getName();
045        this.logger.info("Running prefetch: " + prefetchName);
046        // We process each of the Prefetch Charts in this TelemetryPrefetch instance.
047        for (PrefetchChart chart : this.telemetryPrefetch.getPrefetchChart()) {
048          // [1] Try to make an authenticated TelemetryClient using the PrefetchChart data.
049          String user = chart.getAuthorizedUserName();
050          String password = chart.getAuthorizedUserPassword();
051          TelemetryClient client = new TelemetryClient(host, user, password);
052          try {
053            client.authenticate();
054          }
055          catch (Exception e) {
056            String msg = "Prefetch failed authentication, skipping: " + prefetchName + " " + host + " " 
057            + user + " " + password + " " + e.getMessage();
058            this.logger.info(msg);
059            return;
060          }
061          // [2] Ensure that the specified Start Time is a valid XMLGregorianCalendar. 
062          XMLGregorianCalendar startTime;
063          try {
064            startTime = Tstamp.makeTimestamp(chart.getStartTime());
065          }
066          catch (Exception e) {
067            this.logger.info("Prefetch had illegal time, skipping: " + prefetchName + " " 
068                + chart.getStartTime());
069            return;
070          }
071          XMLGregorianCalendar yesterday = Tstamp.incrementDays(Tstamp.makeTimestamp(), -1);
072          // [3] Request the chart from the Telemetry Client.
073          String chartName = chart.getChartName();
074          try {
075            client.getChart(chartName, chart.getProjectOwner(), chart.getProjectName(), 
076                "Day", startTime, yesterday, chart.getChartParameters());
077            this.logger.info("Prefetched chart: " + chartName);
078          }
079          catch (Exception e) {
080            this.logger.info("Error prefetching chart: " + chartName + " " + e.getMessage());
081          }
082        }
083      }
084    }