Program: 30104
Project: 30104
Buggy source:
/** 
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
@Override public void execute(AdminCommandContext context){
  events.register(this);
  final DeployCommandSupplementalInfo suppInfo=new DeployCommandSupplementalInfo();
  context.getActionReport().setResultType(DeployCommandSupplementalInfo.class,suppInfo);
  try {
    DeploymentTracing timing=new DeploymentTracing();
    DeploymentTracing tracing=null;
    if (System.getProperty("String_Node_Str") != null) {
      tracing=new DeploymentTracing();
    }
    final ActionReport report=context.getActionReport();
    final Logger logger=context.getLogger();
    originalPathValue=path;
    if (!path.exists()) {
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath()));
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      return;
    }
    if (snifferManager.hasNoSniffers()) {
      String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str");
      report.failure(logger,msg);
      return;
    }
    ReadableArchive archive;
    try {
      archive=archiveFactory.openArchive(path,this);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.ARCHIVE_OPENED);
      }
    }
 catch (    IOException e) {
      final String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath());
      if (logReportedErrors) {
        report.failure(logger,msg,e);
      }
 else {
        report.setMessage(msg + path.getAbsolutePath() + e.toString());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      }
      return;
    }
    File expansionDir=null;
    ExtendedDeploymentContext deploymentContext=null;
    try {
      deployment.validateSpecifiedTarget(target);
      ArchiveHandler archiveHandler=deployment.getArchiveHandler(archive,type);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED);
      }
      if (archiveHandler == null) {
        report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getName()));
        return;
      }
      ExtendedDeploymentContext initialContext=new DeploymentContextImpl(report,logger,archive,this,env);
      if (tracing != null) {
        initialContext.addModuleMetaData(tracing);
        tracing.addMark(DeploymentTracing.Mark.INITIAL_CONTEXT_CREATED);
      }
      if (name == null) {
        name=archiveHandler.getDefaultApplicationName(archive,initialContext);
      }
 else {
        DeploymentUtils.validateApplicationName(name);
      }
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.APPNAME_DETERMINED);
      }
      boolean isUntagged=VersioningUtils.isUntagged(name);
      if (name != null && !isUntagged && type != null && type.equals("String_Node_Str")) {
        ActionReport.MessagePart msgPart=context.getActionReport().getTopMessagePart();
        msgPart.setChildrenType("String_Node_Str");
        ActionReport.MessagePart childPart=msgPart.addChild();
        childPart.setMessage(VersioningUtils.LOCALSTRINGS.getLocalString("String_Node_Str","String_Node_Str"));
        name=VersioningUtils.getUntaggedName(name);
      }
      if (isUntagged) {
        String versionIdentifier=archiveHandler.getVersionIdentifier(archive);
        if (versionIdentifier != null && !versionIdentifier.isEmpty()) {
          StringBuilder sb=new StringBuilder(name).append(VersioningUtils.EXPRESSION_SEPARATOR).append(versionIdentifier);
          name=sb.toString();
        }
      }
      if (target == null) {
        target=deployment.getDefaultTarget(name,origin);
      }
      Collection<Interceptor> interceptors=habitat.getAllByContract("String_Node_Str");
      if (interceptors != null) {
        for (        Interceptor interceptor : interceptors) {
          interceptor.intercept(this,initialContext);
        }
      }
      boolean isRegistered=deployment.isRegistered(name);
      isredeploy=isRegistered && force;
      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()) {
          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(),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;
      if (type == null) {
        appInfo=deployment.deploy(deploymentContext);
      }
 else {
        appInfo=deployment.deploy(deployment.prepareSniffersForOSGiDeployment(type,deploymentContext),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);
          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;
        }
      }
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.REGISTRATION);
      }
      if (retrieve != null) {
        retrieveArtifacts(context,downloadableArtifacts.getArtifacts(),retrieve,false);
      }
    }
 catch (    Throwable e) {
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      report.setMessage(e.getMessage());
      report.setFailureCause(e);
    }
 finally {
      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);
      }
    }
  }
  finally {
    events.unregister(this);
  }
}

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){
  events.register(this);
  final DeployCommandSupplementalInfo suppInfo=new DeployCommandSupplementalInfo();
  context.getActionReport().setResultType(DeployCommandSupplementalInfo.class,suppInfo);
  try {
    DeploymentTracing timing=new DeploymentTracing();
    DeploymentTracing tracing=null;
    if (System.getProperty("String_Node_Str") != null) {
      tracing=new DeploymentTracing();
    }
    final ActionReport report=context.getActionReport();
    final Logger logger=context.getLogger();
    originalPathValue=path;
    if (!path.exists()) {
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath()));
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      return;
    }
    if (!path.canRead()) {
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath()));
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      return;
    }
    if (snifferManager.hasNoSniffers()) {
      String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str");
      report.failure(logger,msg);
      return;
    }
    ReadableArchive archive;
    try {
      archive=archiveFactory.openArchive(path,this);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.ARCHIVE_OPENED);
      }
    }
 catch (    IOException e) {
      final String msg=localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getAbsolutePath());
      if (logReportedErrors) {
        report.failure(logger,msg,e);
      }
 else {
        report.setMessage(msg + path.getAbsolutePath() + e.toString());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      }
      return;
    }
    File expansionDir=null;
    ExtendedDeploymentContext deploymentContext=null;
    try {
      deployment.validateSpecifiedTarget(target);
      ArchiveHandler archiveHandler=deployment.getArchiveHandler(archive,type);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED);
      }
      if (archiveHandler == null) {
        report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",path.getName()));
        return;
      }
      ExtendedDeploymentContext initialContext=new DeploymentContextImpl(report,logger,archive,this,env);
      if (tracing != null) {
        initialContext.addModuleMetaData(tracing);
        tracing.addMark(DeploymentTracing.Mark.INITIAL_CONTEXT_CREATED);
      }
      if (name == null) {
        name=archiveHandler.getDefaultApplicationName(archive,initialContext);
      }
 else {
        DeploymentUtils.validateApplicationName(name);
      }
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.APPNAME_DETERMINED);
      }
      boolean isUntagged=VersioningUtils.isUntagged(name);
      if (name != null && !isUntagged && type != null && type.equals("String_Node_Str")) {
        ActionReport.MessagePart msgPart=context.getActionReport().getTopMessagePart();
        msgPart.setChildrenType("String_Node_Str");
        ActionReport.MessagePart childPart=msgPart.addChild();
        childPart.setMessage(VersioningUtils.LOCALSTRINGS.getLocalString("String_Node_Str","String_Node_Str"));
        name=VersioningUtils.getUntaggedName(name);
      }
      if (isUntagged) {
        String versionIdentifier=archiveHandler.getVersionIdentifier(archive);
        if (versionIdentifier != null && !versionIdentifier.isEmpty()) {
          StringBuilder sb=new StringBuilder(name).append(VersioningUtils.EXPRESSION_SEPARATOR).append(versionIdentifier);
          name=sb.toString();
        }
      }
      if (target == null) {
        target=deployment.getDefaultTarget(name,origin);
      }
      Collection<Interceptor> interceptors=habitat.getAllByContract("String_Node_Str");
      if (interceptors != null) {
        for (        Interceptor interceptor : interceptors) {
          interceptor.intercept(this,initialContext);
        }
      }
      boolean isRegistered=deployment.isRegistered(name);
      isredeploy=isRegistered && force;
      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()) {
          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(),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;
      if (type == null) {
        appInfo=deployment.deploy(deploymentContext);
      }
 else {
        appInfo=deployment.deploy(deployment.prepareSniffersForOSGiDeployment(type,deploymentContext),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);
          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;
        }
      }
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.REGISTRATION);
      }
      if (retrieve != null) {
        retrieveArtifacts(context,downloadableArtifacts.getArtifacts(),retrieve,false);
      }
    }
 catch (    Throwable e) {
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      report.setMessage(e.getMessage());
      report.setFailureCause(e);
    }
 finally {
      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);
      }
    }
  }
  finally {
    events.unregister(this);
  }
}