Program: 28321
Project: 28321
Buggy source:
public ApplicationInfo deploy(Collection<? extends Sniffer> sniffers,final ExtendedDeploymentContext context){
  long operationStartTime=Calendar.getInstance().getTimeInMillis();
  events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_START,context),false);
  final ActionReport report=context.getActionReport();
  final DeployCommandParameters commandParams=context.getCommandParameters(DeployCommandParameters.class);
  final String appName=commandParams.name();
  if (commandParams.origin == OpsParams.Origin.deploy && appRegistry.get(appName) != null) {
    report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",appName));
    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    return null;
  }
  if (commandParams.virtualservers == null) {
    commandParams.virtualservers=DeploymentUtils.getVirtualServers(commandParams.target,env,domain);
  }
  if (commandParams.enabled == null) {
    commandParams.enabled=Boolean.TRUE;
  }
  if (commandParams.altdd != null) {
    context.getSource().addArchiveMetaData(DeploymentProperties.ALT_DD,commandParams.altdd);
  }
  if (commandParams.runtimealtdd != null) {
    context.getSource().addArchiveMetaData(DeploymentProperties.RUNTIME_ALT_DD,commandParams.runtimealtdd);
  }
  ProgressTracker tracker=new ProgressTracker(){
    @Override public void actOn(    Logger logger){
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.stop(context);
        }
 catch (        Exception e) {
        }
      }
      try {
        PreDestroy.class.cast(context).preDestroy();
      }
 catch (      Exception e) {
      }
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.unload(context);
        }
 catch (        Exception e) {
        }
      }
      try {
        ApplicationInfo appInfo=appRegistry.get(appName);
        if (appInfo != null) {
          events.send(new Event<ApplicationInfo>(Deployment.APPLICATION_DISABLED,appInfo));
        }
      }
 catch (      Exception e) {
      }
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.clean(context);
        }
 catch (        Exception e) {
        }
      }
      if (!commandParams.keepfailedstubs) {
        try {
          context.clean();
        }
 catch (        Exception e) {
        }
      }
      appRegistry.remove(appName);
    }
  }
