@BeforeClass(alwaysRun = true)
  public void setUpGlobal() throws Exception {

    port1 = findFreePort();
    embedded = new Embedded();
    String path = new File(".").getAbsolutePath();
    embedded.setCatalinaHome(path);

    Engine engine = embedded.createEngine();
    engine.setDefaultHost("127.0.0.1");

    Host host = embedded.createHost("127.0.0.1", path);
    engine.addChild(host);

    Context c = embedded.createContext("/", path);
    c.setReloadable(false);
    Wrapper w = c.createWrapper();
    w.addMapping("/*");
    w.setServletClass(org.apache.catalina.servlets.WebdavServlet.class.getName());
    w.addInitParameter("readonly", "false");
    w.addInitParameter("listings", "true");

    w.setLoadOnStartup(0);

    c.addChild(w);
    host.addChild(c);

    Connector connector =
        embedded.createConnector("127.0.0.1", port1, Http11NioProtocol.class.getName());
    connector.setContainer(host);
    embedded.addEngine(engine);
    embedded.addConnector(connector);
    embedded.start();
  }
 public void start() throws LifecycleException {
   if (!started) {
     savedProperties = System.getProperties();
     System.setProperties(new Properties(savedProperties));
     server.start();
     started = true;
   }
 }
  /**
   * Creates a single webapp configuration to be run in Tomcat.
   *
   * @param contextName the context name without leading slash, for example, "openmrs"
   * @param port the port at which to run tomcat.
   */
  public TomcatManager(String contextName, int port) {

    // create server
    container = new Embedded();
    container.setCatalinaHome("tomcat");

    // create context
    Context rootContext = container.createContext("/" + contextName, contextName);
    rootContext.setReloadable(true);

    // create host
    Host localHost = container.createHost("localhost", "webapps");
    localHost.addChild(rootContext);

    // create engine
    Engine engine = container.createEngine();
    engine.setName("Catalina");
    engine.addChild(localHost);
    engine.setDefaultHost(localHost.getName());
    container.addEngine(engine);

    // create http connector
    Connector httpConnector = container.createConnector((InetAddress) null, port, false);
    container.addConnector(httpConnector);
  }
 public void destroy() throws Exception {
   if (started) {
     server.stop();
     sessions.clear();
     started = false;
     System.setProperties(savedProperties);
     initServer();
   }
 }
 /** Stops the embedded Tomcat server. */
 public void stopContainer() {
   try {
     if (container != null) {
       container.stop();
       logger.info("Stopped container");
     }
   } catch (LifecycleException exception) {
     logger.warn("Cannot Stop Tomcat" + exception.getMessage());
   }
 }
  public EmbeddedTomcat(String contextPath, int port, String jvmRoute)
      throws MalformedURLException {
    this.contextPath = contextPath;
    this.port = port;

    // create server
    container = new Embedded();
    container.setCatalinaHome(catalinaHome);
    // Not really necessasry, but let's still do it...
    container.setRealm(new MemoryRealm());

    // create webapp loader
    WebappLoader loader = new WebappLoader(this.getClass().getClassLoader());
    if (classesDir != null) {
      loader.addRepository(new File(classesDir).toURI().toURL().toString());
    }

    rootContext = container.createContext("", webappDir);
    rootContext.setLoader(loader);
    rootContext.setReloadable(true);
    // Otherwise we get NPE when instantiating servlets
    rootContext.setIgnoreAnnotations(true);

    // create host
    Host localHost = container.createHost("127.0.0.1", new File("").getAbsolutePath());
    localHost.addChild(rootContext);

    localHost.setDeployOnStartup(true);

    // create engine
    engine = container.createEngine();
    engine.setName("localEngine");
    engine.addChild(localHost);
    engine.setDefaultHost(localHost.getName());
    engine.setJvmRoute(jvmRoute);
    engine.setService(new StandardService());
    container.addEngine(engine);

    // create http connector
    Connector httpConnector = container.createConnector((InetAddress) null, port, false);
    container.addConnector(httpConnector);
    container.setAwait(true);

    // Create the JVMRoute valve for session failover
    ValveBase valve = new JvmRouteBinderValve();
    ((StandardEngine) engine).addValve(valve);
  }
  private void initServer() throws Exception {
    // point catalina at the provided home directory
    server = new Embedded();
    server.setCatalinaHome(catalinaHome.getAbsolutePath());

    Engine engine = server.createEngine();
    engine.setName("embedded");
    server.addEngine(engine);

    host = server.createHost("localhost", new File(catalinaHome, "webapps").getAbsolutePath());
    engine.addChild(host);
    engine.setDefaultHost(host.getName());

    // bind to an available port
    httpConnector = new EmbeddedConnector();
    server.addConnector(httpConnector);

    // disable session persistence on restart
    sessions = new MemoryStore();
    sessionManager = new PersistentManager();
    sessionManager.setDistributable(false);
    sessionManager.setSaveOnRestart(false);
    sessionManager.setStore(sessions);

    Context root = server.createContext("", new File(catalinaHome, "conf").getAbsolutePath());
    root.setManager(sessionManager);
    host.addChild(root);
  }
  /** Starts the embedded Tomcat server. */
  public void startContainer() throws LifecycleException {
    // start server
    container.start();

    // add shutdown hook to stop server
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                stopContainer();
              }
            });
  }
  /** Stops the embedded Tomcat server. */
  public boolean stop() {

    boolean stopMySql = false;

    // stop tomcat.
    try {
      if (container != null) {
        container.stop();
        container = null;
        stopMySql = true;
      }
    } catch (LifecycleException exception) {
      System.out.println("Cannot Stop Tomcat" + exception.getMessage());
      return false;
    }

    // stop mysql.
    if (stopMySql) StandaloneUtil.stopMySqlServer();

    return true;
  }
 /** Starts the embedded Tomcat server. */
 public void run() throws LifecycleException, MalformedURLException {
   container.setAwait(true);
   container.start();
 }
 @AfterClass(alwaysRun = true)
 public void tearDownGlobal() throws InterruptedException, Exception {
   embedded.stop();
 }
 private Context newContext(String path, File docBase) {
   Context context = server.createContext(path, docBase.getAbsolutePath());
   context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));
   context.setManager(sessionManager);
   return context;
 }