Program: 107051
Project: 107051
Buggy source:
@Override public List<? extends Cluster> discoverCluster(AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException {
  Long dcId=cmd.getZoneId();
  Long podId=cmd.getPodId();
  String clusterName=cmd.getClusterName();
  String url=cmd.getUrl();
  String username=cmd.getUsername();
  String password=cmd.getPassword();
  url=URLDecoder.decode(url);
  URI uri=null;
  DataCenterVO zone=_dcDao.findById(dcId);
  if (zone == null) {
    throw new InvalidParameterValueException("String_Node_Str" + dcId);
  }
  Account account=UserContext.current().getCaller();
  if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getType())) {
    throw new PermissionDeniedException("String_Node_Str" + dcId);
  }
  if (podId != null) {
    if (_podDao.findById(podId) == null) {
      throw new InvalidParameterValueException("String_Node_Str" + podId);
    }
    HostPodVO pod=_podDao.findById(podId);
    if (!Long.valueOf(pod.getDataCenterId()).equals(dcId)) {
      throw new InvalidParameterValueException("String_Node_Str" + podId + "String_Node_Str"+ dcId);
    }
  }
  if (clusterName == null || clusterName.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getHypervisor() == null || cmd.getHypervisor().isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Hypervisor.HypervisorType hypervisorType=Hypervisor.HypervisorType.getType(cmd.getHypervisor());
  if (hypervisorType == null) {
    s_logger.error("String_Node_Str" + cmd.getHypervisor() + "String_Node_Str");
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getHypervisor() + "String_Node_Str");
  }
  Cluster.ClusterType clusterType=null;
  if (cmd.getClusterType() != null && !cmd.getClusterType().isEmpty()) {
    clusterType=Cluster.ClusterType.valueOf(cmd.getClusterType());
  }
  if (clusterType == null) {
    clusterType=Cluster.ClusterType.CloudManaged;
  }
  Grouping.AllocationState allocationState=null;
  if (cmd.getAllocationState() != null && !cmd.getAllocationState().isEmpty()) {
    try {
      allocationState=Grouping.AllocationState.valueOf(cmd.getAllocationState());
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getAllocationState() + "String_Node_Str");
    }
  }
  if (allocationState == null) {
    allocationState=Grouping.AllocationState.Enabled;
  }
  Discoverer discoverer=getMatchingDiscover(hypervisorType);
  if (discoverer == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getHypervisor());
  }
  List<ClusterVO> result=new ArrayList<ClusterVO>();
  long clusterId=0;
  ClusterVO cluster=new ClusterVO(dcId,podId,clusterName);
  cluster.setHypervisorType(cmd.getHypervisor());
  cluster.setClusterType(clusterType);
  cluster.setAllocationState(allocationState);
  try {
    cluster=_clusterDao.persist(cluster);
  }
 catch (  Exception e) {
    throw new CloudRuntimeException("String_Node_Str" + clusterName + "String_Node_Str"+ podId+ "String_Node_Str"+ dcId,e);
  }
  clusterId=cluster.getId();
  result.add(cluster);
  if (clusterType == Cluster.ClusterType.CloudManaged) {
    return result;
  }
  Map<String,String> details=new HashMap<String,String>();
  details.put("String_Node_Str",url);
  details.put("String_Node_Str",username);
  details.put("String_Node_Str",password);
  _clusterDetailsDao.persist(cluster.getId(),details);
  boolean success=false;
  try {
    try {
      uri=new URI(UriUtils.encodeURIComponent(url));
      if (uri.getScheme() == null) {
        throw new InvalidParameterValueException("String_Node_Str" + url + "String_Node_Str");
      }
 else       if (uri.getScheme().equalsIgnoreCase("String_Node_Str")) {
        if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("String_Node_Str") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("String_Node_Str")) {
          throw new InvalidParameterValueException("String_Node_Str");
        }
      }
    }
 catch (    URISyntaxException e) {
      throw new InvalidParameterValueException(url + "String_Node_Str");
    }
    List<HostVO> hosts=new ArrayList<HostVO>();
    Map<? extends ServerResource,Map<String,String>> resources=null;
    try {
      resources=discoverer.find(dcId,podId,clusterId,uri,username,password);
    }
 catch (    Exception e) {
      s_logger.info("String_Node_Str" + discoverer.getName());
    }
    if (resources != null) {
      for (      Map.Entry<? extends ServerResource,Map<String,String>> entry : resources.entrySet()) {
        ServerResource resource=entry.getKey();
        if (hypervisorType == Hypervisor.HypervisorType.Hyperv) {
          break;
        }
        AgentAttache attache=simulateStart(resource,entry.getValue(),true,null,null);
        if (attache != null) {
          hosts.add(_hostDao.findById(attache.getId()));
        }
        discoverer.postDiscovery(hosts,_nodeId);
      }
      s_logger.info("String_Node_Str" + discoverer.getName());
      success=true;
      return result;
    }
    s_logger.warn("String_Node_Str" + url);
    throw new DiscoveryException("String_Node_Str");
  }
 catch (  Throwable e) {
    s_logger.error("String_Node_Str",e);
    throw new DiscoveryException("String_Node_Str");
  }
 finally {
    if (!success) {
      _clusterDetailsDao.deleteDetails(clusterId);
      _clusterDao.remove(clusterId);
    }
  }
}

