private long getUploadTemplateSize(String jobId) {
   UploadJob uj = jobs.get(jobId);
   if (uj != null) {
     return uj.getTemplatesize();
   }
   return 0;
 }
 @Override
 public int getUploadPct(String jobId) {
   UploadJob uj = jobs.get(jobId);
   if (uj != null) {
     return uj.getTemplateUploader().getUploadPercent();
   }
   return 0;
 }
 @Override
 public String getUploadError(String jobId) {
   UploadJob uj = jobs.get(jobId);
   if (uj != null) {
     return uj.getTemplateUploader().getUploadError();
   }
   return null;
 }
 @Override
 public Status getUploadStatus(String jobId) {
   UploadJob job = jobs.get(jobId);
   if (job != null) {
     TemplateUploader tu = job.getTemplateUploader();
     if (tu != null) {
       return tu.getStatus();
     }
   }
   return Status.UNKNOWN;
 }
 private UploadAnswer handleUploadProgressCmd(UploadProgressCommand cmd) {
   String jobId = cmd.getJobId();
   UploadAnswer answer;
   UploadJob uj = null;
   if (jobId != null) uj = jobs.get(jobId);
   if (uj == null) {
     return new UploadAnswer(
         null, 0, "Cannot find job", com.cloud.storage.UploadVO.Status.UNKNOWN, "", "", 0);
   }
   TemplateUploader td = uj.getTemplateUploader();
   switch (cmd.getRequest()) {
     case GET_STATUS:
       break;
     case ABORT:
       td.stopUpload();
       sleep();
       break;
       /*case RESTART:
       td.stopUpload();
       sleep();
       threadPool.execute(td);
       break;*/
     case PURGE:
       td.stopUpload();
       answer =
           new UploadAnswer(
               jobId,
               getUploadPct(jobId),
               getUploadError(jobId),
               getUploadStatus2(jobId),
               getUploadLocalPath(jobId),
               getInstallPath(jobId),
               getUploadTemplateSize(jobId));
       jobs.remove(jobId);
       return answer;
     default:
       break; // TODO
   }
   return new UploadAnswer(
       jobId,
       getUploadPct(jobId),
       getUploadError(jobId),
       getUploadStatus2(jobId),
       getUploadLocalPath(jobId),
       getInstallPath(jobId),
       getUploadTemplateSize(jobId));
 }
  /**
   * Get notified of change of job status. Executed in context of uploader thread
   *
   * @param jobId the id of the job
   * @param status the status of the job
   */
  public void setUploadStatus(String jobId, Status status) {
    UploadJob uj = jobs.get(jobId);
    if (uj == null) {
      s_logger.warn("setUploadStatus for jobId: " + jobId + ", status=" + status + " no job found");
      return;
    }
    TemplateUploader tu = uj.getTemplateUploader();
    s_logger.warn("Upload Completion for jobId: " + jobId + ", status=" + status);
    s_logger.warn(
        "UploadedBytes="
            + tu.getUploadedBytes()
            + ", error="
            + tu.getUploadError()
            + ", pct="
            + tu.getUploadPercent());

    switch (status) {
      case ABORTED:
      case NOT_STARTED:
      case UNRECOVERABLE_ERROR:
        // Delete the entity only if its a volume. TO DO - find a better way of finding it a volume.
        if (uj.getTemplateUploader().getUploadLocalPath().indexOf("volume") > -1) {
          uj.cleanup();
        }
        break;
      case UNKNOWN:
        return;
      case IN_PROGRESS:
        s_logger.info("Resuming jobId: " + jobId + ", status=" + status);
        tu.setResume(true);
        threadPool.execute(tu);
        break;
      case RECOVERABLE_ERROR:
        threadPool.execute(tu);
        break;
      case UPLOAD_FINISHED:
        tu.setUploadError("Upload success, starting install ");
        String result = postUpload(jobId);
        if (result != null) {
          s_logger.error("Failed post upload script: " + result);
          tu.setStatus(Status.UNRECOVERABLE_ERROR);
          tu.setUploadError("Failed post upload script: " + result);
        } else {
          s_logger.warn(
              "Upload completed successfully at " + new SimpleDateFormat().format(new Date()));
          tu.setStatus(Status.POST_UPLOAD_FINISHED);
          tu.setUploadError(
              "Upload completed successfully at " + new SimpleDateFormat().format(new Date()));
        }
        // Delete the entity only if its a volume. TO DO - find a better way of finding it a volume.
        if (uj.getTemplateUploader().getUploadLocalPath().indexOf("volume") > -1) {
          uj.cleanup();
        }
        break;
      default:
        break;
    }
  }