Class TomcatServerComponent

java.lang.Object
org.apache.juneau.microservice.tomcat.TomcatServerComponent
All Implemented Interfaces:
MicroserviceListener

public class TomcatServerComponent extends Object implements MicroserviceListener
Drives the embedded Tomcat server lifecycle on behalf of the microservice via the MicroserviceListener fan-out.

Contributed by TomcatConfiguration as a @Bean. The microservice's Microservice.start() call invokes onStart(Microservice), which:

  1. Picks the first available port from the resolved ports (falling back to Tomcat/port config, then Tomcat-Port manifest, then 8000).
  2. Resolves the Tomcat instance — preferring an externally-supplied @Bean Tomcat, otherwise building one programmatically via the TomcatServerFactory against a writable base directory.
  3. Mounts servlets from Tomcat/servlets, Tomcat/servletMap, Tomcat/servletAttributes config sections, plus auto-mounts every @Bean Servlet whose runtime class is annotated with @Rest (at Rest.path(), or "/" when no path is set).
  4. Mounts every @Bean ServletFilterRegistration (and the @Bean AuthFilterChain, at /*, unless a registration already wraps it) in order.
  5. Materializes the connector via Tomcat.getConnector(), starts the server, and publishes the bound port via the availablePort / juneau.serverPort system properties.

onStop(Microservice) performs the inverse: stops and destroys the server gracefully in a worker thread, then deletes the auto-created temp base directory (if one was created).

External callers reach this component via Microservice.getInstance().getBeanStore().getBean(TomcatServerComponent.class).orElseThrow().

See Also:
Since:
10.0.0
  • Constructor Details

  • Method Details

    • onStart

      public void onStart(Microservice ms)
      Description copied from interface: MicroserviceListener
      Called at the beginning of the Microservice.start() call.
      Specified by:
      onStart in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
    • onStop

      public void onStop(Microservice ms)
      Description copied from interface: MicroserviceListener
      Called at the end of the Microservice.stop() call.
      Specified by:
      onStop in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
    • onConfigChange

      public void onConfigChange(Microservice ms, ConfigEvents events)
      Description copied from interface: MicroserviceListener
      Called if one or more changes occur in the config file.
      Specified by:
      onConfigChange in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
      events - The list of changes in the config file.
    • addServlet

      public TomcatServerComponent addServlet(jakarta.servlet.Servlet servlet, String pathSpec)
      Adds an arbitrary servlet to this Tomcat server.
      Parameters:
      servlet - The servlet instance.
      pathSpec - The context path of the servlet.
      Returns:
      This object.
    • addServlet

      public TomcatServerComponent addServlet(jakarta.servlet.Servlet servlet, String... pathSpecs)
      Adds an arbitrary servlet to this Tomcat server at one or more context paths.

      The servlet is registered async-capable (Tomcat's default is off), so that ServletRequest.startAsync() and asynchronous REST operations work, matching Jetty.

      Parameters:
      servlet - The servlet instance.
      pathSpecs - The context paths of the servlet.
      Returns:
      This object.
    • addFilter

      public TomcatServerComponent addFilter(jakarta.servlet.Filter filter, String urlPattern)
      Adds a servlet filter to this Tomcat server at the specified URL pattern.
      Parameters:
      filter - The filter instance.
      urlPattern - The URL pattern the filter applies to (e.g. "/*", "/api/*").
      Returns:
      This object.
    • addFilter

      public TomcatServerComponent addFilter(jakarta.servlet.Filter filter, String... urlPatterns)
      Adds a servlet filter to this Tomcat server at one or more URL patterns.
      Parameters:
      filter - The filter instance.
      urlPatterns - The URL patterns the filter applies to.
      Returns:
      This object.
    • addFilter

      Adds a servlet filter registration to this Tomcat server.

      Maps the filter on every URL pattern and dispatcher type of the registration with one filter map, and sets exactly the requested dispatcher types. Mapping order is call order, so call this outermost first.

      Applications contribute a ServletFilterRegistration @Bean instead; onStart(Microservice) applies the beans through this method in order. Calling it directly is only possible once the server object exists (during startup) and before the server runs.

      Parameters:
      registration - The registration. Cannot be null.
      Returns:
      This object.
      Throws:
      IllegalArgumentException - If the registration is null.
      IllegalStateException - If the server has already started.
    • addFilter

      public TomcatServerComponent addFilter(jakarta.servlet.Filter filter, Set<jakarta.servlet.DispatcherType> dispatcherTypes, String... urlPatterns)
      Adds a servlet filter to this Tomcat server on the given dispatcher types and URL patterns.

      Shorthand for addFilter(ServletFilterRegistration) with the default order and async support, and the same validation. The overloads without dispatcher types keep their REQUEST-only behavior.

      Parameters:
      filter - The filter instance. Cannot be null.
      dispatcherTypes - The dispatcher types. Cannot be empty.
      urlPatterns - The URL patterns. At least one is required; see ServletFilterRegistration.Builder#urlPatterns(String...).
      Returns:
      This object.
      Throws:
      IllegalArgumentException - If an argument is invalid.
      IllegalStateException - If the server has already started.
    • addServletAttribute

      Adds an attribute to the root servlet context of the Tomcat server.
      Parameters:
      name - The attribute name.
      value - The attribute value.
      Returns:
      This object.
    • getContextPath

      Returns the context path that this server is using.
      Returns:
      The context path.
    • getContext

      public org.apache.catalina.Context getContext()
      Returns the root Context of the Tomcat server, creating it if necessary.
      Returns:
      The root context.
    • getHostName

      public String getHostName()
      Returns the hostname of this microservice.
      Returns:
      The hostname.
    • getPort

      public int getPort()
      Returns the port that this server is listening on.
      Returns:
      The port.
    • getProtocol

      public String getProtocol()
      Returns whether this server is using "http" or "https".
      Returns:
      The protocol.
    • getServer

      public org.apache.catalina.startup.Tomcat getServer()
      Returns the underlying Tomcat server.
      Returns:
      The underlying Tomcat server.
      Throws:
      NullPointerException - If the server has not been created yet.
    • getURI

      public URI getURI()
      Returns the URI where this server is listening.
      Returns:
      The URI.