Class ServerConfig

java.lang.Object
com.ns.tcpframework.ServerConfig

public class ServerConfig extends Object
Configuration container for HTTP server settings and virtual host management.

This class encapsulates all configuration parameters needed to initialize and run an HTTP server instance. It includes server-wide settings (port, log level, environment) and virtual host configurations that enable hosting multiple websites on a single server.

Key configuration aspects:

  • Environment: Deployment environment identifier (development, staging, production)
  • Port: TCP port number on which the server listens for connections
  • Log Level: Minimum severity level for logging messages
  • Virtual Hosts: Name-based virtual hosting configuration for serving multiple domains
  • Default Host: Fallback virtual host for requests without a matching Host header

Virtual hosting allows a single server to serve multiple domain names, each with its own configuration, document root, and route mappings. The server selects the appropriate virtual host based on the HTTP Host header in incoming requests.

Configuration lifecycle:

  1. Configuration is loaded from YAML files by ConfigLoader
  2. ServerConfig instance is created with all parameters
  3. Server components are initialized using these settings
  4. Configuration remains immutable during server runtime (except via setters)

Usage example:

ServerConfig config = ConfigLoader.load("production");
int port = config.getPort();                    // 8080
String env = config.getEnvironment();           // "production"
VirtualHostConfig host = config.getDefaultHost();
Map<String, VirtualHostConfig> hosts = config.getHosts();

Thread-safety: This class is not inherently thread-safe. If configuration values need to be modified after server startup, external synchronization should be used. However, in typical usage, configuration is set once during initialization and read-only thereafter.

See Also:
  • Constructor Details

    • ServerConfig

      public ServerConfig(String environment, int port, Loglevel loglevel, VirtualHostConfig defaultHost, Map<String, VirtualHostConfig> hosts)
      Constructs a ServerConfig instance with all configuration parameters.

      This constructor is typically invoked by ConfigLoader after parsing configuration files. All parameters are required to ensure the server has complete configuration information.

      The constructor performs simple assignment without validation. Configuration validation should be performed by the loading mechanism or during server initialization.

      Parameters:
      environment - The deployment environment name (e.g., "development", "production"). Must not be null.
      port - The TCP port number for the server. Should be in the range 1-65535.
      loglevel - The minimum logging level for the server. Must not be null.
      defaultHost - The fallback virtual host configuration. Must not be null.
      hosts - Map of hostname to virtual host configurations. Must not be null, but may be empty (defaultHost will handle all requests).
  • Method Details

    • getEnvironment

      public String getEnvironment()
      Gets the deployment environment name.
      Returns:
      The environment name (e.g., "development", "staging", "production"). Never null.
    • setEnvironment

      public void setEnvironment(String environment)
      Sets the deployment environment name.

      Changing the environment at runtime is unusual and may require reloading configuration or restarting server components to take full effect.

      Parameters:
      environment - The new environment name. Should not be null.
    • getPort

      public int getPort()
      Gets the TCP port number on which the server listens.
      Returns:
      The port number (1-65535).
    • setPort

      public void setPort(int port)
      Sets the TCP port number for the server.

      Note: Changing the port at runtime requires restarting the server socket to bind to the new port. This setter is primarily useful for testing or pre-initialization configuration adjustments.

      Parameters:
      port - The new port number. Should be in the range 1-65535 and not already in use by another process.
    • getLoglevel

      public Loglevel getLoglevel()
      Gets the minimum log level for server logging.
      Returns:
      The configured log level. Never null.
    • setLoglevel

      public void setLoglevel(Loglevel loglevel)
      Sets the minimum log level for server logging.

      Changing the log level at runtime will immediately affect which messages are logged by the server. This can be useful for temporary debugging or reducing log verbosity in production.

      Parameters:
      loglevel - The new log level. Should not be null.
    • getHosts

      public Map<String, VirtualHostConfig> getHosts()
      Gets the map of all configured virtual hosts.

      The returned map contains hostname-to-configuration mappings for all virtual hosts. Modifying this map will affect server routing behavior, though this is typically not recommended during runtime.

      Returns:
      The map of hostname to virtual host configurations. Never null, but may be empty.
    • setHosts

      public void setHosts(Map<String, VirtualHostConfig> hosts)
      Sets the map of virtual host configurations.

      This replaces the entire virtual host configuration. Use with caution as it will affect all virtual host resolution for incoming requests.

      Parameters:
      hosts - The new map of hostname to virtual host configurations. Should not be null.
    • getDefaultHost

      public VirtualHostConfig getDefaultHost()
      Gets the default virtual host configuration used as a fallback.

      This host handles requests that don't match any configured virtual host or lack a Host header. It ensures all requests can be processed even if virtual host resolution fails.

      Returns:
      The default virtual host configuration. Never null.