;
  context.addTransientAppMetaData(ExtendedDeploymentContext.TRACKER,tracker);
  context.setPhase(DeploymentContextImpl.Phase.PREPARE);
  ApplicationInfo appInfo=null;
  try {
    ArchiveHandler handler=context.getArchiveHandler();
    if (handler == null) {
      handler=getArchiveHandler(context.getSource(),commandParams.type);
      context.setArchiveHandler(handler);
    }
    if (handler == null) {
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",context.getSourceDir()));
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      return null;
    }
    DeploymentTracing tracing=context.getModuleMetaData(DeploymentTracing.class);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED);
    }
    getDeployableTypes(context);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.PARSING_DONE);
    }
    if (sniffers != null && logger.isLoggable(Level.FINE)) {
      for (      Sniffer sniffer : sniffers) {
        logger.fine("String_Node_Str" + sniffer.getModuleType());
      }
    }
    sniffers=getSniffers(handler,sniffers,context);
    ClassLoaderHierarchy clh=habitat.getService(ClassLoaderHierarchy.class);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_HIERARCHY);
    }
    context.createDeploymentClassLoader(clh,handler);
    events.send(new Event<DeploymentContext>(Deployment.AFTER_DEPLOYMENT_CLASSLOADER_CREATION,context),false);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
    }
    final ClassLoader cloader=context.getClassLoader();
    final ClassLoader currentCL=Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(cloader);
      List<EngineInfo> sortedEngineInfos=setupContainerInfos(handler,sniffers,context);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.CONTAINERS_SETUP_DONE);
      }
      if (logger.isLoggable(Level.FINE)) {
        for (        EngineInfo info : sortedEngineInfos) {
          logger.fine("String_Node_Str" + info.getSniffer().getModuleType());
        }
      }
      if (sortedEngineInfos == null || sortedEngineInfos.isEmpty()) {
        report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",context.getSource().getName()));
        tracker.actOn(logger);
        return null;
      }
      ApplicationInfo tempAppInfo=new ApplicationInfo(events,context.getSource(),appName);
      for (      Object m : context.getModuleMetadata()) {
        tempAppInfo.addMetaData(m);
      }
      tempAppInfo.setIsJavaEEApp(sortedEngineInfos);
      context.getSource().setExtraData(Boolean.class,tempAppInfo.isJavaEEApp());
      appRegistry.add(appName,tempAppInfo);
      try {
        notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.PREPARE,context);
      }
 catch (      Throwable interceptorException) {
        report.failure(logger,"String_Node_Str",null);
        report.setFailureCause(interceptorException);
        logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,interceptorException);
        tracker.actOn(logger);
        return null;
      }
      events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_BEFORE_CLASSLOADER_CREATION,context),false);
      context.createApplicationClassLoader(clh,handler);
      events.send(new Event<DeploymentContext>(Deployment.AFTER_APPLICATION_CLASSLOADER_CREATION,context),false);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
      }
      ModuleInfo moduleInfo=null;
      try {
        moduleInfo=prepareModule(sortedEngineInfos,appName,context,tracker);
        final ClientJarWriter cjw=new ClientJarWriter(context);
        cjw.run();
      }
 catch (      Throwable prepareException) {
        prepareException.printStackTrace();
        report.failure(logger,"String_Node_Str",null);
        report.setFailureCause(prepareException);
        logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,prepareException);
        tracker.actOn(logger);
        return null;
      }
      appInfo=context.getModuleMetaData(ApplicationInfo.class);
      if (appInfo == null) {
        appInfo=new ApplicationInfo(events,context.getSource(),appName);
        appInfo.addModule(moduleInfo);
        for (        Object m : context.getModuleMetadata()) {
          moduleInfo.addMetaData(m);
          appInfo.addMetaData(m);
        }
      }
 else {
        for (        EngineRef ref : moduleInfo.getEngineRefs()) {
          appInfo.add(ref);
        }
      }
      appRegistry.remove(appName);
      appInfo.setIsJavaEEApp(sortedEngineInfos);
      appRegistry.add(appName,appInfo);
      notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.PREPARE,context);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.PREPARED);
      }
      context.setPhase(DeploymentContextImpl.Phase.PREPARED);
      Thread.currentThread().setContextClassLoader(context.getClassLoader());
      appInfo.setAppClassLoader(context.getClassLoader());
      events.send(new Event<DeploymentContext>(Deployment.APPLICATION_PREPARED,context),false);
      if (loadOnCurrentInstance(context)) {
        appInfo.setLibraries(commandParams.libraries());
        try {
          notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.LOAD,context);
          appInfo.load(context,tracker);
          notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.LOAD,context);
          notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.START,context);
          appInfo.start(context,tracker);
          notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.START,context);
        }
 catch (        Throwable loadException) {
          logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,loadException);
          report.failure(logger,"String_Node_Str",null);
          report.setFailureCause(loadException);
          tracker.actOn(logger);
          return null;
        }
      }
      return appInfo;
    }
  finally {
      context.postDeployClean(false);
      Thread.currentThread().setContextClassLoader(currentCL);
    }
  }
 catch (  Throwable e) {
    report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",appName),null);
    report.setFailureCause(e);
    logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,e);
    tracker.actOn(logger);
    return null;
  }
 finally {
    if (report.getActionExitCode() == ActionReport.ExitCode.SUCCESS) {
      events.send(new Event<ApplicationInfo>(Deployment.DEPLOYMENT_SUCCESS,appInfo));
      long operationTime=Calendar.getInstance().getTimeInMillis() - operationStartTime;
      if (appInfo != null) {
        deploymentLifecycleProbeProvider.applicationDeployedEvent(appName,getApplicationType(appInfo),String.valueOf(operationTime));
      }
    }
 else {
      events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_FAILURE,context));
    }
  }
}

