Class AbstractEndpoint<S,U>

java.lang.Object
org.apache.tomcat.util.net.AbstractEndpoint<S,U>
Type Parameters:
S - The type used by the socket wrapper associated with this endpoint. Might be the same as U.
U - The type of the underlying socket used by this endpoint. Might be the same as S.
Direct Known Subclasses:
AbstractJsseEndpoint

public abstract class AbstractEndpoint<S,U> extends Object
Abstract endpoint implementation.
  • Field Details

    • sm

      protected static final StringManager sm
      String manager for this package.
    • running

      protected volatile boolean running
      Running state of the endpoint.
    • paused

      protected volatile boolean paused
      Will be set to true whenever the endpoint is paused.
    • internalExecutor

      protected volatile boolean internalExecutor
      Are we using an internal executor
    • socketProperties

      protected final SocketProperties socketProperties
      Socket properties
    • acceptor

      protected Acceptor<U> acceptor
      Thread used to accept new connections and pass them to worker threads.
    • processorCache

      protected SynchronizedStack<SocketProcessorBase<S>> processorCache
      Cache for SocketProcessor objects
    • connections

      protected Map<U, SocketWrapperBase<S>> connections
      Map holding all current connections keyed with the sockets.
    • sslHostConfigs

      protected ConcurrentMap<String, SSLHostConfig> sslHostConfigs
      Map of SSL host configurations keyed by host name.
    • acceptorThreadPriority

      protected int acceptorThreadPriority
      Priority of the acceptor threads.
    • threadPriority

      protected int threadPriority
      Priority of the worker threads.
    • negotiableProtocols

      protected final List<String> negotiableProtocols
      List of protocols that can be negotiated (e.g. ALPN).
    • attributes

      protected HashMap<String,Object> attributes
      Attributes provide a way for configuration to be passed to subcomponents without the ProtocolHandler being aware of the properties available on those subcomponents.
  • Constructor Details

    • AbstractEndpoint

      public AbstractEndpoint()
      Construct and return an endpoint.
  • Method Details

    • toTimeout

      public static long toTimeout(long timeout)
      Convert a timeout value to a positive timeout suitable for use with socket operations.
      Parameters:
      timeout - The timeout value in milliseconds. If less than or equal to 0, returns Long.MAX_VALUE.
      Returns:
      The timeout value, or Long.MAX_VALUE if the input is less than or equal to 0
    • getSocketProperties

      public SocketProperties getSocketProperties()
      Get the socket properties.
      Returns:
      The socket properties
    • getConnections

      public Set<SocketWrapperBase<S>> getConnections()
      Get a set with the current open connections.
      Returns:
      A set with the open socket wrappers
    • getStrictSni

      public boolean getStrictSni()
      Get the strict SNI check flag.
      Returns:
      True if strict SNI checking is enabled
    • setStrictSni

      public void setStrictSni(boolean strictSni)
      Set the strict SNI check flag.
      Parameters:
      strictSni - True to enable strict SNI checking
    • getDefaultSSLHostConfigName

      public String getDefaultSSLHostConfigName()
      Get the host name for the default SSL configuration for this endpoint - always in lower case.
      Returns:
      The host name for the default SSL configuration for this endpoint - always in lower case.
    • setDefaultSSLHostConfigName

      public void setDefaultSSLHostConfigName(String defaultSSLHostConfigName)
      Set the host name for the default SSL configuration.
      Parameters:
      defaultSSLHostConfigName - The default SSL host configuration name
    • addSslHostConfig

      public void addSslHostConfig(SSLHostConfig sslHostConfig) throws IllegalArgumentException
      Add the given SSL Host configuration.
      Parameters:
      sslHostConfig - The configuration to add
      Throws:
      IllegalArgumentException - If the host name is not valid or if a configuration has already been provided for that host
    • addSslHostConfig

      public void addSslHostConfig(SSLHostConfig sslHostConfig, boolean replace) throws IllegalArgumentException
      Add the given SSL Host configuration, optionally replacing the existing configuration for the given host.
      Parameters:
      sslHostConfig - The configuration to add
      replace - If true replacement of an existing configuration is permitted, otherwise any such attempted replacement will trigger an exception
      Throws:
      IllegalArgumentException - If the host name is not valid or if a configuration has already been provided for that host and replacement is not allowed
    • removeSslHostConfig

      public SSLHostConfig removeSslHostConfig(String hostName)
      Removes the SSL host configuration for the given host name, if such a configuration exists.
      Parameters:
      hostName - The host name associated with the SSL host configuration to remove
      Returns:
      The SSL host configuration that was removed, if any
    • reloadSslHostConfig

      public void reloadSslHostConfig(String hostName)
      Re-read the configuration files for the SSL host and replace the existing SSL configuration with the updated settings. Note this replacement will happen even if the settings remain unchanged.
      Parameters:
      hostName - The SSL host for which the configuration should be reloaded. This must match a current SSL host
    • reloadSslHostConfigs

      public void reloadSslHostConfigs()
      Re-read the configuration files for all SSL hosts and replace the existing SSL configuration with the updated settings. Note this replacement will happen even if the settings remain unchanged.
    • findSslHostConfigs

      public SSLHostConfig[] findSslHostConfigs()
      Find all SSL host configurations.
      Returns:
      An array of all SSL host configurations
    • createSSLContext

      protected abstract void createSSLContext(SSLHostConfig sslHostConfig) throws Exception
      Create the SSLContext for the given SSLHostConfig.
      Parameters:
      sslHostConfig - The SSLHostConfig for which the SSLContext should be created
      Throws:
      Exception - If the SSLContext cannot be created for the given SSLHostConfig
    • logCertificate

      protected void logCertificate(SSLHostConfigCertificate certificate)
      Log information about the given certificate.
      Parameters:
      certificate - The certificate to log information about
    • generateCertificateDebug

      protected String generateCertificateDebug(X509Certificate certificate)
      Generate debug information about a certificate.
      Parameters:
      certificate - The certificate to generate debug info for
      Returns:
      A string containing certificate debug information
    • destroySsl

      protected void destroySsl() throws Exception
      Destroy the SSL configuration.
      Throws:
      Exception - If an error occurs while destroying SSL
    • releaseSSLContext

      protected void releaseSSLContext(SSLHostConfig sslHostConfig)
      Release the SSLContext, if any, associated with the SSLHostConfig.
      Parameters:
      sslHostConfig - The SSLHostConfig for which the SSLContext should be released
    • getSSLHostConfig

      protected SSLHostConfig getSSLHostConfig(String sniHostName)
      Look up the SSLHostConfig for the given host name. Lookup order is:
      1. exact match
      2. wild card match
      3. default SSLHostConfig
      Parameters:
      sniHostName - Host name - must be in lower case
      Returns:
      The SSLHostConfig for the given host name.
    • checkSni

      public boolean checkSni(String sniHostName, String protocolHostName)
      Check if two host names share the same SSLHostConfig.
      Parameters:
      sniHostName - the host name from SNI, null if SNI is not in use
      protocolHostName - the host name from the protocol
      Returns:
      true if SNI is not checked, if the SNI host name matches the protocol host name, if both host names use the same SSLHostConfig configuration, if there is no SNI and the protocol host name uses the default SSLHostConfig configuration, and false otherwise
    • getUseSendfile

      public boolean getUseSendfile()
      Get the sendfile flag.
      Returns:
      True if sendfile is enabled
    • setUseSendfile

      public void setUseSendfile(boolean useSendfile)
      Set the sendfile flag.
      Parameters:
      useSendfile - True to enable sendfile
    • getExecutorTerminationTimeoutMillis

      public long getExecutorTerminationTimeoutMillis()
      Get the executor termination timeout.
      Returns:
      The executor termination timeout in milliseconds
    • setExecutorTerminationTimeoutMillis

      public void setExecutorTerminationTimeoutMillis(long executorTerminationTimeoutMillis)
      Set the executor termination timeout.
      Parameters:
      executorTerminationTimeoutMillis - The timeout in milliseconds
    • setAcceptorThreadPriority

      public void setAcceptorThreadPriority(int acceptorThreadPriority)
      Set the acceptor thread priority.
      Parameters:
      acceptorThreadPriority - The thread priority
    • getAcceptorThreadPriority

      public int getAcceptorThreadPriority()
      Get the acceptor thread priority.
      Returns:
      The acceptor thread priority
    • setMaxConnections

      public void setMaxConnections(int maxCon)
      Set the maximum number of connections.
      Parameters:
      maxCon - The maximum number of connections (-1 for unlimited)
    • getMaxConnections

      public int getMaxConnections()
      Get the maximum number of connections.
      Returns:
      The maximum number of connections
    • getConnectionCount

      public long getConnectionCount()
      Return the current count of connections handled by this endpoint, if the connections are counted (which happens when the maximum count of connections is limited), or -1 if they are not. This property is added here so that this value can be inspected through JMX. It is visible on "ThreadPool" MBean.

      The count is incremented by the Acceptor before it tries to accept a new connection. Until the limit is reached and thus the count cannot be incremented, this value is more by 1 (the count of acceptors) than the actual count of connections that are being served.

      Returns:
      The count
    • setExecutor

      public void setExecutor(Executor executor)
      Set the external executor.
      Parameters:
      executor - The external executor (null to use internal executor)
    • getExecutor

      public Executor getExecutor()
      Get the external executor.
      Returns:
      The external executor, or null if none is configured
    • setUseVirtualThreads

      public void setUseVirtualThreads(boolean useVirtualThreads)
      Set whether virtual threads should be used.
      Parameters:
      useVirtualThreads - True to use virtual threads
    • getUseVirtualThreads

      public boolean getUseVirtualThreads()
      Get whether virtual threads are enabled.
      Returns:
      True if virtual threads are enabled
    • setUtilityExecutor

      public void setUtilityExecutor(ScheduledExecutorService utilityExecutor)
      Set the utility executor.
      Parameters:
      utilityExecutor - The utility executor
    • getUtilityExecutor

      public ScheduledExecutorService getUtilityExecutor()
      Get the utility executor.
      Returns:
      The utility executor
    • getPort

      public int getPort()
      Get the server socket port.
      Returns:
      The server socket port
    • setPort

      public void setPort(int port)
      Set the server socket port.
      Parameters:
      port - The server socket port
    • getPortOffset

      public int getPortOffset()
      Get the port offset.
      Returns:
      The port offset
    • setPortOffset

      public void setPortOffset(int portOffset)
      Set the port offset.
      Parameters:
      portOffset - The port offset (must be >= 0)
    • getPortWithOffset

      public int getPortWithOffset()
      Get the port with offset applied.
      Returns:
      The port with offset, or the port if port <= 0
    • getLocalPort

      public final int getLocalPort()
      Get the local port the server socket is bound to.
      Returns:
      The local port or -1 if not bound
    • getAddress

      public InetAddress getAddress()
      Get the bind address.
      Returns:
      The bind address
    • setAddress

      public void setAddress(InetAddress address)
      Set the bind address.
      Parameters:
      address - The bind address
    • getLocalAddress

      protected abstract InetSocketAddress getLocalAddress() throws IOException
      Obtain the network address the server socket is bound to. This primarily exists to enable the correct address to be used when unlocking the server socket since it removes the guess-work involved if no address is specifically set.
      Returns:
      The network address that the server socket is listening on or null if the server socket is not currently bound.
      Throws:
      IOException - If there is a problem determining the currently bound socket
    • setAcceptCount

      public void setAcceptCount(int acceptCount)
      Set the accept count.
      Parameters:
      acceptCount - The accept count (must be > 0)
    • getAcceptCount

      public int getAcceptCount()
      Get the accept count.
      Returns:
      The accept count
    • getBindOnInit

      public boolean getBindOnInit()
      Get the bind on init flag.
      Returns:
      True if the endpoint binds on init, false if it binds on start
    • setBindOnInit

      public void setBindOnInit(boolean b)
      Set the bind on init flag.
      Parameters:
      b - True to bind on init, false to bind on start
    • getBindState

      protected AbstractEndpoint.BindState getBindState()
      Get the current bind state.
      Returns:
      The current bind state
    • getKeepAliveTimeout

      public int getKeepAliveTimeout()
      Get the keepalive timeout.
      Returns:
      The keepalive timeout in milliseconds
    • setKeepAliveTimeout

      public void setKeepAliveTimeout(int keepAliveTimeout)
      Set the keepalive timeout.
      Parameters:
      keepAliveTimeout - The keepalive timeout in milliseconds
    • getTcpNoDelay

      public boolean getTcpNoDelay()
      Socket TCP no delay.
      Returns:
      The current TCP no delay setting for sockets created by this endpoint
    • setTcpNoDelay

      public void setTcpNoDelay(boolean tcpNoDelay)
      Set the TCP no delay flag.
      Parameters:
      tcpNoDelay - True to enable TCP no delay
    • getConnectionLinger

      public int getConnectionLinger()
      Socket linger.
      Returns:
      The current socket linger time for sockets created by this endpoint
    • setConnectionLinger

      public void setConnectionLinger(int connectionLinger)
      Set the connection linger time.
      Parameters:
      connectionLinger - The socket linger time in seconds
    • getConnectionTimeout

      public int getConnectionTimeout()
      Socket timeout.
      Returns:
      The current socket timeout for sockets created by this endpoint
    • setConnectionTimeout

      public void setConnectionTimeout(int soTimeout)
      Set the connection timeout.
      Parameters:
      soTimeout - The socket timeout in milliseconds
    • isSSLEnabled

      public boolean isSSLEnabled()
      Check if SSL is enabled.
      Returns:
      True if SSL is enabled
    • setSSLEnabled

      public void setSSLEnabled(boolean SSLEnabled)
      Set the SSL enabled flag.
      Parameters:
      SSLEnabled - True to enable SSL
    • setMinSpareThreads

      public void setMinSpareThreads(int minSpareThreads)
      Set the minimum number of spare threads.
      Parameters:
      minSpareThreads - The minimum number of spare threads
    • getMinSpareThreads

      public int getMinSpareThreads()
      Get the minimum number of spare threads.
      Returns:
      The minimum number of spare threads
    • setMaxThreads

      public void setMaxThreads(int maxThreads)
      Set the maximum number of worker threads.
      Parameters:
      maxThreads - The maximum number of worker threads
    • getMaxThreads

      public int getMaxThreads()
      Get the maximum number of worker threads.
      Returns:
      The maximum number of worker threads, or -1 if no internal executor is used
    • setMaxQueueSize

      public void setMaxQueueSize(int maxQueueSize)
      Set the maximum task queue size.
      Parameters:
      maxQueueSize - The maximum queue size
    • getMaxQueueSize

      public int getMaxQueueSize()
      Get the maximum queue size.
      Returns:
      The maximum queue size
    • setThreadsMaxIdleTime

      public void setThreadsMaxIdleTime(int threadsMaxIdleTime)
      Set the maximum idle time for threads in the internal thread pool.
      Parameters:
      threadsMaxIdleTime - The maximum idle time in milliseconds
    • getThreadsMaxIdleTime

      public int getThreadsMaxIdleTime()
      Get the maximum idle time for threads in the internal thread pool.
      Returns:
      The maximum idle time in milliseconds, or -1 if no internal executor is used
    • setThreadPriority

      public void setThreadPriority(int threadPriority)
      Set the priority of the worker threads.
      Parameters:
      threadPriority - The thread priority
    • getThreadPriority

      public int getThreadPriority()
      Get the priority of the worker threads.
      Returns:
      The thread priority, or -1 if no internal executor is used
    • getMaxKeepAliveRequests

      public int getMaxKeepAliveRequests()
      Get the maximum number of keep alive requests.
      Returns:
      The maximum number of keep alive requests
    • setMaxKeepAliveRequests

      public void setMaxKeepAliveRequests(int maxKeepAliveRequests)
      Set the maximum number of keep alive requests.
      Parameters:
      maxKeepAliveRequests - The maximum number of keep alive requests
    • setName

      public void setName(String name)
      Set the name of the thread pool.
      Parameters:
      name - The name of the thread pool
    • getName

      public String getName()
      Get the name of the thread pool.
      Returns:
      The name of the thread pool
    • setDomain

      public void setDomain(String domain)
      Set the domain for JMX registration.
      Parameters:
      domain - The JMX domain
    • getDomain

      public String getDomain()
      Get the JMX domain.
      Returns:
      The JMX domain, or null if not set
    • setDaemon

      public void setDaemon(boolean b)
      Set whether the threads should be daemon threads.
      Parameters:
      b - True to use daemon threads
    • getDaemon

      public boolean getDaemon()
      Check if the threads are daemon threads.
      Returns:
      True if the threads are daemon threads
    • setUseAsyncIO

      public void setUseAsyncIO(boolean useAsyncIO)
      Set whether asynchronous IO is enabled.
      Parameters:
      useAsyncIO - True to enable asynchronous IO
    • getUseAsyncIO

      public boolean getUseAsyncIO()
      Check if asynchronous IO is enabled.
      Returns:
      True if asynchronous IO is enabled
    • getDeferAccept

      @Deprecated public boolean getDeferAccept()
      Deprecated.
      This code will be removed in Tomcat 11 onwards
      Always returns false.
      Returns:
      Always false
    • getId

      public String getId()
      The default behavior is to identify connectors uniquely with address and port. However, certain connectors are not using that and need some other identifier, which then can be used as a replacement.
      Returns:
      the id
    • addNegotiatedProtocol

      public void addNegotiatedProtocol(String negotiableProtocol)
      Add a protocol that can be negotiated (e.g. ALPN).
      Parameters:
      negotiableProtocol - The protocol to add
    • hasNegotiableProtocols

      public boolean hasNegotiableProtocols()
      Check if there are any negotiable protocols configured.
      Returns:
      True if there are negotiable protocols
    • setHandler

      public void setHandler(AbstractEndpoint.Handler<S> handler)
      Set the handler for this endpoint.
      Parameters:
      handler - The handler to set
    • getHandler

      public AbstractEndpoint.Handler<S> getHandler()
      Get the handler.
      Returns:
      The handler
    • setAttribute

      public void setAttribute(String name, Object value)
      Generic property setter called when a property for which a specific setter already exists within the ProtocolHandler needs to be made available to subcomponents. The specific setter will call this method to populate the attributes.
      Parameters:
      name - Name of property to set
      value - The value to set the property to
    • getAttribute

      public Object getAttribute(String key)
      Used by subcomponents to retrieve configuration information.
      Parameters:
      key - The name of the property for which the value should be retrieved
      Returns:
      The value of the specified property
    • setProperty

      public boolean setProperty(String name, String value)
      Set the specified property.
      Parameters:
      name - The name of the property
      value - The value of the property
      Returns:
      True if the property was set successfully
    • getProperty

      public String getProperty(String name)
      Get the value of the specified property.
      Parameters:
      name - The name of the property
      Returns:
      The value of the property, or null if not found
    • getCurrentThreadCount

      public int getCurrentThreadCount()
      Return the amount of threads that are managed by the pool.
      Returns:
      the amount of threads that are managed by the pool
    • getCurrentThreadsBusy

      public int getCurrentThreadsBusy()
      Return the amount of threads that are in use
      Returns:
      the amount of threads that are in use
    • isRunning

      public boolean isRunning()
      Check if the endpoint is running.
      Returns:
      True if the endpoint is running
    • isPaused

      public boolean isPaused()
      Check if the endpoint is paused.
      Returns:
      True if the endpoint is paused
    • createExecutor

      public void createExecutor()
      Create the internal executor.
    • shutdownExecutor

      public void shutdownExecutor()
      Shutdown the internal executor.
    • unlockAccept

      protected void unlockAccept()
      Unlock the server socket acceptor threads using bogus connections.
    • processSocket

      public boolean processSocket(SocketWrapperBase<S> socketWrapper, SocketEvent event, boolean dispatch)
      Process the given SocketWrapper with the given status. Used to trigger processing as if the Poller (for those endpoints that have one) selected the socket.
      Parameters:
      socketWrapper - The socket wrapper to process
      event - The socket event to be processed
      dispatch - Should the processing be performed on a new container thread
      Returns:
      if processing was triggered successfully
    • createSocketProcessor

      protected abstract SocketProcessorBase<S> createSocketProcessor(SocketWrapperBase<S> socketWrapper, SocketEvent event)
      Create a socket processor for the given socket wrapper.
      Parameters:
      socketWrapper - The socket wrapper to process
      event - The socket event
      Returns:
      The socket processor
    • bind

      public abstract void bind() throws Exception
      Bind the endpoint to its port and address.
      Throws:
      Exception - If an error occurs during binding
    • unbind

      public abstract void unbind() throws Exception
      Throws:
      Exception
    • startInternal

      public abstract void startInternal() throws Exception
      Start the endpoint internal components.
      Throws:
      Exception - If an error occurs during startup
    • stopInternal

      public abstract void stopInternal() throws Exception
      Stop the endpoint internal components.
      Throws:
      Exception - If an error occurs during shutdown
    • init

      public final void init() throws Exception
      Initialize the endpoint.
      Throws:
      Exception - If an error occurs during initialization
    • start

      public final void start() throws Exception
      Start the endpoint.
      Throws:
      Exception - If an error occurs during startup
    • startAcceptorThread

      protected void startAcceptorThread()
      Start the acceptor thread.
    • pause

      public void pause()
      Pause the endpoint, which will stop it accepting new connections and unlock the acceptor.
    • resume

      public void resume()
      Resume the endpoint, which will make it start accepting new connections again.
    • stop

      public final void stop() throws Exception
      Stop the endpoint.
      Throws:
      Exception - If an error occurs during shutdown
    • destroy

      public final void destroy() throws Exception
      Destroy the endpoint and release all resources.
      Throws:
      Exception - If an error occurs while destroying the endpoint
    • getLog

      protected abstract Log getLog()
      Get the logger for this endpoint.
      Returns:
      The logger
    • getLogCertificate

      protected Log getLogCertificate()
      Get the logger for certificate-related messages.
      Returns:
      The logger (defaults to getLog())
    • initializeConnectionLatch

      protected LimitLatch initializeConnectionLatch()
      Initialize the connection latch if it has not already been created.
      Returns:
      The connection latch, or null if connection counting is disabled
    • countUpOrAwaitConnection

      protected void countUpOrAwaitConnection() throws InterruptedException
      Count up or await a connection slot.
      Throws:
      InterruptedException - If thrown during thread interruption
    • countDownConnection

      protected long countDownConnection()
      Count down a connection.
      Returns:
      The number of connections remaining, or -1 if connection counting is disabled
    • closeServerSocketGraceful

      public final void closeServerSocketGraceful()
      Close the server socket (to prevent further connections) if the server socket was originally bound on start() (rather than on init()).
      See Also:
    • awaitConnectionsClose

      public final long awaitConnectionsClose(long waitMillis)
      Wait for the client connections to the server to close gracefully. The method will return when all of the client connections have closed or the method has been waiting for waitTimeMillis.
      Parameters:
      waitMillis - The maximum time to wait in milliseconds for the client connections to close.
      Returns:
      The wait time, if any remaining when the method returned
    • doCloseServerSocket

      protected abstract void doCloseServerSocket() throws IOException
      Actually close the server socket but don't perform any other clean-up.
      Throws:
      IOException - If an error occurs closing the socket
    • serverSocketAccept

      protected abstract U serverSocketAccept() throws Exception
      Accept a connection from the server socket.
      Returns:
      The accepted socket
      Throws:
      Exception - If an error occurs during accept
    • setSocketOptions

      protected abstract boolean setSocketOptions(U socket)
      Set the socket options for the given accepted socket.
      Parameters:
      socket - The accepted socket
      Returns:
      True if the socket options were set successfully
    • closeSocket

      protected void closeSocket(U socket)
      Close the socket when the connection has to be immediately closed when an error occurs while configuring the accepted socket or trying to dispatch it for processing. The wrapper associated with the socket will be used for the close.
      Parameters:
      socket - The newly accepted socket
    • destroySocket

      protected abstract void destroySocket(U socket)
      Close the socket. This is used when the connector is not in a state which allows processing the socket, or if there was an error which prevented the allocation of the socket wrapper.
      Parameters:
      socket - The newly accepted socket