<commit_before>package jetbrains.buildServer.deployer.agent.tomcat;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildProcessAdapter;
import jetbrains.buildServer.agent.BuildRunnerContext;
import org.apache.catalina.ant.DeployTask;
import org.jetbrains.annotations.NotNull;

import java.io.File;


class TomcatBuildProcessAdapter extends BuildProcessAdapter {

    private static final String MANAGER_APP_SUFFIX = "manager/html";
    private static final String HTTP_PREFIX = "http://";
    private final String target;
    private final String username;
    private final String password;
    private final BuildRunnerContext context;
    private final String sourcePath;

    private volatile boolean hasFinished;

    public TomcatBuildProcessAdapter(String target, String username, String password, BuildRunnerContext context, String sourcePath) {
        this.target = target;
        this.username = username;
        this.password = password;
        this.context = context;
        this.sourcePath = sourcePath;
        hasFinished = false;
    }

    @NotNull
    @Override
    public BuildFinishedStatus waitFor() throws RunBuildException {
        while (!isInterrupted() && !hasFinished) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RunBuildException(e);
            }
        }
        return hasFinished ? BuildFinishedStatus.FINISHED_SUCCESS :
                BuildFinishedStatus.INTERRUPTED;
    }

    @Override
    public void start() throws RunBuildException {

        try {
            final StringBuilder sb = new StringBuilder();
            if (!target.startsWith(HTTP_PREFIX)) {
                sb.append(HTTP_PREFIX);
            }
            sb.append(target);

            if (!target.endsWith(MANAGER_APP_SUFFIX)) { // path to manager/html is required
                if (!target.endsWith("/")) {
                    sb.append('/');
                }
                sb.append(MANAGER_APP_SUFFIX);
            }

            final String targetUrl = sb.toString();
            DeployTask deployTask = new DeployTask();
            deployTask.setUrl(targetUrl);
            deployTask.setUsername(username);
            deployTask.setPassword(password);

            final File deployableWar = new File(context.getWorkingDirectory(), sourcePath);
            deployTask.setWar(deployableWar.getCanonicalPath());

            final String fileNameWithExtension = deployableWar.getName();
            final String contextName = fileNameWithExtension.substring(0, fileNameWithExtension.length() - 4);

            deployTask.setPath("/" + contextName);   // leading / is required!
            deployTask.execute();
        } catch (Exception e) {
            throw new RunBuildException(e);
        } finally {
            hasFinished = true;<commit_msg>ability to re-deploy existing applications in Tomcat runner<commit_after>package jetbrains.buildServer.deployer.agent.tomcat;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildProcessAdapter;
import jetbrains.buildServer.agent.BuildRunnerContext;
import org.apache.catalina.ant.DeployTask;
import org.apache.catalina.ant.ListTask;
import org.apache.catalina.ant.UndeployTask;
import org.apache.tools.ant.Project;
import org.jetbrains.annotations.NotNull;

import java.io.File;


class TomcatBuildProcessAdapter extends BuildProcessAdapter {

    private static final String MANAGER_APP_SUFFIX = "manager";
    private static final String HTTP_PREFIX = "http://";
    private static final String CONTEXTS_LIST_PROPERTY = "temp.contexts.list";

    private final String myTarget;
    private final String myUsername;
    private final String myPassword;
    private final BuildRunnerContext myContext;
    private final String myWarPath;

    private volatile boolean hasFinished;

    public TomcatBuildProcessAdapter(String target, String username, String password, BuildRunnerContext context, String sourcePath) {
        myTarget = target;
        myUsername = username;
        myPassword = password;
        myContext = context;
        myWarPath = sourcePath;
        hasFinished = false;
    }

    @NotNull
    @Override
    public BuildFinishedStatus waitFor() throws RunBuildException {
        while (!isInterrupted() && !hasFinished) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RunBuildException(e);
            }
        }
        return hasFinished ? BuildFinishedStatus.FINISHED_SUCCESS :
                BuildFinishedStatus.INTERRUPTED;
    }

    @Override
    public void start() throws RunBuildException {

        try {
            final StringBuilder sb = new StringBuilder();
            if (!myTarget.startsWith(HTTP_PREFIX)) {
                sb.append(HTTP_PREFIX);
            }
            sb.append(myTarget);

            if (!myTarget.endsWith(MANAGER_APP_SUFFIX)) { // path to manager/html is required
                if (!myTarget.endsWith("/")) {
                    sb.append('/');
                }
                sb.append(MANAGER_APP_SUFFIX);
            }

            final File deployableWar = new File(myContext.getWorkingDirectory(), myWarPath);
            final String fileNameWithExtension = deployableWar.getName();
            final String contextName = fileNameWithExtension.substring(0, fileNameWithExtension.length() - 4);
            final String targetUrl = sb.toString();

            final Project tempProject = new Project();
            final ListTask list = new ListTask();
            list.setProject(tempProject);
            list.setUrl(targetUrl);
            list.setUsername(myUsername);
            list.setPassword(<PASSWORD>);
            list.setOutputproperty(CONTEXTS_LIST_PROPERTY);
            list.execute();

            final String contextsList = tempProject.getProperty(CONTEXTS_LIST_PROPERTY);

            if (contextsList.contains(contextName)) {
                final UndeployTask undeployTask = new UndeployTask();
                undeployTask.setUrl(targetUrl);
                undeployTask.setUsername(myUsername);
                undeployTask.setPassword(<PASSWORD>);
                undeployTask.setPath("/" + contextName);
                undeployTask.execute();
            }

            final DeployTask deployTask = new DeployTask();
            deployTask.setUrl(targetUrl);
            deployTask.setUsername(myUsername);
            deployTask.setPassword(<PASSWORD>);
            deployTask.setWar(deployableWar.getCanonicalPath());
            deployTask.setPath("/" + contextName);   // leading / is required!
            deployTask.execute();

        } catch (Exception e) {
            throw new RunBuildException(e);
        } finally {
            hasFinished = true;
{'fraction_non_alphanumeric': 0.08448694162979878, 'fraction_numerical': 0.0017125731411445698, 'mean_word_length': 2.4590325765054293, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686334', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1723, 'n_words': 394}