Fixed version (ground truth):
public ApplicationInfo deploy(Collection<? extends Sniffer> sniffers,final ExtendedDeploymentContext context){
  long operationStartTime=Calendar.getInstance().getTimeInMillis();
  events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_START,context),false);
  final ActionReport report=context.getActionReport();
  final DeployCommandParameters commandParams=context.getCommandParameters(DeployCommandParameters.class);
  final String appName=commandParams.name();
  if (commandParams.origin == OpsParams.Origin.deploy && appRegistry.get(appName) != null) {
    report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",appName));
    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    return null;
  }
  if (commandParams.virtualservers == null) {
    commandParams.virtualservers=DeploymentUtils.getVirtualServers(commandParams.target,env,domain);
  }
  if (commandParams.enabled == null) {
    commandParams.enabled=Boolean.TRUE;
  }
  if (commandParams.altdd != null) {
    context.getSource().addArchiveMetaData(DeploymentProperties.ALT_DD,commandParams.altdd);
  }
  if (commandParams.runtimealtdd != null) {
    context.getSource().addArchiveMetaData(DeploymentProperties.RUNTIME_ALT_DD,commandParams.runtimealtdd);
  }
  ProgressTracker tracker=new ProgressTracker(){
    @Override public void actOn(    Logger logger){
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.stop(context);
        }
 catch (        Exception e) {
        }
      }
      try {
        PreDestroy.class.cast(context).preDestroy();
      }
 catch (      Exception e) {
      }
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.unload(context);
        }
 catch (        Exception e) {
        }
      }
      try {
        ApplicationInfo appInfo=appRegistry.get(appName);
        if (appInfo != null) {
          events.send(new Event<ApplicationInfo>(Deployment.APPLICATION_DISABLED,appInfo));
        }
      }
 catch (      Exception e) {
      }
      for (      EngineRef module : get("String_Node_Str",EngineRef.class)) {
        try {
          module.clean(context);
        }
 catch (        Exception e) {
        }
      }
      if (!commandParams.keepfailedstubs) {
        try {
          context.clean();
        }
 catch (        Exception e) {
        }
      }
      appRegistry.remove(appName);
    }
  }
