Program: 29247
Project: 29247
Buggy source:
/** 
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
@Override public void execute(AdminCommandContext context){
  try {
    Collection<Interceptor> interceptors=habitat.getAllServices(Interceptor.class);
    if (interceptors != null) {
      for (      Interceptor interceptor : interceptors) {
        interceptor.intercept(this,initialContext);
      }
    }
    deployment.validateDeploymentTarget(target,name,isredeploy);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.TARGET_VALIDATED);
    }
    ActionReport.MessagePart part=report.getTopMessagePart();
    part.addProperty(DeploymentProperties.NAME,name);
    ApplicationConfigInfo savedAppConfig=new ApplicationConfigInfo(apps.getModule(Application.class,name));
    Properties undeployProps=handleRedeploy(name,report);
    if (enabled == null) {
      enabled=Boolean.TRUE;
    }
    if (!keepreposdir.booleanValue()) {
      final File reposDir=new File(env.getApplicationRepositoryPath(),VersioningUtils.getRepositoryName(name));
      if (reposDir.exists()) {
        for (int i=0; i < domain.getApplications().getApplications().size(); i++) {
          File existrepos=new File(new URI(domain.getApplications().getApplications().get(i).getLocation()));
          String appname=domain.getApplications().getApplications().get(i).getName();
          if (!appname.equals(name) && existrepos.getAbsoluteFile().equals(reposDir.getAbsoluteFile())) {
            report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",name,appname));
            return;
          }
 else {
            final FileArchive arch=DeploymentUtils.openAsFileArchive(reposDir,archiveFactory);
            arch.delete();
          }
        }
      }
    }
    if (!DeploymentUtils.isDomainTarget(target) && enabled) {
      try {
        versioningService.handleDisable(name,target,report);
      }
 catch (      VersioningSyntaxException e) {
        report.failure(logger,e.getMessage());
        return;
      }
    }
    File source=new File(archive.getURI().getSchemeSpecificPart());
    boolean isDirectoryDeployed=true;
    if (!source.isDirectory()) {
      isDirectoryDeployed=false;
      expansionDir=new File(domain.getApplicationRoot(),VersioningUtils.getRepositoryName(name));
      path=expansionDir;
    }
 else {
      String versionFromSameDir=versioningService.getVersionFromSameDir(source);
      if (!force && versionFromSameDir != null) {
        report.failure(logger,VersioningUtils.LOCALSTRINGS.getLocalString("String_Node_Str","String_Node_Str",source.getPath(),versionFromSameDir));
        return;
      }
    }
    deploymentContext=deployment.getBuilder(logger,this,report).source(archive).archiveHandler(archiveHandler).build(initialContext);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CONTEXT_CREATED);
      deploymentContext.addModuleMetaData(tracing);
    }
    if (undeployProps != null) {
      deploymentContext.getAppProps().putAll(undeployProps);
    }
    if (properties != null || property != null) {
      if (properties == null) {
        properties=new Properties();
      }
      if (property != null) {
        properties.putAll(property);
      }
    }
    if (properties != null) {
      deploymentContext.getAppProps().putAll(properties);
      validateDeploymentProperties(properties,deploymentContext);
    }
    deploymentContext.clean();
    Properties appProps=deploymentContext.getAppProps();
    String appLocation=DeploymentUtils.relativizeWithinDomainIfPossible(deploymentContext.getSource().getURI());
    appProps.setProperty(ServerTags.LOCATION,appLocation);
    appProps.setProperty(ServerTags.OBJECT_TYPE,"String_Node_Str");
    if (contextroot != null) {
      appProps.setProperty(ServerTags.CONTEXT_ROOT,contextroot);
    }
    appProps.setProperty(ServerTags.DIRECTORY_DEPLOYED,String.valueOf(isDirectoryDeployed));
    savedAppConfig.store(appProps);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS,previousTargets);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS,previousVirtualServers);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES,previousEnabledAttributes);
    Transaction t=deployment.prepareAppConfigChanges(deploymentContext);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.DEPLOY);
    }
    ApplicationInfo appInfo;
    appInfo=deployment.deploy(deploymentContext);
    final Artifacts downloadableArtifacts=DeploymentUtils.downloadableArtifacts(deploymentContext);
    final Artifacts generatedArtifacts=DeploymentUtils.generatedArtifacts(deploymentContext);
    if (report.getActionExitCode() == ActionReport.ExitCode.SUCCESS) {
      try {
        moveAppFilesToPermanentLocation(deploymentContext,logger);
        recordFileLocations(appProps);
        downloadableArtifacts.record(appProps);
        generatedArtifacts.record(appProps);
        deployment.registerAppInDomainXML(appInfo,deploymentContext,t);
        if (tracing != null) {
          tracing.addMark(DeploymentTracing.Mark.REGISTRATION);
        }
        if (retrieve != null) {
          retrieveArtifacts(context,downloadableArtifacts.getArtifacts(),retrieve,false);
        }
        suppInfo.setDeploymentContext(deploymentContext);
        ActionReport.ExitCode worstExitCode=ExitCode.SUCCESS;
        for (        ActionReport subReport : report.getSubActionsReport()) {
          ActionReport.ExitCode actionExitCode=subReport.getActionExitCode();
          if (actionExitCode.isWorse(worstExitCode)) {
            worstExitCode=actionExitCode;
          }
        }
        report.setActionExitCode(worstExitCode);
        report.setResultType(String.class,name);
      }
 catch (      Exception e) {
        deployment.undeploy(name,deploymentContext);
        deploymentContext.clean();
        throw e;
      }
    }
  }
 catch (  Throwable e) {
    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    report.setMessage(e.getMessage());
    report.setFailureCause(e);
  }
 finally {
    events.unregister(this);
    try {
      archive.close();
    }
 catch (    IOException e) {
      logger.log(Level.FINE,localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath()),e);
    }
    if (tracing != null) {
      tracing.print(System.out);
      TracingUtilities.dump("String_Node_Str",System.out);
    }
    if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
      report.setResultType(String.class,name);
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",name));
      logger.info(localStrings.getLocalString("String_Node_Str","String_Node_Str",name,timing.elapsed()));
    }
 else     if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
      String errorMessage=report.getMessage();
      Throwable cause=report.getFailureCause();
      if (cause != null) {
        String causeMessage=cause.getMessage();
        if (causeMessage != null && !causeMessage.equals(errorMessage)) {
          errorMessage=errorMessage + "String_Node_Str" + cause.getMessage();
        }
        logger.log(Level.SEVERE,errorMessage,cause.getCause());
      }
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",errorMessage));
      report.setFailureCause(null);
      if (expansionDir != null) {
        final FileArchive arch;
        try {
          arch=DeploymentUtils.openAsFileArchive(expansionDir,archiveFactory);
          arch.delete();
        }
 catch (        IOException ex) {
          final String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str",expansionDir.getAbsolutePath());
          report.failure(logger,msg,ex);
        }
      }
    }
    if (deploymentContext != null) {
      deploymentContext.postDeployClean(true);
    }
  }
}

Fixed version (ground truth):
/** 
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
@Override public void execute(AdminCommandContext context){
  try {
    Collection<Interceptor> interceptors=habitat.getAllServices(Interceptor.class);
    if (interceptors != null) {
      for (      Interceptor interceptor : interceptors) {
        interceptor.intercept(this,initialContext);
      }
    }
    deployment.validateDeploymentTarget(target,name,isredeploy);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.TARGET_VALIDATED);
    }
    ActionReport.MessagePart part=report.getTopMessagePart();
    part.addProperty(DeploymentProperties.NAME,name);
    ApplicationConfigInfo savedAppConfig=new ApplicationConfigInfo(apps.getModule(Application.class,name));
    Properties undeployProps=handleRedeploy(name,report);
    if (enabled == null) {
      enabled=Boolean.TRUE;
    }
    if (!keepreposdir.booleanValue()) {
      final File reposDir=new File(env.getApplicationRepositoryPath(),VersioningUtils.getRepositoryName(name));
      if (reposDir.exists()) {
        for (int i=0; i < domain.getApplications().getApplications().size(); i++) {
          File existrepos=new File(new URI(domain.getApplications().getApplications().get(i).getLocation()));
          String appname=domain.getApplications().getApplications().get(i).getName();
          if (!appname.equals(name) && existrepos.getAbsoluteFile().equals(reposDir.getAbsoluteFile())) {
            report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",name,appname));
            return;
          }
        }
        final FileArchive arch=DeploymentUtils.openAsFileArchive(reposDir,archiveFactory);
        arch.delete();
      }
    }
    if (!DeploymentUtils.isDomainTarget(target) && enabled) {
      try {
        versioningService.handleDisable(name,target,report);
      }
 catch (      VersioningSyntaxException e) {
        report.failure(logger,e.getMessage());
        return;
      }
    }
    File source=new File(archive.getURI().getSchemeSpecificPart());
    boolean isDirectoryDeployed=true;
    if (!source.isDirectory()) {
      isDirectoryDeployed=false;
      expansionDir=new File(domain.getApplicationRoot(),VersioningUtils.getRepositoryName(name));
      path=expansionDir;
    }
 else {
      String versionFromSameDir=versioningService.getVersionFromSameDir(source);
      if (!force && versionFromSameDir != null) {
        report.failure(logger,VersioningUtils.LOCALSTRINGS.getLocalString("String_Node_Str","String_Node_Str",source.getPath(),versionFromSameDir));
        return;
      }
    }
    deploymentContext=deployment.getBuilder(logger,this,report).source(archive).archiveHandler(archiveHandler).build(initialContext);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CONTEXT_CREATED);
      deploymentContext.addModuleMetaData(tracing);
    }
    if (undeployProps != null) {
      deploymentContext.getAppProps().putAll(undeployProps);
    }
    if (properties != null || property != null) {
      if (properties == null) {
        properties=new Properties();
      }
      if (property != null) {
        properties.putAll(property);
      }
    }
    if (properties != null) {
      deploymentContext.getAppProps().putAll(properties);
      validateDeploymentProperties(properties,deploymentContext);
    }
    deploymentContext.clean();
    Properties appProps=deploymentContext.getAppProps();
    String appLocation=DeploymentUtils.relativizeWithinDomainIfPossible(deploymentContext.getSource().getURI());
    appProps.setProperty(ServerTags.LOCATION,appLocation);
    appProps.setProperty(ServerTags.OBJECT_TYPE,"String_Node_Str");
    if (contextroot != null) {
      appProps.setProperty(ServerTags.CONTEXT_ROOT,contextroot);
    }
    appProps.setProperty(ServerTags.DIRECTORY_DEPLOYED,String.valueOf(isDirectoryDeployed));
    savedAppConfig.store(appProps);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS,previousTargets);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS,previousVirtualServers);
    deploymentContext.addTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES,previousEnabledAttributes);
    Transaction t=deployment.prepareAppConfigChanges(deploymentContext);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.DEPLOY);
    }
    ApplicationInfo appInfo;
    appInfo=deployment.deploy(deploymentContext);
    final Artifacts downloadableArtifacts=DeploymentUtils.downloadableArtifacts(deploymentContext);
    final Artifacts generatedArtifacts=DeploymentUtils.generatedArtifacts(deploymentContext);
    if (report.getActionExitCode() == ActionReport.ExitCode.SUCCESS) {
      try {
        moveAppFilesToPermanentLocation(deploymentContext,logger);
        recordFileLocations(appProps);
        downloadableArtifacts.record(appProps);
        generatedArtifacts.record(appProps);
        deployment.registerAppInDomainXML(appInfo,deploymentContext,t);
        if (tracing != null) {
          tracing.addMark(DeploymentTracing.Mark.REGISTRATION);
        }
        if (retrieve != null) {
          retrieveArtifacts(context,downloadableArtifacts.getArtifacts(),retrieve,false);
        }
        suppInfo.setDeploymentContext(deploymentContext);
        ActionReport.ExitCode worstExitCode=ExitCode.SUCCESS;
        for (        ActionReport subReport : report.getSubActionsReport()) {
          ActionReport.ExitCode actionExitCode=subReport.getActionExitCode();
          if (actionExitCode.isWorse(worstExitCode)) {
            worstExitCode=actionExitCode;
          }
        }
        report.setActionExitCode(worstExitCode);
        report.setResultType(String.class,name);
      }
 catch (      Exception e) {
        deployment.undeploy(name,deploymentContext);
        deploymentContext.clean();
        throw e;
      }
    }
  }
 catch (  Throwable e) {
    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    report.setMessage(e.getMessage());
    report.setFailureCause(e);
  }
 finally {
    events.unregister(this);
    try {
      archive.close();
    }
 catch (    IOException e) {
      logger.log(Level.FINE,localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath()),e);
    }
    if (tracing != null) {
      tracing.print(System.out);
      TracingUtilities.dump("String_Node_Str",System.out);
    }
    if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
      report.setResultType(String.class,name);
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",name));
      logger.info(localStrings.getLocalString("String_Node_Str","String_Node_Str",name,timing.elapsed()));
    }
 else     if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
      String errorMessage=report.getMessage();
      Throwable cause=report.getFailureCause();
      if (cause != null) {
        String causeMessage=cause.getMessage();
        if (causeMessage != null && !causeMessage.equals(errorMessage)) {
          errorMessage=errorMessage + "String_Node_Str" + cause.getMessage();
        }
        logger.log(Level.SEVERE,errorMessage,cause.getCause());
      }
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",errorMessage));
      report.setFailureCause(null);
      if (expansionDir != null) {
        final FileArchive arch;
        try {
          arch=DeploymentUtils.openAsFileArchive(expansionDir,archiveFactory);
          arch.delete();
        }
 catch (        IOException ex) {
          final String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str",expansionDir.getAbsolutePath());
          report.failure(logger,msg,ex);
        }
      }
    }
    if (deploymentContext != null) {
      deploymentContext.postDeployClean(true);
    }
  }
}