public class ModuleRegistry extends Object implements IModuleManager<IModule<?>>, IEventProducer, IEventListener
This class is in charge of loading all configured modules on startup as well as dynamically loading/unloading modules on demand. It also keeps lists of all loaded and available modules.
TODO implement global event manager for all modules ? TODO return weak references to modules ?Modifier and Type | Field and Description |
---|---|
static String |
ID |
static long |
SHUTDOWN_TIMEOUT_MS |
Constructor and Description |
---|
ModuleRegistry(IModuleConfigRepository configRepos,
EventBus eventBus) |
Modifier and Type | Method and Description |
---|---|
ModuleConfig |
createModuleConfig(IModuleProvider provider)
Creates a new module config class using information from a module provider
|
void |
destroyModule(String moduleID)
Removes the module with the given id
|
Collection<ModuleConfig> |
getAvailableModules()
Gets the list of all modules handled by this manager
that are configured but not yet loaded (i.e.
|
Collection<IModuleProvider> |
getInstalledModuleTypes()
Retrieves list of all installed module types
|
Collection<IModuleProvider> |
getInstalledModuleTypes(Class<?> moduleClass)
Retrieves list of all installed module types that are sub-types
of the specified class
|
Collection<IModule<?>> |
getLoadedModules()
Gets the list of all modules handled by this manager
that are already loaded (i.e.
|
IModule<?> |
getModuleById(String moduleID)
Retrieves a module instance by its local ID
|
File |
getModuleDataFolder(String moduleID)
Retrieves the folder where the module data should be stored
|
WeakReference<? extends IModule<?>> |
getModuleRef(String moduleID) |
IModuleStateManager |
getStateManager(String moduleID)
Returns the default state manager for the given module
|
void |
handleEvent(Event<?> e) |
IModule<?> |
initModule(String moduleID)
Initializes the module with the given local ID
This method is synchronous so it will block forever until the module is actually initialized or an exception is thrown |
IModule<?> |
initModule(String moduleID,
boolean force,
long timeOut)
Initializes the module with the given local ID
This method is synchronous so it will block until the module is actually initialized, the timeout occurs or an exception is thrown |
void |
initModuleAsync(IModule<?> module,
boolean force)
Inits the module asynchronously in a separate thread
|
IModule<?> |
initModuleAsync(String moduleID,
boolean force,
IEventListener listener)
Initializes the module with the given local ID
This method is asynchronous so it returns immediately and the listener will be notified when the module is actually initialized |
boolean |
isModuleLoaded(String moduleID) |
void |
loadAllModules()
Loads all enabled modules from configuration entries provided
by the specified IModuleConfigRepository
|
Object |
loadClass(String className)
Loads any class by reflection
|
IModule<?> |
loadModule(ModuleConfig config)
Instantiates and loads a module using the given configuration
This method is synchronous so it will block forever until the module is actually loaded, and it will also wait for it to be started if 'autostart' was requested. |
IModule<?> |
loadModule(ModuleConfig config,
long timeOut)
Instantiates and loads a module using the given configuration
This method is synchronous so it will block until the module is actually loaded, (and started if 'autostart' was true), the timeout occurs or an exception is thrown |
IModule<?> |
loadModuleAsync(ModuleConfig config,
IEventListener listener)
Instantiates and loads a module using the given configuration
This method is asynchronous so, when it returns without error, the module is guaranteed to be loaded but not necessarilly initialized or started. |
protected void |
postInit(IModule<?> module) |
void |
registerListener(IEventListener listener)
Registers a listener with this event producer.
|
void |
restartModuleAsync(IModule<?> module)
Restarts the module asynchronously in a separate thread
This will actually called requestStop() and then requestStart() |
void |
restartModuleAsync(String moduleID,
IEventListener listener)
Restarts the module with the given local ID
This method is asynchronous so it returns immediately and the listener will be notified when the module is actually restarted |
void |
saveConfiguration(ModuleConfig... configList)
Saves the given module configurations in the repository
|
void |
saveModulesConfiguration()
Save all modules current configuration to the repository
|
void |
shutdown(boolean saveConfig,
boolean saveState)
Shuts down all modules and the config repository
|
IModule<?> |
startModule(String moduleID)
Starts the module with the given local ID
This method is synchronous so it will block forever until the module is actually started or an exception is thrown |
IModule<?> |
startModule(String moduleID,
long timeOut)
Starts the module with the given local ID
This method is synchronous so it will block until the module is actually started, the timeout occurs or an exception is thrown |
void |
startModuleAsync(IModule<?> module)
Starts the module asynchronously in a separate thread
|
IModule<?> |
startModuleAsync(String moduleID,
IEventListener listener)
Starts the module with the given local ID
This method is asynchronous so it returns immediately and the listener will be notified when the module is actually started |
IModule<?> |
stopModule(String moduleID)
Stops the module with the given local ID
This method is synchronous so it will block forever until the module is actually stopped or an exception is thrown |
IModule<?> |
stopModule(String moduleID,
long timeOut)
Stops the module with the given local ID
This method is synchronous so it will block until the module is actually stopped, the timeout occurs or an exception is thrown |
void |
stopModuleAsync(IModule<?> module)
Stops the module asynchronously in a separate thread
|
IModule<?> |
stopModuleAsync(String moduleID,
IEventListener listener)
Stops the module with the given local ID
This method is asynchronous so it returns immediately and the listener will be notified when the module is actually stopped |
void |
unloadModule(String moduleID)
Unloads a module instance.
This causes the module to be removed from registry but its last saved configuration is kept as-is. |
void |
unregisterListener(IEventListener listener)
Unregisters a listener from this event producer.
|
void |
updateModuleConfigAsync(IModule module,
ModuleConfig config)
Updates the module configuration asynchronously in a separate thread
|
void |
updateModuleConfigAsync(ModuleConfig config)
Updates the configuration of the module with the given local ID
|
void |
waitForAllModulesLoaded() |
public static final String ID
public static final long SHUTDOWN_TIMEOUT_MS
public ModuleRegistry(IModuleConfigRepository configRepos, EventBus eventBus)
public void loadAllModules()
public void waitForAllModulesLoaded()
public IModule<?> loadModule(ModuleConfig config) throws SensorHubException
config
- Configuration class to use to instantiate the moduleSensorHubException
public IModule<?> loadModule(ModuleConfig config, long timeOut) throws SensorHubException
config
- Configuration class to use to instantiate the moduletimeOut
- Maximum time to wait for load and startup to complete (or <= 0 to wait forever)SensorHubException
public IModule<?> loadModuleAsync(ModuleConfig config, IEventListener listener) throws SensorHubException
config
- Configuration class to use to instantiate the modulelistener
- Listener to register for receiving the module's eventsSensorHubException
- if no module with given ID can be foundpublic Object loadClass(String className) throws SensorHubException
className
- SensorHubException
public ModuleConfig createModuleConfig(IModuleProvider provider) throws SensorHubException
provider
- SensorHubException
public boolean isModuleLoaded(String moduleID)
isModuleLoaded
in interface IModuleManager<IModule<?>>
moduleID
- local ID of modulepublic void unloadModule(String moduleID) throws SensorHubException
saveConfiguration(ModuleConfig...)
first if you want to
keep the current config.moduleID
- SensorHubException
public IModule<?> initModule(String moduleID) throws SensorHubException
moduleID
- Local ID of module to initializeSensorHubException
- if an error occurs during initpublic IModule<?> initModule(String moduleID, boolean force, long timeOut) throws SensorHubException
moduleID
- Local ID of module to initializeforce
- set to true to force a reinit, even if module was already initializedtimeOut
- Maximum time to wait for init to complete (or <= 0 to wait forever)SensorHubException
- if an error occurs during initpublic IModule<?> initModuleAsync(String moduleID, boolean force, IEventListener listener) throws SensorHubException
moduleID
- Local ID of module to initializeforce
- set to true to force a reinit, even if module was already initializedlistener
- Listener to register for receiving the module's eventsSensorHubException
- if no module with given ID can be foundpublic void initModuleAsync(IModule<?> module, boolean force) throws SensorHubException
module
- module instance to initializeforce
- set to true to force a reinit, even if module was already initializedSensorHubException
public IModule<?> startModule(String moduleID) throws SensorHubException
moduleID
- Local ID of module to startSensorHubException
- if an error occurs during startuppublic IModule<?> startModule(String moduleID, long timeOut) throws SensorHubException
moduleID
- Local ID of module to starttimeOut
- Maximum time to wait for startup to complete (or <= 0 to wait forever)SensorHubException
- if an error occurs during startuppublic IModule<?> startModuleAsync(String moduleID, IEventListener listener) throws SensorHubException
moduleID
- Local ID of module to startlistener
- Listener to register for receiving the module's eventsSensorHubException
- if no module with given ID can be foundpublic void startModuleAsync(IModule<?> module) throws SensorHubException
module
- module instance to startSensorHubException
public IModule<?> stopModule(String moduleID) throws SensorHubException
moduleID
- Local ID of module to disableSensorHubException
- if an error occurs during shutdownpublic IModule<?> stopModule(String moduleID, long timeOut) throws SensorHubException
moduleID
- Local ID of module to enabletimeOut
- Maximum time to wait for shutdown to complete (or <= 0 to wait forever)SensorHubException
- if an error occurs during shutdownpublic IModule<?> stopModuleAsync(String moduleID, IEventListener listener) throws SensorHubException
moduleID
- Local ID of module to stoplistener
- Listener to register for receiving the module's eventsSensorHubException
- if no module with given ID can be foundpublic void stopModuleAsync(IModule<?> module) throws SensorHubException
module
- module instance to stopSensorHubException
public void restartModuleAsync(String moduleID, IEventListener listener) throws SensorHubException
moduleID
- Local ID of module to restartlistener
- Listener to register for receiving the module's eventsSensorHubException
- if no module with given ID can be foundpublic void restartModuleAsync(IModule<?> module) throws SensorHubException
module
- module instance to restartSensorHubException
public void updateModuleConfigAsync(ModuleConfig config) throws SensorHubException
config
- new configuration (must contain the valid local ID of the module to update)SensorHubException
- if no module with given ID can be foundpublic void updateModuleConfigAsync(IModule module, ModuleConfig config) throws SensorHubException
module
- module instance to updateconfig
- new module configurationSensorHubException
public void destroyModule(String moduleID) throws SensorHubException
moduleID
- Local ID of module to deleteSensorHubException
public void saveModulesConfiguration()
public void saveConfiguration(ModuleConfig... configList)
configList
- public Collection<IModule<?>> getLoadedModules()
IModuleManager
getLoadedModules
in interface IModuleManager<IModule<?>>
public IModule<?> getModuleById(String moduleID) throws SensorHubException
IModuleManager
getModuleById
in interface IModuleManager<IModule<?>>
moduleID
- Id of module to retrieveSensorHubException
- if no module with given ID can be foundpublic WeakReference<? extends IModule<?>> getModuleRef(String moduleID) throws SensorHubException
SensorHubException
public Collection<ModuleConfig> getAvailableModules()
IModuleManager
getAvailableModules
in interface IModuleManager<IModule<?>>
public Collection<IModuleProvider> getInstalledModuleTypes()
public Collection<IModuleProvider> getInstalledModuleTypes(Class<?> moduleClass)
moduleClass
- Parent class of modules to search forpublic void shutdown(boolean saveConfig, boolean saveState) throws SensorHubException
saveConfig
- If true, save current modules configsaveState
- If true, save current module stateSensorHubException
public IModuleStateManager getStateManager(String moduleID)
moduleID
- public File getModuleDataFolder(String moduleID)
moduleID
- Local ID of modulepublic void registerListener(IEventListener listener)
IEventProducer
registerListener
in interface IEventProducer
public void unregisterListener(IEventListener listener)
IEventProducer
unregisterListener
in interface IEventProducer
public void handleEvent(Event<?> e)
handleEvent
in interface IEventListener
protected void postInit(IModule<?> module)
Copyright © 2016 Sensia Software LLC. All rights reserved.