Program: 105461
Project: 105461
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_TEMPLATE_CREATE,eventDescription="String_Node_Str",async=true) public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
  Long userId=CallContext.current().getCallingUserId();
  if (userId == null) {
    userId=User.UID_SYSTEM;
  }
  final long templateId=command.getEntityId();
  Long volumeId=command.getVolumeId();
  Long snapshotId=command.getSnapshotId();
  VMTemplateVO privateTemplate=null;
  final Long accountId=CallContext.current().getCallingAccountId();
  SnapshotVO snapshot=null;
  VolumeVO volume=null;
  try {
    TemplateInfo tmplInfo=_tmplFactory.getTemplate(templateId,DataStoreRole.Image);
    long zoneId=0;
    if (snapshotId != null) {
      snapshot=_snapshotDao.findById(snapshotId);
      zoneId=snapshot.getDataCenterId();
    }
 else     if (volumeId != null) {
      volume=_volumeDao.findById(volumeId);
      zoneId=volume.getDataCenterId();
    }
    DataStore store=_dataStoreMgr.getImageStore(zoneId);
    if (store == null) {
      throw new CloudRuntimeException("String_Node_Str" + zoneId);
    }
    AsyncCallFuture<TemplateApiResult> future=null;
    if (snapshotId != null) {
      DataStoreRole dataStoreRole=getDataStoreRole(snapshot);
      SnapshotInfo snapInfo=_snapshotFactory.getSnapshot(snapshotId,dataStoreRole);
      if (dataStoreRole == DataStoreRole.Image) {
        DataStore snapStore=snapInfo.getDataStore();
        if (snapStore != null) {
          store=snapStore;
        }
      }
      future=_tmpltSvr.createTemplateFromSnapshotAsync(snapInfo,tmplInfo,store);
    }
 else     if (volumeId != null) {
      VolumeInfo volInfo=_volFactory.getVolume(volumeId);
      future=_tmpltSvr.createTemplateFromVolumeAsync(volInfo,tmplInfo,store);
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
    CommandResult result=null;
    try {
      result=future.get();
      if (result.isFailed()) {
        privateTemplate=null;
        s_logger.debug("String_Node_Str" + result.getResult());
        throw new CloudRuntimeException("String_Node_Str" + result.getResult());
      }
      if (_dataStoreMgr.isRegionStore(store)) {
        _tmpltSvr.associateTemplateToZone(templateId,null);
      }
 else {
        VMTemplateZoneVO templateZone=new VMTemplateZoneVO(zoneId,templateId,new Date());
        _tmpltZoneDao.persist(templateZone);
      }
      privateTemplate=_tmpltDao.findById(templateId);
      TemplateDataStoreVO srcTmpltStore=_tmplStoreDao.findByStoreTemplate(store.getId(),templateId);
      UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE,privateTemplate.getAccountId(),zoneId,privateTemplate.getId(),privateTemplate.getName(),null,privateTemplate.getSourceTemplateId(),srcTmpltStore.getPhysicalSize(),privateTemplate.getSize());
      _usageEventDao.persist(usageEvent);
    }
 catch (    InterruptedException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
catch (    ExecutionException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  finally {
    if (privateTemplate == null) {
      final VolumeVO volumeFinal=volume;
      final SnapshotVO snapshotFinal=snapshot;
      Transaction.execute(new TransactionCallbackNoReturn(){
        @Override public void doInTransactionWithoutResult(        TransactionStatus status){
          _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
          _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
          _tmpltDao.expunge(templateId);
          if (accountId != null) {
            _resourceLimitMgr.decrementResourceCount(accountId,ResourceType.template);
            _resourceLimitMgr.decrementResourceCount(accountId,ResourceType.secondary_storage,new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
          }
        }
      }
);
    }
  }
  if (privateTemplate != null) {
    return privateTemplate;
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_TEMPLATE_CREATE,eventDescription="String_Node_Str",async=true) public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
  final long templateId=command.getEntityId();
  Long volumeId=command.getVolumeId();
  Long snapshotId=command.getSnapshotId();
  VMTemplateVO privateTemplate=null;
  final Long accountId=CallContext.current().getCallingAccountId();
  SnapshotVO snapshot=null;
  VolumeVO volume=null;
  try {
    TemplateInfo tmplInfo=_tmplFactory.getTemplate(templateId,DataStoreRole.Image);
    long zoneId=0;
    if (snapshotId != null) {
      snapshot=_snapshotDao.findById(snapshotId);
      zoneId=snapshot.getDataCenterId();
    }
 else     if (volumeId != null) {
      volume=_volumeDao.findById(volumeId);
      zoneId=volume.getDataCenterId();
    }
    DataStore store=_dataStoreMgr.getImageStore(zoneId);
    if (store == null) {
      throw new CloudRuntimeException("String_Node_Str" + zoneId);
    }
    AsyncCallFuture<TemplateApiResult> future=null;
    if (snapshotId != null) {
      DataStoreRole dataStoreRole=getDataStoreRole(snapshot);
      SnapshotInfo snapInfo=_snapshotFactory.getSnapshot(snapshotId,dataStoreRole);
      if (dataStoreRole == DataStoreRole.Image) {
        DataStore snapStore=snapInfo.getDataStore();
        if (snapStore != null) {
          store=snapStore;
        }
      }
      future=_tmpltSvr.createTemplateFromSnapshotAsync(snapInfo,tmplInfo,store);
    }
 else     if (volumeId != null) {
      VolumeInfo volInfo=_volFactory.getVolume(volumeId);
      future=_tmpltSvr.createTemplateFromVolumeAsync(volInfo,tmplInfo,store);
    }
 else {
      throw new CloudRuntimeException("String_Node_Str");
    }
    CommandResult result=null;
    try {
      result=future.get();
      if (result.isFailed()) {
        privateTemplate=null;
        s_logger.debug("String_Node_Str" + result.getResult());
        throw new CloudRuntimeException("String_Node_Str" + result.getResult());
      }
      if (_dataStoreMgr.isRegionStore(store)) {
        _tmpltSvr.associateTemplateToZone(templateId,null);
      }
 else {
        VMTemplateZoneVO templateZone=new VMTemplateZoneVO(zoneId,templateId,new Date());
        _tmpltZoneDao.persist(templateZone);
      }
      privateTemplate=_tmpltDao.findById(templateId);
      TemplateDataStoreVO srcTmpltStore=_tmplStoreDao.findByStoreTemplate(store.getId(),templateId);
      UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE,privateTemplate.getAccountId(),zoneId,privateTemplate.getId(),privateTemplate.getName(),null,privateTemplate.getSourceTemplateId(),srcTmpltStore.getPhysicalSize(),privateTemplate.getSize());
      _usageEventDao.persist(usageEvent);
    }
 catch (    InterruptedException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
catch (    ExecutionException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  finally {
    if (privateTemplate == null) {
      final VolumeVO volumeFinal=volume;
      final SnapshotVO snapshotFinal=snapshot;
      Transaction.execute(new TransactionCallbackNoReturn(){
        @Override public void doInTransactionWithoutResult(        TransactionStatus status){
          _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
          _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
          _tmpltDao.expunge(templateId);
          if (accountId != null) {
            _resourceLimitMgr.decrementResourceCount(accountId,ResourceType.template);
            _resourceLimitMgr.decrementResourceCount(accountId,ResourceType.secondary_storage,new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
          }
        }
      }
);
    }
  }
  if (privateTemplate != null) {
    return privateTemplate;
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
}