Program: 89234
Project: 89234
Buggy source:
@Override public void handleTemplateSync(HostVO ssHost){
  if (ssHost == null) {
    s_logger.warn("String_Node_Str");
    return;
  }
  long sserverId=ssHost.getId();
  long zoneId=ssHost.getDataCenterId();
  if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  Map<String,TemplateInfo> templateInfos=listTemplate(ssHost);
  if (templateInfos == null) {
    return;
  }
  Set<VMTemplateVO> toBeDownloaded=new HashSet<VMTemplateVO>();
  List<VMTemplateVO> allTemplates=_templateDao.listAllInZone(zoneId);
  List<VMTemplateVO> rtngTmplts=_templateDao.listAllSystemVMTemplates();
  List<VMTemplateVO> defaultBuiltin=_templateDao.listDefaultBuiltinTemplates();
  if (rtngTmplts != null) {
    for (    VMTemplateVO rtngTmplt : rtngTmplts) {
      if (!allTemplates.contains(rtngTmplt)) {
        allTemplates.add(rtngTmplt);
      }
    }
  }
  if (defaultBuiltin != null) {
    for (    VMTemplateVO builtinTmplt : defaultBuiltin) {
      if (!allTemplates.contains(builtinTmplt)) {
        allTemplates.add(builtinTmplt);
      }
    }
  }
  toBeDownloaded.addAll(allTemplates);
  for (  VMTemplateVO tmplt : allTemplates) {
    String uniqueName=tmplt.getUniqueName();
    VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
    if (templateInfos.containsKey(uniqueName)) {
      TemplateInfo tmpltInfo=templateInfos.remove(uniqueName);
      toBeDownloaded.remove(tmplt);
      if (tmpltHost != null) {
        s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str");
        if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
          tmpltHost.setErrorString("String_Node_Str");
        }
        if (tmpltInfo.isCorrupted()) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          String msg="String_Node_Str" + tmplt.getName() + "String_Node_Str"+ tmplt.getId()+ "String_Node_Str"+ tmpltHost.getId();
          tmpltHost.setErrorString(msg);
          s_logger.info("String_Node_Str");
          if (tmplt.getUrl() == null) {
            msg="String_Node_Str" + tmplt + "String_Node_Str"+ tmpltInfo.getInstallPath()+ "String_Node_Str"+ tmpltHost.getHostId();
            s_logger.warn(msg);
          }
 else {
            toBeDownloaded.add(tmplt);
          }
        }
 else {
          tmpltHost.setDownloadPercent(100);
          tmpltHost.setDownloadState(Status.DOWNLOADED);
          tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
          tmpltHost.setSize(tmpltInfo.getSize());
          tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
          tmpltHost.setLastUpdated(new Date());
        }
        _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
      }
 else {
        tmpltHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),100,Status.DOWNLOADED,null,null,null,tmpltInfo.getInstallPath(),tmplt.getUrl());
        tmpltHost.setSize(tmpltInfo.getSize());
        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
        _vmTemplateHostDao.persist(tmpltHost);
        VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
        if (tmpltZoneVO == null) {
          tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
          _vmTemplateZoneDao.persist(tmpltZoneVO);
        }
 else {
          tmpltZoneVO.setLastUpdated(new Date());
          _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
        }
      }
      continue;
    }
    if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
    }
 else     if (tmpltHost == null) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
      VMTemplateHostVO templtHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),0,Status.NOT_DOWNLOADED,null,null,null,null,tmplt.getUrl());
      _vmTemplateHostDao.persist(templtHost);
      VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
      if (tmpltZoneVO == null) {
        tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
        _vmTemplateZoneDao.persist(tmpltZoneVO);
      }
 else {
        tmpltZoneVO.setLastUpdated(new Date());
        _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
      }
    }
  }
  if (toBeDownloaded.size() > 0) {
    List<HypervisorType> availHypers=_clusterDao.getAvailableHypervisorInZone(zoneId);
    if (availHypers.isEmpty()) {
      availHypers.add(HypervisorType.KVM);
    }
    availHypers.remove(HypervisorType.BareMetal);
    availHypers.add(HypervisorType.None);
    for (    VMTemplateVO tmplt : toBeDownloaded) {
      if (tmplt.getUrl() == null) {
        continue;
      }
      if (!tmplt.isPublicTemplate() && !tmplt.isFeatured()) {
        VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
        if (tmpltHost == null) {
          continue;
        }
      }
      if (availHypers.contains(tmplt.getHypervisorType())) {
        s_logger.debug("String_Node_Str" + tmplt.getName() + "String_Node_Str"+ ssHost.getName());
        downloadTemplateToStorage(tmplt,ssHost);
      }
    }
  }
  for (  String uniqueName : templateInfos.keySet()) {
    TemplateInfo tInfo=templateInfos.get(uniqueName);
    DeleteTemplateCommand dtCommand=new DeleteTemplateCommand(ssHost.getStorageUrl(),tInfo.getInstallPath());
    try {
      _agentMgr.sendToSecStorage(ssHost,dtCommand,null);
    }
 catch (    AgentUnavailableException e) {
      String err="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
      s_logger.error(err);
      return;
    }
    String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
    s_logger.info(description);
  }
}

