<commit_before>package org.seekay.contract.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.seekay.contract.common.Http;
import org.seekay.contract.configuration.GitConfigurationSource;
import org.seekay.contract.configuration.LocalConfigurationSource;
import org.seekay.contract.model.builder.ContractOperator;
import org.seekay.contract.model.domain.Contract;
import org.seekay.contract.model.tools.ContractTools;
import org.seekay.contract.server.servet.ConfigurationServlet;
import org.seekay.contract.server.servet.FilterServlet;
import org.seekay.contract.server.servet.HealthServlet;
import org.seekay.contract.server.servet.RequestHandlerServlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static org.apache.catalina.startup.Tomcat.addServlet;
import static org.seekay.contract.common.ApplicationContext.objectMapper;
import static org.seekay.contract.model.tools.SleepTools.sleep;

@Slf4j
public class ContractServer implements ContractOperator<ContractServer> {

  private Tomcat tomcat;

  private Integer port;

  private List<Contract> contracts;

  private ObjectMapper objectMapper;

  private ContractServer() {
    this.contracts = new ArrayList<>();
    this.objectMapper = objectMapper();
    this.tomcat = new Tomcat();
  }

  private ContractServer(List<Contract> contracts) {
    this.contracts = contracts;
    this.objectMapper = objectMapper();
    this.tomcat = new Tomcat();
  }

  /**
   * Creates a new ContractServer
   * @return
   */
  public static ContractServer newServer() {
    return new ContractServer();
  }

  /**
   * Sets the port on which the server is to start.
   * @param port
   * @return
   */
  public ContractServer onPort(Integer port) {
    this.port = port;
    return this;
  }

  /**
   * Sets the port on which the server is to start somewhere between 9000 and 9999
   * @return
   */
  public ContractServer onRandomPort() {
    this.port = new Random().nextInt(999) + 9000;
    return this;
  }

  /**
   * Starts the server and populates it with loaded contracts.
   * @return
   */
  public ContractServer startServer() {
    tomcat.setPort(this.port);
    tomcat.setBaseDir("target/tomcat/");
    configureServer();
    try {
      tomcat.start();
      waitForServerToStart();
    } catch (LifecycleException e) {
      throw new IllegalStateException("Problem occurred starting tomcat", e);
    }
    log.info("Tomcat server started on port " + this.port);
    pushContractsToServer();
    return this;
  }

  /**
   * Returns the path of the current server. Useful when using the randomPort() method.
   * @return
   */
  public String path() {
    return "http://localhost:" + port;
  }

  /**
   * Returns the config url of the server
   * @return
   */
  public String configurePath() {
    return "http://localhost:" + port + "/__configure";
  }

  /**
   * Removes all contracts from the server.
   */
  public void reset() {<commit_msg>Added ability to stop a server<commit_after>package org.seekay.contract.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.seekay.contract.common.Http;
import org.seekay.contract.configuration.GitConfigurationSource;
import org.seekay.contract.configuration.LocalConfigurationSource;
import org.seekay.contract.model.builder.ContractOperator;
import org.seekay.contract.model.domain.Contract;
import org.seekay.contract.model.tools.ContractTools;
import org.seekay.contract.server.servet.ConfigurationServlet;
import org.seekay.contract.server.servet.FilterServlet;
import org.seekay.contract.server.servet.HealthServlet;
import org.seekay.contract.server.servet.RequestHandlerServlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static org.apache.catalina.startup.Tomcat.addServlet;
import static org.seekay.contract.common.ApplicationContext.objectMapper;
import static org.seekay.contract.model.tools.SleepTools.sleep;

@Slf4j
public class ContractServer implements ContractOperator<ContractServer> {

  private Tomcat tomcat;

  private Integer port;

  private List<Contract> contracts;

  private ObjectMapper objectMapper;

  private ContractServer() {
    this.contracts = new ArrayList<>();
    this.objectMapper = objectMapper();
  }

  private ContractServer(List<Contract> contracts) {
    this.contracts = contracts;
    this.objectMapper = objectMapper();
  }

  /**
   * Creates a new ContractServer
   * @return
   */
  public static ContractServer newServer() {
    return new ContractServer();
  }

  /**
   * Sets the port on which the server is to start.
   * @param port
   * @return
   */
  public ContractServer onPort(Integer port) {
    this.port = port;
    return this;
  }

  /**
   * Sets the port on which the server is to start somewhere between 9000 and 9999
   * @return
   */
  public ContractServer onRandomPort() {
    this.port = new Random().nextInt(999) + 9000;
    return this;
  }

  /**
   * Starts the server and populates it with loaded contracts.
   * @return
   */
  public ContractServer startServer() {
    tomcat = new Tomcat();
    tomcat.setPort(this.port);
    tomcat.setBaseDir("target/tomcat/");
    configureServer();
    try {
      tomcat.start();
      waitForServerToStart();
    } catch (LifecycleException e) {
      throw new IllegalStateException("Problem occurred starting tomcat", e);
    }
    log.info("Tomcat server started on port " + this.port);
    pushContractsToServer();
    return this;
  }

  public void stopServer() {
    try {
      tomcat.stop();
      tomcat.destroy();
    } catch (LifecycleException e) {
      throw new IllegalStateException(e);
    }
  }

  /**
   * Returns the path of the current server. Useful when using the randomPort() method.
   * @return
   */
  public String path() {
    return "http://localhost:" + port;
  }

  /**
   * Returns the config url of the server
   * @return
   */
  public String configurePath() {
    return "http://localhost:" + port + "/__configure";
  }

  /**
   * Removes all contracts from the server.
   */
  public void reset() {
{'fraction_non_alphanumeric': 0.1076669661575322, 'fraction_numerical': 0.005390835579514825, 'mean_word_length': 3.7469793887704337, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6746217', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1927, 'n_words': 480}