Fixed version (ground truth):
@Override public List<? extends Cluster> discoverCluster(AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException {
  Long dcId=cmd.getZoneId();
  Long podId=cmd.getPodId();
  String clusterName=cmd.getClusterName();
  String url=cmd.getUrl();
  String username=cmd.getUsername();
  String password=cmd.getPassword();
  if (url != null)   url=URLDecoder.decode(url);
  URI uri=null;
  DataCenterVO zone=_dcDao.findById(dcId);
  if (zone == null) {
    throw new InvalidParameterValueException("String_Node_Str" + dcId);
  }
  Account account=UserContext.current().getCaller();
  if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getType())) {
    throw new PermissionDeniedException("String_Node_Str" + dcId);
  }
  if (podId != null) {
    if (_podDao.findById(podId) == null) {
      throw new InvalidParameterValueException("String_Node_Str" + podId);
    }
    HostPodVO pod=_podDao.findById(podId);
    if (!Long.valueOf(pod.getDataCenterId()).equals(dcId)) {
      throw new InvalidParameterValueException("String_Node_Str" + podId + "String_Node_Str"+ dcId);
    }
  }
  if (clusterName == null || clusterName.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getHypervisor() == null || cmd.getHypervisor().isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Hypervisor.HypervisorType hypervisorType=Hypervisor.HypervisorType.getType(cmd.getHypervisor());
  if (hypervisorType == null) {
    s_logger.error("String_Node_Str" + cmd.getHypervisor() + "String_Node_Str");
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getHypervisor() + "String_Node_Str");
  }
  Cluster.ClusterType clusterType=null;
  if (cmd.getClusterType() != null && !cmd.getClusterType().isEmpty()) {
    clusterType=Cluster.ClusterType.valueOf(cmd.getClusterType());
  }
  if (clusterType == null) {
    clusterType=Cluster.ClusterType.CloudManaged;
  }
  Grouping.AllocationState allocationState=null;
  if (cmd.getAllocationState() != null && !cmd.getAllocationState().isEmpty()) {
    try {
      allocationState=Grouping.AllocationState.valueOf(cmd.getAllocationState());
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getAllocationState() + "String_Node_Str");
    }
  }
  if (allocationState == null) {
    allocationState=Grouping.AllocationState.Enabled;
  }
  Discoverer discoverer=getMatchingDiscover(hypervisorType);
  if (discoverer == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getHypervisor());
  }
  List<ClusterVO> result=new ArrayList<ClusterVO>();
  long clusterId=0;
  ClusterVO cluster=new ClusterVO(dcId,podId,clusterName);
  cluster.setHypervisorType(cmd.getHypervisor());
  cluster.setClusterType(clusterType);
  cluster.setAllocationState(allocationState);
  try {
    cluster=_clusterDao.persist(cluster);
  }
 catch (  Exception e) {
    throw new CloudRuntimeException("String_Node_Str" + clusterName + "String_Node_Str"+ podId+ "String_Node_Str"+ dcId,e);
  }
  clusterId=cluster.getId();
  result.add(cluster);
  if (clusterType == Cluster.ClusterType.CloudManaged) {
    return result;
  }
  Map<String,String> details=new HashMap<String,String>();
  details.put("String_Node_Str",url);
  details.put("String_Node_Str",username);
  details.put("String_Node_Str",password);
  _clusterDetailsDao.persist(cluster.getId(),details);
  boolean success=false;
  try {
    try {
      uri=new URI(UriUtils.encodeURIComponent(url));
      if (uri.getScheme() == null) {
        throw new InvalidParameterValueException("String_Node_Str" + url + "String_Node_Str");
      }
 else       if (uri.getScheme().equalsIgnoreCase("String_Node_Str")) {
        if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("String_Node_Str") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("String_Node_Str")) {
          throw new InvalidParameterValueException("String_Node_Str");
        }
      }
    }
 catch (    URISyntaxException e) {
      throw new InvalidParameterValueException(url + "String_Node_Str");
    }
    List<HostVO> hosts=new ArrayList<HostVO>();
    Map<? extends ServerResource,Map<String,String>> resources=null;
    try {
      resources=discoverer.find(dcId,podId,clusterId,uri,username,password);
    }
 catch (    Exception e) {
      s_logger.info("String_Node_Str" + discoverer.getName());
    }
    if (resources != null) {
      for (      Map.Entry<? extends ServerResource,Map<String,String>> entry : resources.entrySet()) {
        ServerResource resource=entry.getKey();
        if (hypervisorType == Hypervisor.HypervisorType.Hyperv) {
          break;
        }
        AgentAttache attache=simulateStart(resource,entry.getValue(),true,null,null);
        if (attache != null) {
          hosts.add(_hostDao.findById(attache.getId()));
        }
        discoverer.postDiscovery(hosts,_nodeId);
      }
      s_logger.info("String_Node_Str" + discoverer.getName());
      success=true;
      return result;
    }
    s_logger.warn("String_Node_Str" + url);
    throw new DiscoveryException("String_Node_Str");
  }
 catch (  Throwable e) {
    s_logger.error("String_Node_Str",e);
    throw new DiscoveryException("String_Node_Str");
  }
 finally {
    if (!success) {
      _clusterDetailsDao.deleteDetails(clusterId);
      _clusterDao.remove(clusterId);
    }
  }
}