Program: 106288
Project: 106288
Buggy source:
private void initiateTemplateDownload(DataObject template,DataStore store,AsyncCompletionCallback<DownloadAnswer> callback){
  boolean downloadJobExists=false;
  TemplateDataStoreVO vmTemplateStore=null;
  vmTemplateStore=_vmTemplateStoreDao.findByStoreTemplate(store.getId(),template.getId());
  if (vmTemplateStore == null) {
    vmTemplateStore=new TemplateDataStoreVO(store.getId(),template.getId(),new Date(),0,VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED,null,null,"String_Node_Str",null,template.getUri());
    _vmTemplateStoreDao.persist(vmTemplateStore);
  }
 else   if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
    downloadJobExists=true;
  }
  Long maxTemplateSizeInBytes=getMaxTemplateSizeInBytes();
  if (vmTemplateStore != null) {
    start();
    VirtualMachineTemplate tmpl=this._templateDao.findById(template.getId());
    DownloadCommand dcmd=new DownloadCommand(store.getTO(),tmpl,maxTemplateSizeInBytes);
    dcmd.setProxy(getHttpProxy());
    if (downloadJobExists) {
      dcmd=new DownloadProgressCommand(dcmd,vmTemplateStore.getJobId(),RequestType.GET_OR_RESTART);
    }
    if (vmTemplateStore.isCopy()) {
      dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER,_copyAuthPasswd);
    }
    EndPoint ep=_epSelector.select(template);
    if (ep == null) {
      s_logger.warn("String_Node_Str" + store.getName());
      return;
    }
    DownloadListener dl=new DownloadListener(ep,store,template,_timer,this,dcmd,callback);
    if (downloadJobExists) {
      dl.setCurrState(vmTemplateStore.getDownloadState());
    }
    DownloadListener old=null;
synchronized (_listenerTemplateMap) {
      old=_listenerTemplateMap.put(vmTemplateStore,dl);
    }
    if (old != null) {
      old.abandon();
    }
    try {
      ep.sendMessageAsyncWithListener(dcmd,dl);
    }
 catch (    Exception e) {
      s_logger.warn("String_Node_Str" + template.getId() + "String_Node_Str"+ store.getName(),e);
      dl.setDisconnected();
      dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
    }
  }
}

Fixed version (ground truth):
private void initiateTemplateDownload(DataObject template,DataStore store,AsyncCompletionCallback<DownloadAnswer> callback){
  boolean downloadJobExists=false;
  TemplateDataStoreVO vmTemplateStore=null;
  vmTemplateStore=_vmTemplateStoreDao.findByStoreTemplate(store.getId(),template.getId());
  if (vmTemplateStore == null) {
    vmTemplateStore=new TemplateDataStoreVO(store.getId(),template.getId(),new Date(),0,VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED,null,null,"String_Node_Str",null,template.getUri());
    _vmTemplateStoreDao.persist(vmTemplateStore);
  }
 else   if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
    downloadJobExists=true;
  }
  Long maxTemplateSizeInBytes=getMaxTemplateSizeInBytes();
  if (vmTemplateStore != null) {
    start();
    VirtualMachineTemplate tmpl=this._templateDao.findById(template.getId());
    DownloadCommand dcmd=new DownloadCommand(store.getTO(),tmpl,maxTemplateSizeInBytes);
    dcmd.setProxy(getHttpProxy());
    if (downloadJobExists) {
      dcmd=new DownloadProgressCommand(dcmd,vmTemplateStore.getJobId(),RequestType.GET_OR_RESTART);
    }
    if (vmTemplateStore.isCopy()) {
      dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER,_copyAuthPasswd);
    }
    EndPoint ep=_epSelector.select(template);
    if (ep == null) {
      s_logger.warn("String_Node_Str" + store.getName());
      return;
    }
    DownloadListener dl=new DownloadListener(ep,store,template,_timer,this,dcmd,callback);
    ComponentContext.inject(dl);
    if (downloadJobExists) {
      dl.setCurrState(vmTemplateStore.getDownloadState());
    }
    DownloadListener old=null;
synchronized (_listenerTemplateMap) {
      old=_listenerTemplateMap.put(vmTemplateStore,dl);
    }
    if (old != null) {
      old.abandon();
    }
    try {
      ep.sendMessageAsyncWithListener(dcmd,dl);
    }
 catch (    Exception e) {
      s_logger.warn("String_Node_Str" + template.getId() + "String_Node_Str"+ store.getName(),e);
      dl.setDisconnected();
      dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
    }
  }
}