/**
   * Client side method to poll for the oldest run which must be older than localAge. If found, the
   * run will be downloaded into the temp space. and the run name will be returned.
   *
   * @param localAge The age of the oldest local run in the queue
   * @return The file reference to the local run in the directory
   */
  public static File pollRun(long localAge) {
    Config.HostInfo selectedHost = null;
    NameValuePair<Long> selectedRun = null;
    for (int i = 0; i < Config.pollHosts.length; i++) {
      Config.HostInfo pollHost = Config.pollHosts[i];
      NameValuePair<Long> run = null;
      try {
        run = poll(pollHost, localAge);
      } catch (IOException e) {
        logger.log(Level.WARNING, "Error polling " + pollHost.url + '.', e);
      }
      if (run != null && (selectedRun == null || run.value > selectedRun.value)) {
        selectedRun = run;
        selectedHost = pollHost;
      }
    }
    File tmpDir = null;
    if (selectedRun != null) {
      try {
        // Download and unjar the run.
        File tmpJar = download(selectedHost, selectedRun);
        if (tmpJar == null) {
          logger.warning("Download null jar file.");
          return null;
        }
        tmpDir = FileHelper.unjarTmp(tmpJar);
        File metaInf = new File(tmpDir, "META-INF");
        if (!metaInf.isDirectory()) metaInf.mkdir();

        // Create origin file to know where this run came from.
        FileHelper.writeStringToFile(
            selectedHost.name + '.' + selectedRun.name, new File(metaInf, "origin"));
        tmpJar.delete();
      } catch (IOException e) {
        logger.log(
            Level.WARNING,
            "Error downloading run " + selectedRun.name + " from " + selectedHost.url + '.',
            e);
      }
    }
    return tmpDir;
  }
  /**
   * Jars up the run directory.
   *
   * @param run The run to jar up
   * @return The resulting jar file
   * @throws IOException Error creating the jar
   */
  public static File jar(Run run) throws IOException {

    logger.info("Preparing run " + run.getRunId() + " for download.");

    String runId = run.getRunId();

    String jarName = runId + ".jar";
    File jar = new File(Config.TMP_DIR, jarName);

    String[] files = new File(Config.OUT_DIR, runId).list();
    if (jar.exists()) jar.delete();

    FileHelper.jar(Config.OUT_DIR + runId, files, jar.getAbsolutePath());
    return jar;
  }