Fixed version (ground truth):
@Override public void handleTemplateSync(HostVO ssHost){
  if (ssHost == null) {
    s_logger.warn("String_Node_Str");
    return;
  }
  long sserverId=ssHost.getId();
  long zoneId=ssHost.getDataCenterId();
  if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  Map<String,TemplateInfo> templateInfos=listTemplate(ssHost);
  if (templateInfos == null) {
    return;
  }
  Set<VMTemplateVO> toBeDownloaded=new HashSet<VMTemplateVO>();
  List<VMTemplateVO> allTemplates=_templateDao.listAllInZone(zoneId);
  List<VMTemplateVO> rtngTmplts=_templateDao.listAllSystemVMTemplates();
  List<VMTemplateVO> defaultBuiltin=_templateDao.listDefaultBuiltinTemplates();
  if (rtngTmplts != null) {
    for (    VMTemplateVO rtngTmplt : rtngTmplts) {
      if (!allTemplates.contains(rtngTmplt)) {
        allTemplates.add(rtngTmplt);
      }
    }
  }
  if (defaultBuiltin != null) {
    for (    VMTemplateVO builtinTmplt : defaultBuiltin) {
      if (!allTemplates.contains(builtinTmplt)) {
        allTemplates.add(builtinTmplt);
      }
    }
  }
  toBeDownloaded.addAll(allTemplates);
  for (  VMTemplateVO tmplt : allTemplates) {
    String uniqueName=tmplt.getUniqueName();
    VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
    if (templateInfos.containsKey(uniqueName)) {
      TemplateInfo tmpltInfo=templateInfos.remove(uniqueName);
      toBeDownloaded.remove(tmplt);
      if (tmpltHost != null) {
        s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str");
        if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
          tmpltHost.setErrorString("String_Node_Str");
        }
        if (tmpltInfo.isCorrupted()) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          String msg="String_Node_Str" + tmplt.getName() + "String_Node_Str"+ tmplt.getId()+ "String_Node_Str"+ tmpltHost.getId();
          tmpltHost.setErrorString(msg);
          s_logger.info("String_Node_Str");
          if (tmplt.getUrl() == null) {
            msg="String_Node_Str" + tmplt + "String_Node_Str"+ tmpltInfo.getInstallPath()+ "String_Node_Str"+ tmpltHost.getHostId();
            s_logger.warn(msg);
          }
 else {
            toBeDownloaded.add(tmplt);
          }
        }
 else {
          tmpltHost.setDownloadPercent(100);
          tmpltHost.setDownloadState(Status.DOWNLOADED);
          tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
          tmpltHost.setSize(tmpltInfo.getSize());
          tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
          tmpltHost.setLastUpdated(new Date());
        }
        _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
      }
 else {
        tmpltHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),100,Status.DOWNLOADED,null,null,null,tmpltInfo.getInstallPath(),tmplt.getUrl());
        tmpltHost.setSize(tmpltInfo.getSize());
        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
        _vmTemplateHostDao.persist(tmpltHost);
        VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
        if (tmpltZoneVO == null) {
          tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
          _vmTemplateZoneDao.persist(tmpltZoneVO);
        }
 else {
          tmpltZoneVO.setLastUpdated(new Date());
          _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
        }
      }
      continue;
    }
    if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
    }
 else     if (tmpltHost == null) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
      VMTemplateHostVO templtHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),0,Status.NOT_DOWNLOADED,null,null,null,null,tmplt.getUrl());
      _vmTemplateHostDao.persist(templtHost);
      VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
      if (tmpltZoneVO == null) {
        tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
        _vmTemplateZoneDao.persist(tmpltZoneVO);
      }
 else {
        tmpltZoneVO.setLastUpdated(new Date());
        _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
      }
    }
  }
  if (toBeDownloaded.size() > 0) {
    List<HypervisorType> availHypers=_clusterDao.getAvailableHypervisorInZone(zoneId);
    if (availHypers.isEmpty()) {
      availHypers.add(HypervisorType.KVM);
    }
    availHypers.remove(HypervisorType.BareMetal);
    availHypers.add(HypervisorType.None);
    for (    VMTemplateVO tmplt : toBeDownloaded) {
      if (tmplt.getUrl() == null) {
        continue;
      }
      if (!tmplt.isPublicTemplate() && !tmplt.isFeatured()) {
        VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
        if (tmpltHost == null) {
          continue;
        }
      }
      if (availHypers.contains(tmplt.getHypervisorType())) {
        if (_swiftMgr.isSwiftEnabled()) {
          if (_swiftMgr.isTemplateInstalled(tmplt.getId())) {
            continue;
          }
        }
        s_logger.debug("String_Node_Str" + tmplt.getName() + "String_Node_Str"+ ssHost.getName());
        downloadTemplateToStorage(tmplt,ssHost);
      }
    }
  }
  for (  String uniqueName : templateInfos.keySet()) {
    TemplateInfo tInfo=templateInfos.get(uniqueName);
    DeleteTemplateCommand dtCommand=new DeleteTemplateCommand(ssHost.getStorageUrl(),tInfo.getInstallPath());
    try {
      _agentMgr.sendToSecStorage(ssHost,dtCommand,null);
    }
 catch (    AgentUnavailableException e) {
      String err="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
      s_logger.error(err);
      return;
    }
    String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
    s_logger.info(description);
  }
}