;
  context.addTransientAppMetaData(ExtendedDeploymentContext.TRACKER,tracker);
  context.setPhase(DeploymentContextImpl.Phase.PREPARE);
  ApplicationInfo appInfo=null;
  try {
    ArchiveHandler handler=context.getArchiveHandler();
    if (handler == null) {
      handler=getArchiveHandler(context.getSource(),commandParams.type);
      context.setArchiveHandler(handler);
    }
    if (handler == null) {
      report.setMessage(localStrings.getLocalString("String_Node_Str","String_Node_Str",context.getSourceDir()));
      report.setActionExitCode(ActionReport.ExitCode.FAILURE);
      return null;
    }
    DeploymentTracing tracing=context.getModuleMetaData(DeploymentTracing.class);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED);
    }
    if (handler.requiresAnnotationScanning(context.getSource())) {
      getDeployableTypes(context);
    }
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.PARSING_DONE);
    }
    if (sniffers != null && logger.isLoggable(Level.FINE)) {
      for (      Sniffer sniffer : sniffers) {
        logger.fine("String_Node_Str" + sniffer.getModuleType());
      }
    }
    sniffers=getSniffers(handler,sniffers,context);
    ClassLoaderHierarchy clh=habitat.getService(ClassLoaderHierarchy.class);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_HIERARCHY);
    }
    context.createDeploymentClassLoader(clh,handler);
    events.send(new Event<DeploymentContext>(Deployment.AFTER_DEPLOYMENT_CLASSLOADER_CREATION,context),false);
    if (tracing != null) {
      tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
    }
    final ClassLoader cloader=context.getClassLoader();
    final ClassLoader currentCL=Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(cloader);
      List<EngineInfo> sortedEngineInfos=setupContainerInfos(handler,sniffers,context);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.CONTAINERS_SETUP_DONE);
      }
      if (logger.isLoggable(Level.FINE)) {
        for (        EngineInfo info : sortedEngineInfos) {
          logger.fine("String_Node_Str" + info.getSniffer().getModuleType());
        }
      }
      if (sortedEngineInfos == null || sortedEngineInfos.isEmpty()) {
        report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",context.getSource().getName()));
        tracker.actOn(logger);
        return null;
      }
      ApplicationInfo tempAppInfo=new ApplicationInfo(events,context.getSource(),appName);
      for (      Object m : context.getModuleMetadata()) {
        tempAppInfo.addMetaData(m);
      }
      tempAppInfo.setIsJavaEEApp(sortedEngineInfos);
      context.getSource().setExtraData(Boolean.class,tempAppInfo.isJavaEEApp());
      appRegistry.add(appName,tempAppInfo);
      try {
        notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.PREPARE,context);
      }
 catch (      Throwable interceptorException) {
        report.failure(logger,"String_Node_Str",null);
        report.setFailureCause(interceptorException);
        logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,interceptorException);
        tracker.actOn(logger);
        return null;
      }
      events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_BEFORE_CLASSLOADER_CREATION,context),false);
      context.createApplicationClassLoader(clh,handler);
      events.send(new Event<DeploymentContext>(Deployment.AFTER_APPLICATION_CLASSLOADER_CREATION,context),false);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
      }
      ModuleInfo moduleInfo=null;
      try {
        moduleInfo=prepareModule(sortedEngineInfos,appName,context,tracker);
        final ClientJarWriter cjw=new ClientJarWriter(context);
        cjw.run();
      }
 catch (      Throwable prepareException) {
        prepareException.printStackTrace();
        report.failure(logger,"String_Node_Str",null);
        report.setFailureCause(prepareException);
        logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,prepareException);
        tracker.actOn(logger);
        return null;
      }
      appInfo=context.getModuleMetaData(ApplicationInfo.class);
      if (appInfo == null) {
        appInfo=new ApplicationInfo(events,context.getSource(),appName);
        appInfo.addModule(moduleInfo);
        for (        Object m : context.getModuleMetadata()) {
          moduleInfo.addMetaData(m);
          appInfo.addMetaData(m);
        }
      }
 else {
        for (        EngineRef ref : moduleInfo.getEngineRefs()) {
          appInfo.add(ref);
        }
      }
      appRegistry.remove(appName);
      appInfo.setIsJavaEEApp(sortedEngineInfos);
      appRegistry.add(appName,appInfo);
      notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.PREPARE,context);
      if (tracing != null) {
        tracing.addMark(DeploymentTracing.Mark.PREPARED);
      }
      context.setPhase(DeploymentContextImpl.Phase.PREPARED);
      Thread.currentThread().setContextClassLoader(context.getClassLoader());
      appInfo.setAppClassLoader(context.getClassLoader());
      events.send(new Event<DeploymentContext>(Deployment.APPLICATION_PREPARED,context),false);
      if (loadOnCurrentInstance(context)) {
        appInfo.setLibraries(commandParams.libraries());
        try {
          notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.LOAD,context);
          appInfo.load(context,tracker);
          notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.LOAD,context);
          notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.START,context);
          appInfo.start(context,tracker);
          notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.START,context);
        }
 catch (        Throwable loadException) {
          logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,loadException);
          report.failure(logger,"String_Node_Str",null);
          report.setFailureCause(loadException);
          tracker.actOn(logger);
          return null;
        }
      }
      return appInfo;
    }
  finally {
      context.postDeployClean(false);
      Thread.currentThread().setContextClassLoader(currentCL);
    }
  }
 catch (  Throwable e) {
    report.failure(logger,localStrings.getLocalString("String_Node_Str","String_Node_Str",appName),null);
    report.setFailureCause(e);
    logger.log(Level.SEVERE,KernelLoggerInfo.lifecycleException,e);
    tracker.actOn(logger);
    return null;
  }
 finally {
    if (report.getActionExitCode() == ActionReport.ExitCode.SUCCESS) {
      events.send(new Event<ApplicationInfo>(Deployment.DEPLOYMENT_SUCCESS,appInfo));
      long operationTime=Calendar.getInstance().getTimeInMillis() - operationStartTime;
      if (appInfo != null) {
        deploymentLifecycleProbeProvider.applicationDeployedEvent(appName,getApplicationType(appInfo),String.valueOf(operationTime));
      }
    }
 else {
      events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_FAILURE,context));
    }
  }
}