@InterfaceAudience.Private public class Server extends Object
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).
Modifier and Type | Class and Description |
---|---|
static class |
Server.Status
Enumeration that defines the server status.
|
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static final String CONF_SERVICES
public static final String CONF_SERVICES_EXT
public static final String CONF_STARTUP_STATUS
public static final String DEFAULT_LOG4J_PROPERTIES
#SERVER#-log4j.properties
is not defined
in the server configuration directory.public Server(String name, String homeDir)
The config, log and temp directories are all under the specified home directory.
name
- server name.homeDir
- server home directory.public Server(String name, String homeDir, String configDir, String logDir, String tempDir)
name
- server name.homeDir
- server home directory.configDir
- config directory.logDir
- log directory.tempDir
- temp directory.public Server(String name, String homeDir, org.apache.hadoop.conf.Configuration config)
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.
name
- server name.homeDir
- server home directory.config
- server configuration.public Server(String name, String homeDir, String configDir, String logDir, String tempDir, org.apache.hadoop.conf.Configuration config)
It uses the provided configuration instead loading it from the config dir.
name
- server name.homeDir
- server home directory.configDir
- config directory.logDir
- log directory.tempDir
- temp directory.config
- server configuration.public Server.Status getStatus()
public void setStatus(Server.Status status) throws ServerException
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.
status
- status to set.ServerException
- thrown if the service has been destroy because of
a failed notification to a service.protected void ensureOperational()
IllegalStateException
- thrown if the server is not operational.public void init() throws ServerException
The initialization steps are:
#SERVER#-default.xml
configuration file from the classpath#SERVER#-log4j.properties
file does not exist in the config
directory it load default-log4j.properties
from the classpath
#SERVER#-site.xml
file from the server config
directory and merges it with the default configuration.ServerException
- thrown if the server could not be initialized.protected void initLog() throws ServerException
ServerException
- thrown if Log4j could not be initialized.protected void initConfig() throws ServerException
ServerException
- thrown if the configuration could not be loaded/initialized.protected List<Service> loadServices() throws ServerException
services
and
services.ext
and de-dups them.ServerException
- throw if the services could not be loaded.protected void initServices(List<Service> services) throws ServerException
services
- services to initialized, it must be a de-dupped list of
services.ServerException
- thrown if the services could not be initialized.protected void checkServiceDependencies(Service service) throws ServerException
service
- service to check if all its dependencies are available.ServerException
- thrown if a service dependency is missing.protected void destroyServices()
public void destroy()
All services are destroyed in reverse order of initialization, then the Log4j framework is shutdown.
public String getName()
public String getPrefix()
By default it is the server name.
public String getPrefixedName(String name)
name
- of the property.public String getHomeDir()
public String getConfigDir()
public String getLogDir()
public String getTempDir()
public org.apache.hadoop.conf.Configuration getConfig()
public <T> T get(Class<T> serviceKlass)
Service
associated to the specified interface.serviceKlass
- service interface.public void setService(Class<? extends Service> klass) throws ServerException
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.
klass
- service class to add.ServerException
- throw if the service could not initialized/added
to the server.Copyright © 2008–2019 Apache Software Foundation. All rights reserved.