org.apache.hadoop.lib.server
Class Server

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

@InterfaceAudience.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).


Nested Class Summary
static class Server.Status
          Enumeration that defines the server status.
 
Field Summary
static String CONF_SERVICES
          Server property name that defines the service classes.
static String CONF_SERVICES_EXT
          Server property name that defines the service extension classes.
static String CONF_STARTUP_STATUS
          Server property name that defines server startup status.
static 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.
 
Constructor Summary
Server(String name, String homeDir)
          Creates a server instance.
Server(String name, String homeDir, org.apache.hadoop.conf.Configuration config)
          Creates a server instance.
Server(String name, String homeDir, String configDir, String logDir, String tempDir)
          Creates a server instance.
Server(String name, String homeDir, String configDir, String logDir, String tempDir, org.apache.hadoop.conf.Configuration config)
          Creates a server instance.
 
Method Summary
protected  void checkServiceDependencies(Service service)
          Checks if all service dependencies of a service are available.
 void destroy()
          Destroys the server.
protected  void destroyServices()
          Destroys the server services.
protected  void ensureOperational()
          Verifies the server is operational.
<T> T
get(Class<T> serviceKlass)
          Returns the Service associated to the specified interface.
 org.apache.hadoop.conf.Configuration getConfig()
          Returns the server configuration.
 String getConfigDir()
          Returns the server config dir.
 String getHomeDir()
          Returns the server home dir.
 String getLogDir()
          Returns the server log dir.
 String getName()
          Returns the name of the server.
 String getPrefix()
          Returns the server prefix for server configuration properties.
 String getPrefixedName(String name)
          Returns the prefixed name of a server property.
 Server.Status getStatus()
          Returns the current server status.
 String getTempDir()
          Returns the server temp dir.
 void init()
          Initializes the Server.
protected  void initConfig()
          Loads and inializes the server configuration.
protected  void initLog()
          Initializes Log4j logging.
protected  void initServices(List<Service> services)
          Initializes the list of services.
protected  List<Service> loadServices()
          Loads services defined in services and services.ext and de-dups them.
 void setService(Class<? extends Service> klass)
          Adds a service programmatically.
 void setStatus(Server.Status status)
          Sets a new server status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONF_SERVICES

public static final String CONF_SERVICES
Server property name that defines the service classes.

See Also:
Constant Field Values

CONF_SERVICES_EXT

public static final String CONF_SERVICES_EXT
Server property name that defines the service extension classes.

See Also:
Constant Field Values

CONF_STARTUP_STATUS

public static final String CONF_STARTUP_STATUS
Server property name that defines server startup status.

See Also:
Constant Field Values

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:
Constant Field Values
Constructor Detail

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 Detail

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:


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.


Copyright © 2013 Apache Software Foundation. All Rights Reserved.