Class TomcatWebServer

java.lang.Object
com.bobocode.bring.web.server.TomcatWebServer
All Implemented Interfaces:
WebServer

public class TomcatWebServer extends Object implements WebServer
Implementation of the WebServer interface for the Apache Tomcat server. This class encapsulates the embedded Tomcat server and provides methods to start, stop, and retrieve information about the server.

The TomcatWebServer constructor takes a configured Tomcat instance and initializes the server. The server is started in a separate thread to trigger initialization listeners. Additionally, a blocking non-daemon thread is created to prevent immediate shutdown of the server.

The start method starts the Tomcat server, and the stop method stops the server. Both methods are synchronized to ensure thread safety during server state changes.

This class uses a monitor object for synchronization and a volatile boolean flag (started) to track the server's state. It also contains methods for checking whether connectors have started, getting a description of ports, and obtaining the context path.

Since:
1.0
Author:
Blyzhnytsia Team
See Also:
  • Field Details

    • tomcat

      private final org.apache.catalina.startup.Tomcat tomcat
    • monitor

      private final Object monitor
    • started

      private volatile boolean started
  • Constructor Details

    • TomcatWebServer

      public TomcatWebServer(org.apache.catalina.startup.Tomcat tomcat)
  • Method Details

    • initialize

      private void initialize()
    • startDaemonAwaitThread

      private void startDaemonAwaitThread()
    • stopSilently

      private void stopSilently()
    • destroySilently

      private void destroySilently()
    • start

      public void start()
      Description copied from interface: WebServer
      Starts the web server. Calling this method on an already started server has no effect.
      Specified by:
      start in interface WebServer
    • checkThatConnectorsHaveStarted

      private void checkThatConnectorsHaveStarted()
    • checkConnectorHasStarted

      private void checkConnectorHasStarted(org.apache.catalina.connector.Connector connector)
    • getPortsDescription

      private String getPortsDescription(boolean localPort)
    • getContextPath

      private String getContextPath()
    • stop

      public void stop()
      Description copied from interface: WebServer
      Stops the web server. Calling this method on an already stopped server has no effect.
      Specified by:
      stop in interface WebServer
    • stopTomcat

      private void stopTomcat() throws org.apache.catalina.LifecycleException
      Throws:
      org.apache.catalina.LifecycleException
    • getPort

      public int getPort()
      Description copied from interface: WebServer
      Return the port this server is listening on.
      Specified by:
      getPort in interface WebServer
      Returns:
      the port (or -1 if none)