Class SharedServerManager

java.lang.Object
org.apache.tika.pipes.core.SharedServerManager
All Implemented Interfaces:
Closeable, AutoCloseable, ServerManager

public class SharedServerManager extends Object implements ServerManager
Manages a single shared PipesServer process for multiple PipesClients.

This mode reduces memory overhead by running one JVM instead of N JVMs, at the cost of reduced isolation: one crash affects all in-flight requests.

Not recommended for production use. See Tika Pipes documentation for details.

The shared server creates a ServerSocket and accepts incoming connections from clients. ensureRunning() is synchronized to prevent multiple clients from attempting to restart the server simultaneously.

Security: The server port and a 32-byte auth token are passed to the child process via environment variables (not command-line args), so they are not visible in /proc/<pid>/cmdline. Each client connection must present the token before the server will accept it. This prevents CVE-style abuse from untrusted local processes. Note: if a malicious actor has same-uid access to your host and can read /proc/<pid>/environ, that is beyond Tika's security model.

See Also:
  • Field Details

    • SOCKET_CONNECT_TIMEOUT_MS

      public static final int SOCKET_CONNECT_TIMEOUT_MS
      See Also:
  • Constructor Details

    • SharedServerManager

      public SharedServerManager(PipesConfig pipesConfig, Path tikaConfigPath, int numConnections)
      Creates a SharedServerManager.
      Parameters:
      pipesConfig - the pipes configuration
      tikaConfigPath - path to the tika config file
      numConnections - number of concurrent connections the server should support
  • Method Details

    • getPort

      public int getPort()
      Returns the current server port, blocking if a restart is in progress. This ensures clients always see a consistent port after restart completes.
      Specified by:
      getPort in interface ServerManager
      Returns:
      the server port, or -1 if not started
    • getTempDirectory

      public Path getTempDirectory()
      Description copied from interface: ServerManager
      Returns the path to the temporary directory used by the server. May return null if the server hasn't been started yet.
      Specified by:
      getTempDirectory in interface ServerManager
      Returns:
      the temp directory path, or null if not started
    • isRunning

      public boolean isRunning()
      Description copied from interface: ServerManager
      Checks if the server process is currently running.
      Specified by:
      isRunning in interface ServerManager
      Returns:
      true if the server process is running
    • ensureRunning

      Ensures the shared server is running, starting it if necessary.

      This method is synchronized - only one client will start/restart the server while others wait. The restarting flag is set during restart so that getPort() blocks until the new server is ready.

      Specified by:
      ensureRunning in interface ServerManager
      Throws:
      IOException - if the server cannot be started
      ServerInitializationException - if the server fails to initialize
      InterruptedException - if interrupted while waiting for server startup
      TimeoutException - if the server doesn't start within the configured timeout
    • markServerForRestart

      public void markServerForRestart()
      Marks the server for restart due to a fatal error (OOM, timeout).

      This is called by clients when they receive OOM or TIMEOUT status. It signals that the server process is stopping (System.exit was called), even if isRunning() might still return true briefly.

      The next call to ensureRunning() will wait for the process to fully exit and then restart the server.

      Specified by:
      markServerForRestart in interface ServerManager
    • incrementFilesProcessed

      public void incrementFilesProcessed(long maxFilesPerProcess)
      Increments the count of files processed and marks for restart if limit reached.
      Specified by:
      incrementFilesProcessed in interface ServerManager
      Parameters:
      maxFilesPerProcess - the maximum files before restart (0 means unlimited)
    • needsRestart

      public boolean needsRestart()
      Checks if the server has been marked for restart.
      Specified by:
      needsRestart in interface ServerManager
      Returns:
      true if the server has been marked for restart
    • connect

      public Socket connect(int socketTimeoutMs) throws IOException, ServerInitializationException
      Description copied from interface: ServerManager
      Establishes a connection to the server and returns a connected Socket.

      The behavior differs by implementation:

      • Per-client mode: Accepts incoming connection from the dedicated server
      • Shared mode: Connects out to the shared server

      This method should be called after ServerManager.ensureRunning().

      Specified by:
      connect in interface ServerManager
      Parameters:
      socketTimeoutMs - the socket timeout in milliseconds
      Returns:
      a connected Socket ready for communication
      Throws:
      IOException - if connection fails
      ServerInitializationException - if the server died before connecting
    • shutdown

      public void shutdown() throws InterruptedException
      Description copied from interface: ServerManager
      Shuts down the server process and cleans up resources. After calling this method, ServerManager.ensureRunning() can be called to restart.
      Specified by:
      shutdown in interface ServerManager
      Throws:
      InterruptedException - if interrupted while waiting for shutdown
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException