ConfigType
- public abstract class AbstractModule<ConfigType extends ModuleConfig> extends Object implements IModule<ConfigType>
Class providing default implementation of common module API methods
Modifier and Type | Field and Description |
---|---|
protected ConfigType |
config |
protected IEventHandler |
eventHandler |
protected Throwable |
lastError |
protected org.slf4j.Logger |
logger |
protected ModuleSecurity |
securityHandler |
protected boolean |
startRequested |
protected ModuleEvent.ModuleState |
state |
protected Object |
stateLock |
protected String |
statusMsg |
Constructor and Description |
---|
AbstractModule() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canInit(boolean force) |
protected boolean |
canStart() |
protected boolean |
canStop() |
void |
clearError()
Clears last error
|
void |
clearStatus()
Clears the current status message
|
ConfigType |
getConfiguration()
Retrieves a copy of the module configuration
(i.e.
|
Throwable |
getCurrentError() |
ModuleEvent.ModuleState |
getCurrentState() |
String |
getLocalID()
Helper method to get the module's local ID
|
org.slf4j.Logger |
getLogger() |
String |
getName()
Helper method to get the module's name
|
String |
getStatusMessage() |
void |
init()
Initializes the module synchronously with the current configuration.
Implementations of this method must block until the module is successfully initialized or send an exception. Module lifecycle events may not be generated when calling this method directly. |
void |
init(ConfigType config)
Initializes the module synchronously with the specified configuration.
Implementations of this method must block until the module is successfully initialized or send an exception. This is equivalent to calling IModule.setConfiguration(ModuleConfig)
and then IModule.init() with no arguments.Module lifecycle events may not be generated when calling this method directly. |
boolean |
isInitialized()
Checks if module is initialized
|
boolean |
isStarted()
Checks if module is started
|
void |
loadState(IModuleStateManager loader)
Restores the state of this module
Implementations of this method must block until the module state is successfully loaded or send an exception. |
protected void |
notifyConnectionStatus(boolean connected,
String remoteServiceName)
Helper method to send and log connection/disconnection events
|
void |
registerListener(IEventListener listener)
Registers a listener to receive events generated by this module.
When this method is called, the current state of the module is also notified synchronously to guarantee that the listener always receives it. |
void |
reportError(String msg,
Throwable error)
Sets the module error state and sends corresponding event
|
void |
reportError(String msg,
Throwable error,
boolean logAsDebug)
Sets the module error state and sends corresponding event
|
void |
reportStatus(String msg)
Sets the module status message and sends corresponding event
|
void |
requestInit(boolean force)
Requests to initialize the module with the current configuration.
Implementations of this method block until the module is initialized or return immediately while they wait for the proper init conditions. When this method returns without error the module state is guaranteed to be ModuleEvent.ModuleState.INITIALIZING |
void |
requestStart()
Requests the module to start.
Implementations of this method may block until the module is started or return immediately while they wait for the proper start conditions. When this method returns without error the module state is guaranteed to be ModuleEvent.ModuleState.STARTING |
void |
requestStop()
Requests the module to stop.
Implementations of this method may block until the module is stopped or return immediately while they wait for the proper stop conditions. When this method returns without error the module state is guaranteed to be ModuleEvent.ModuleState.STOPPING |
void |
saveState(IModuleStateManager saver)
Saves the state of this module.
Implementations of this method must block until the module state is successfully saved or send an exception. |
void |
setConfiguration(ConfigType config)
Sets the module configuration
|
protected void |
setState(ModuleEvent.ModuleState newState)
Sets the module state and sends the appropriate event if it has changed
|
String |
toString() |
void |
unregisterListener(IEventListener listener)
Unregisters a listener and thus stop receiving events generayed by this module
|
void |
updateConfig(ConfigType config)
Updates the module's configuration dynamically.
The module must honor this new configuration unless an error is detected. |
boolean |
waitForState(ModuleEvent.ModuleState state,
long timeout)
Waits until the module reaches the specified state or times out.
This method will return immediately if the state has already been reached. |
protected org.slf4j.Logger logger
protected IEventHandler eventHandler
protected ConfigType extends ModuleConfig config
protected ModuleEvent.ModuleState state
protected ModuleSecurity securityHandler
protected final Object stateLock
protected boolean startRequested
protected Throwable lastError
protected String statusMsg
public String getName()
IModule
getName
in interface IModule<ConfigType extends ModuleConfig>
public String getLocalID()
IModule
getLocalID
in interface IModule<ConfigType extends ModuleConfig>
public ConfigType getConfiguration()
IModule
getConfiguration
in interface IModule<ConfigType extends ModuleConfig>
public void setConfiguration(ConfigType config)
IModule
setConfiguration
in interface IModule<ConfigType extends ModuleConfig>
public void updateConfig(ConfigType config) throws SensorHubException
IModule
updateConfig
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
public boolean isInitialized()
IModule
isInitialized
in interface IModule<ConfigType extends ModuleConfig>
public boolean isStarted()
IModule
isStarted
in interface IModule<ConfigType extends ModuleConfig>
public ModuleEvent.ModuleState getCurrentState()
getCurrentState
in interface IModule<ConfigType extends ModuleConfig>
protected void setState(ModuleEvent.ModuleState newState)
newState
- public boolean waitForState(ModuleEvent.ModuleState state, long timeout)
IModule
waitForState
in interface IModule<ConfigType extends ModuleConfig>
state
- state to wait fortimeout
- maximum time to wait in milliseconds or <= 0 to wait foreverpublic Throwable getCurrentError()
getCurrentError
in interface IModule<ConfigType extends ModuleConfig>
public void reportError(String msg, Throwable error)
msg
- error
- false to log only at debug levelpublic void reportError(String msg, Throwable error, boolean logAsDebug)
msg
- error
- logAsDebug
- set to true to log the exception only at debug level,
false to log at error levelpublic void clearError()
public String getStatusMessage()
getStatusMessage
in interface IModule<ConfigType extends ModuleConfig>
public void reportStatus(String msg)
msg
- public void clearStatus()
protected void notifyConnectionStatus(boolean connected, String remoteServiceName)
connected
- protected boolean canInit(boolean force) throws SensorHubException
SensorHubException
public void requestInit(boolean force) throws SensorHubException
IModule
ModuleEvent.ModuleState.INITIALIZING
requestInit
in interface IModule<ConfigType extends ModuleConfig>
force
- set to true to force a reinit, even if module was already initializedSensorHubException
- if module could not enter initialization phasepublic void init() throws SensorHubException
IModule
init
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
public void init(ConfigType config) throws SensorHubException
IModule
IModule.setConfiguration(ModuleConfig)
and then IModule.init()
with no arguments.init
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
protected boolean canStart() throws SensorHubException
SensorHubException
public void requestStart() throws SensorHubException
IModule
ModuleEvent.ModuleState.STARTING
requestStart
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
- if startup could not be initiatedprotected boolean canStop() throws SensorHubException
SensorHubException
public void requestStop() throws SensorHubException
IModule
ModuleEvent.ModuleState.STOPPING
requestStop
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
- if shutdown could not be initiatedpublic void saveState(IModuleStateManager saver) throws SensorHubException
IModule
saveState
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
public void loadState(IModuleStateManager loader) throws SensorHubException
IModule
loadState
in interface IModule<ConfigType extends ModuleConfig>
SensorHubException
public void registerListener(IEventListener listener)
IModule
registerListener
in interface IEventProducer
registerListener
in interface IModule<ConfigType extends ModuleConfig>
public void unregisterListener(IEventListener listener)
IModule
unregisterListener
in interface IEventProducer
unregisterListener
in interface IModule<ConfigType extends ModuleConfig>
public org.slf4j.Logger getLogger()
Copyright © 2016 Sensia Software LLC. All rights reserved.