Class Server

java.lang.Object
org.apache.hadoop.lib.server.Server
Direct Known Subclasses:
ServerWebApp

@Private public class Server extends Object
A Server class provides standard configuration, logging and Service lifecyle management.

A Server normally has a home directory, a configuration directory, a temp directory and logs directory.

The Server configuration is loaded from 2 overlapped files, #SERVER#-default.xml and #SERVER#-site.xml. The default file is loaded from the classpath, the site file is laoded from the configuration directory.

The Server collects all configuration properties prefixed with #SERVER#. The property names are then trimmed from the #SERVER# prefix.

The Server log configuration is loaded from the #SERVICE#-log4j.properties file in the configuration directory.

The lifecycle of server is defined in by Server.Status enum. When a server is create, its status is UNDEF, when being initialized it is BOOTING, once initialization is complete by default transitions to NORMAL. The #SERVER#.startup.status configuration property can be used to specify a different startup status (NORMAL, ADMIN or HALTED).

Services classes are defined in the #SERVER#.services and #SERVER#.services.ext properties. They are loaded in order (services first, then services.ext).

Before initializing the services, they are traversed and duplicate service interface are removed from the service list. The last service using a given interface wins (this enables a simple override mechanism).

After the services have been resoloved by interface de-duplication they are initialized in order. Once all services are initialized they are post-initialized (this enables late/conditional service bindings).

  • Field Details

    • CONF_SERVICES

      public static final String CONF_SERVICES
      Server property name that defines the service classes.
      See Also:
    • CONF_SERVICES_EXT

      public static final String CONF_SERVICES_EXT
      Server property name that defines the service extension classes.
      See Also:
    • CONF_STARTUP_STATUS

      public static final String CONF_STARTUP_STATUS
      Server property name that defines server startup status.
      See Also:
    • DEFAULT_LOG4J_PROPERTIES

      public static final String DEFAULT_LOG4J_PROPERTIES
      Name of the log4j configuration file the Server will load from the classpath if the #SERVER#-log4j.properties is not defined in the server configuration directory.
      See Also:
  • Constructor Details

    • Server

      public Server(String name, String homeDir)
      Creates a server instance.

      The config, log and temp directories are all under the specified home directory.

      Parameters:
      name - server name.
      homeDir - server home directory.
    • Server

      public Server(String name, String homeDir, String configDir, String logDir, String tempDir)
      Creates a server instance.
      Parameters:
      name - server name.
      homeDir - server home directory.
      configDir - config directory.
      logDir - log directory.
      tempDir - temp directory.
    • Server

      public Server(String name, String homeDir, org.apache.hadoop.conf.Configuration config)
      Creates a server instance.

      The config, log and temp directories are all under the specified home directory.

      It uses the provided configuration instead loading it from the config dir.

      Parameters:
      name - server name.
      homeDir - server home directory.
      config - server configuration.
    • Server

      public Server(String name, String homeDir, String configDir, String logDir, String tempDir, org.apache.hadoop.conf.Configuration config)
      Creates a server instance.

      It uses the provided configuration instead loading it from the config dir.

      Parameters:
      name - server name.
      homeDir - server home directory.
      configDir - config directory.
      logDir - log directory.
      tempDir - temp directory.
      config - server configuration.
  • Method Details

    • getStatus

      public Server.Status getStatus()
      Returns the current server status.
      Returns:
      the current server status.
    • setStatus

      public void setStatus(Server.Status status) throws ServerException
      Sets a new server status.

      The status must be settable.

      All services will be notified o the status change via the Service.serverStatusChange(Server.Status, Server.Status) method. If a service throws an exception during the notification, the server will be destroyed.

      Parameters:
      status - status to set.
      Throws:
      ServerException - thrown if the service has been destroy because of a failed notification to a service.
    • ensureOperational

      protected void ensureOperational()
      Verifies the server is operational.
      Throws:
      IllegalStateException - thrown if the server is not operational.
    • init

      public void init() throws ServerException
      Initializes the Server.

      The initialization steps are:

      • It verifies the service home and temp directories exist
      • Loads the Server #SERVER#-default.xml configuration file from the classpath
      • Initializes log4j logging. If the #SERVER#-log4j.properties file does not exist in the config directory it load default-log4j.properties from the classpath
      • Loads the #SERVER#-site.xml file from the server config directory and merges it with the default configuration.
      • Loads the services
      • Initializes the services
      • Post-initializes the services
      • Sets the server startup status
      Throws:
      ServerException - thrown if the server could not be initialized.
    • initLog

      protected void initLog() throws ServerException
      Initializes Log4j logging.
      Throws:
      ServerException - thrown if Log4j could not be initialized.
    • initConfig

      protected void initConfig() throws ServerException
      Loads and inializes the server configuration.
      Throws:
      ServerException - thrown if the configuration could not be loaded/initialized.
    • loadServices

      protected List<Service> loadServices() throws ServerException
      Loads services defined in services and services.ext and de-dups them.
      Returns:
      List of final services to initialize.
      Throws:
      ServerException - throw if the services could not be loaded.
    • initServices

      protected void initServices(List<Service> services) throws ServerException
      Initializes the list of services.
      Parameters:
      services - services to initialized, it must be a de-dupped list of services.
      Throws:
      ServerException - thrown if the services could not be initialized.
    • checkServiceDependencies

      protected void checkServiceDependencies(Service service) throws ServerException
      Checks if all service dependencies of a service are available.
      Parameters:
      service - service to check if all its dependencies are available.
      Throws:
      ServerException - thrown if a service dependency is missing.
    • destroyServices

      protected void destroyServices()
      Destroys the server services.
    • destroy

      public void destroy()
      Destroys the server.

      All services are destroyed in reverse order of initialization, then the Log4j framework is shutdown.

    • getName

      public String getName()
      Returns the name of the server.
      Returns:
      the server name.
    • getPrefix

      public String getPrefix()
      Returns the server prefix for server configuration properties.

      By default it is the server name.

      Returns:
      the prefix for server configuration properties.
    • getPrefixedName

      public String getPrefixedName(String name)
      Returns the prefixed name of a server property.
      Parameters:
      name - of the property.
      Returns:
      prefixed name of the property.
    • getHomeDir

      public String getHomeDir()
      Returns the server home dir.
      Returns:
      the server home dir.
    • getConfigDir

      public String getConfigDir()
      Returns the server config dir.
      Returns:
      the server config dir.
    • getLogDir

      public String getLogDir()
      Returns the server log dir.
      Returns:
      the server log dir.
    • getTempDir

      public String getTempDir()
      Returns the server temp dir.
      Returns:
      the server temp dir.
    • getConfig

      public org.apache.hadoop.conf.Configuration getConfig()
      Returns the server configuration.
      Returns:
      the server configuration.
    • get

      public <T> T get(Class<T> serviceKlass)
      Returns the Service associated to the specified interface.
      Parameters:
      serviceKlass - service interface.
      Returns:
      the service implementation.
    • setService

      public void setService(Class<? extends Service> klass) throws ServerException
      Adds a service programmatically.

      If a service with the same interface exists, it will be destroyed and removed before the given one is initialized and added.

      If an exception is thrown the server is destroyed.

      Parameters:
      klass - service class to add.
      Throws:
      ServerException - throw if the service could not initialized/added to the server.