Class Server
- Direct Known Subclasses:
ServerWebApp
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
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration that defines the server status. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringServer property name that defines the service classes.static final StringServer property name that defines the service extension classes.static final StringServer property name that defines server startup status.static final StringName of the log4j configuration file the Server will load from the classpath if the#SERVER#-log4j.propertiesis not defined in the server configuration directory. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckServiceDependencies(Service service) Checks if all service dependencies of a service are available.voiddestroy()Destroys the server.protected voidDestroys the server services.protected voidVerifies the server is operational.<T> TReturns theServiceassociated to the specified interface.org.apache.hadoop.conf.ConfigurationReturns the server configuration.Returns the server config dir.Returns the server home dir.Returns the server log dir.getName()Returns the name of the server.Returns the server prefix for server configuration properties.getPrefixedName(String name) Returns the prefixed name of a server property.Returns the current server status.Returns the server temp dir.voidinit()Initializes the Server.protected voidLoads and inializes the server configuration.protected voidinitLog()Initializes Log4j logging.protected voidinitServices(List<Service> services) Initializes the list of services.Loads services defined inservicesandservices.extand de-dups them.voidsetService(Class<? extends Service> klass) Adds a service programmatically.voidsetStatus(Server.Status status) Sets a new server status.
-
Field Details
-
CONF_SERVICES
Server property name that defines the service classes.- See Also:
-
CONF_SERVICES_EXT
Server property name that defines the service extension classes.- See Also:
-
CONF_STARTUP_STATUS
Server property name that defines server startup status.- See Also:
-
DEFAULT_LOG4J_PROPERTIES
Name of the log4j configuration file the Server will load from the classpath if the#SERVER#-log4j.propertiesis not defined in the server configuration directory.- See Also:
-
-
Constructor Details
-
Server
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
Creates a server instance.- Parameters:
name- server name.homeDir- server home directory.configDir- config directory.logDir- log directory.tempDir- temp directory.
-
Server
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
Returns the current server status.- Returns:
- the current server status.
-
setStatus
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
Initializes the Server.The initialization steps are:
- It verifies the service home and temp directories exist
- Loads the Server
#SERVER#-default.xmlconfiguration file from the classpath - Initializes log4j logging. If the
#SERVER#-log4j.propertiesfile does not exist in the config directory it loaddefault-log4j.propertiesfrom the classpath - Loads the
#SERVER#-site.xmlfile 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
Initializes Log4j logging.- Throws:
ServerException- thrown if Log4j could not be initialized.
-
initConfig
Loads and inializes the server configuration.- Throws:
ServerException- thrown if the configuration could not be loaded/initialized.
-
loadServices
Loads services defined inservicesandservices.extand de-dups them.- Returns:
- List of final services to initialize.
- Throws:
ServerException- throw if the services could not be loaded.
-
initServices
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
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
Returns the name of the server.- Returns:
- the server name.
-
getPrefix
Returns the server prefix for server configuration properties.By default it is the server name.
- Returns:
- the prefix for server configuration properties.
-
getPrefixedName
Returns the prefixed name of a server property.- Parameters:
name- of the property.- Returns:
- prefixed name of the property.
-
getHomeDir
Returns the server home dir.- Returns:
- the server home dir.
-
getConfigDir
Returns the server config dir.- Returns:
- the server config dir.
-
getLogDir
Returns the server log dir.- Returns:
- the server log dir.
-
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
Returns theServiceassociated to the specified interface.- Parameters:
serviceKlass- service interface.- Returns:
- the service implementation.
-
setService
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.
-