Package org.sensorhub.impl.module
Class ModuleRegistry
java.lang.Object
org.sensorhub.impl.module.ModuleRegistry
- All Implemented Interfaces:
IEventListener
,IModuleManager<IModule<?>>
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.
- Since:
- Sep 2, 2013
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateModuleConfig
(IModuleProvider provider) Creates a new module config class using information from a module providervoid
destroyModule
(String moduleID) Removes the module with the given id<T> Class<T>
Attempts to find a class by name.
If using OSGi this will scan all resolved bundles and attempt to load the class using the bundle's classloader.<T> Class<T>
findModuleClass
(String className) Finds a module class.Retrieves list of all installed module typesgetInstalledModuleTypes
(Class<?> moduleClass) Retrieves list of all installed module types that are sub-types of the specified classIModule<?>
getLoadedModuleById
(String moduleID) Collection<IModule<?>>
Gets the list of all modules handled by this manager that are already loaded (i.e.<T> Collection<T>
getLoadedModules
(Class<T> moduleType) Retrieves list of all loaded modules that are sub-types of the specified classIModule<?>
getModuleById
(String moduleID) Retrieves a module instance by its local ID<T> T
getModuleByType
(Class<T> moduleType) Find first module with the given typegetModuleDataFolder
(String moduleID) Retrieves the folder where the module data should be stored<T extends IModule<?>>
WeakReference<T>getModuleRef
(String moduleID) getStateManager
(String moduleID) Returns the default state manager for the given modulevoid
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 thrownIModule<?>
initModule
(String moduleID, 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 thrownIModule<?>
initModuleAsync
(String moduleID, 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 initializedvoid
initModuleAsync
(IModule<?> module) Initializes the module asynchronously in a separate threadprotected boolean
isDataStoreModule
(ModuleConfig config) boolean
isModuleLoaded
(String moduleID) void
Loads all enabled modules from configuration entries provided by the specified IModuleConfigRepository<T> T
Instantiate any class by reflection using the default constructor.
The class is first loaded usingfindClass(String)
.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 thrownIModule<?>
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 necessarily initialized or started.protected IModule<?>
loadModuleAsync
(ModuleConfig config, IEventListener listener, boolean doAutoStart) <T> T
loadModuleClass
(String className) Loads a module class by reflection.<T extends IModule<C>,
C extends ModuleConfig>
TloadSubModule
(C config, boolean init) Helper method to load and optionally initialize a sub module.<T extends ISubModule<C>,
C extends SubModuleConfig>
TloadSubModule
(IModule<?> parentModule, C config, boolean init) Helper method to load and optionally initialize a sub module.protected void
protected void
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 restartedvoid
restartModuleAsync
(IModule<?> module) Restarts the module asynchronously in a separate thread
This will actually called requestStop() and then requestStart()void
saveConfiguration
(ModuleConfig... configList) Saves the given module configurations in the repositoryvoid
Save all modules current configuration to the repositoryvoid
shutdown
(boolean saveConfig, boolean saveState) Shuts down all modules and the config repositoryIModule<?>
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 thrownIModule<?>
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 thrownIModule<?>
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 startedvoid
startModuleAsync
(IModule<?> module) Starts the module asynchronously in a separate threadIModule<?>
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 thrownIModule<?>
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 thrownIModule<?>
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 stoppedvoid
stopModuleAsync
(IModule<?> module) Stops the module asynchronously in a separate threadvoid
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
updateModuleConfigAsync
(IModule module, ModuleConfig config) Updates the module configuration asynchronously in a separate threadvoid
updateModuleConfigAsync
(ModuleConfig config) Updates the configuration of the module with the given local ID<T extends IModule<?>>
CompletableFuture<T>waitForModule
(String id, ModuleEvent.ModuleState requiredState) <T> CompletableFuture<T>
waitForModule
(Predicate<IModule<?>> predicate) <T> CompletableFuture<T>
waitForModuleType
(Class<T> moduleType, ModuleEvent.ModuleState requiredState)
-
Field Details
-
EVENT_GROUP_ID
- See Also:
-
DEFAULT_TIMEOUT_MS
public static final long DEFAULT_TIMEOUT_MS- See Also:
-
SHUTDOWN_TIMEOUT_MS
public static final long SHUTDOWN_TIMEOUT_MS- See Also:
-
-
Constructor Details
-
ModuleRegistry
-
-
Method Details
-
loadAllModules
Loads all enabled modules from configuration entries provided by the specified IModuleConfigRepository- Throws:
SensorHubException
-
isDataStoreModule
- Throws:
SensorHubException
-
loadModule
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.- Parameters:
config
- Configuration class to use to instantiate the module- Returns:
- loaded module instance
- Throws:
SensorHubException
-
loadModule
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- Parameters:
config
- Configuration class to use to instantiate the moduletimeOut
- Maximum time to wait for load and startup to complete (or 0 to wait forever)- Returns:
- loaded module instance
- Throws:
SensorHubException
-
loadModuleAsync
public IModule<?> loadModuleAsync(ModuleConfig config, IEventListener listener) throws SensorHubException 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 necessarily initialized or started. The listener will be notified when the module's state changes further.- Parameters:
config
- Configuration class to use to instantiate the modulelistener
- Listener to register for receiving the module's events- Returns:
- loaded module instance (may not yet be started when this method returns)
- Throws:
SensorHubException
- if no module with given ID can be found
-
loadModuleAsync
protected IModule<?> loadModuleAsync(ModuleConfig config, IEventListener listener, boolean doAutoStart) throws SensorHubException - Throws:
SensorHubException
-
findClass
Attempts to find a class by name.
If using OSGi this will scan all resolved bundles and attempt to load the class using the bundle's classloader.- Parameters:
className
- Fully qualified name of the class to load- Returns:
- Loaded class
- Throws:
SensorHubException
-
loadClass
Instantiate any class by reflection using the default constructor.
The class is first loaded usingfindClass(String)
.- Parameters:
className
- Fully qualified name of the class to instantiate- Returns:
- New object instantiated
- Throws:
SensorHubException
-
findModuleClass
Finds a module class. If using OSGi, this will attempt to find a service providing the module.- Parameters:
className
- Fully qualified name of the module class to load- Returns:
- new object instantiated
- Throws:
SensorHubException
-
loadModuleClass
Loads a module class by reflection. If using OSGi, this will attempt to find a service providing the module- Parameters:
className
- Fully qualified name of the module class to instantiate- Returns:
- new object instantiated
- Throws:
SensorHubException
-
loadSubModule
public <T extends ISubModule<C>,C extends SubModuleConfig> T loadSubModule(IModule<?> parentModule, C config, boolean init) throws SensorHubException Helper method to load and optionally initialize a sub module. The submodule will be automatically associated with the provided parent module. Once this method returns, the caller (i.e. usually the parent module) is responsible for managing the submodule life cycle, not the module registry itself.- Type Parameters:
T
- Type of module configuration- Parameters:
parentModule
- The module that will be set as the parent of the submoduleconfig
- Sub module configuration classinit
- If set to true, also initialize the module- Returns:
- The module instance
- Throws:
SensorHubException
-
loadSubModule
public <T extends IModule<C>,C extends ModuleConfig> T loadSubModule(C config, boolean init) throws SensorHubException Helper method to load and optionally initialize a sub module. A sub module is a module loaded by another module. Once this method returns, the caller (i.e. usually the parent module) is responsible for managing the submodule life cycle, not the module registry itself.- Type Parameters:
T
- Type of module configuration- Parameters:
config
- Sub module configuration classinit
- If set to true, also initialize the module- Returns:
- The module instance
- Throws:
SensorHubException
-
createModuleConfig
Creates a new module config class using information from a module provider- Parameters:
provider
-- Returns:
- the new configuration class
- Throws:
SensorHubException
-
isModuleLoaded
- Specified by:
isModuleLoaded
in interfaceIModuleManager<IModule<?>>
- Parameters:
moduleID
- local ID of module- Returns:
- true if module is loaded, false otherwise
-
unloadModule
Unloads a module instance.
This causes the module to be removed from registry but its last saved configuration is kept as-is. CallsaveConfiguration(ModuleConfig...)
first if you want to keep the current config.- Parameters:
moduleID
-- Throws:
SensorHubException
-
initModule
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- Parameters:
moduleID
- Local ID of module to initialize- Returns:
- module instance corresponding to moduleID
- Throws:
SensorHubException
- if an error occurs during init
-
initModule
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- Parameters:
moduleID
- Local ID of module to initializetimeOut
- Maximum time to wait for init to complete (or 0 to wait forever)- Returns:
- module Loaded module with the given moduleID
- Throws:
SensorHubException
- if an error occurs during init
-
initModuleAsync
public IModule<?> initModuleAsync(String moduleID, IEventListener listener) throws SensorHubException 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- Parameters:
moduleID
- Local ID of module to initializelistener
- Listener to register for receiving the module's events- Returns:
- the module instance (may not yet be initialized when this method returns)
- Throws:
SensorHubException
- if no module with given ID can be found
-
initModuleAsync
Initializes the module asynchronously in a separate thread- Parameters:
module
- module instance to initialize- Throws:
SensorHubException
-
startModule
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- Parameters:
moduleID
- Local ID of module to start- Returns:
- module instance corresponding to moduleID
- Throws:
SensorHubException
- if an error occurs during startup
-
startModule
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- Parameters:
moduleID
- Local ID of module to starttimeOut
- Maximum time to wait for startup to complete (or 0 to wait forever)- Returns:
- module Loaded module with the given moduleID
- Throws:
SensorHubException
- if an error occurs during startup
-
startModuleAsync
public IModule<?> startModuleAsync(String moduleID, IEventListener listener) throws SensorHubException 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- Parameters:
moduleID
- Local ID of module to startlistener
- Listener to register for receiving the module's events- Returns:
- the module instance (may not yet be started when this method returns)
- Throws:
SensorHubException
- if no module with given ID can be found
-
startModuleAsync
Starts the module asynchronously in a separate thread- Parameters:
module
- module instance to start- Throws:
SensorHubException
-
stopModule
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- Parameters:
moduleID
- Local ID of module to disable- Returns:
- module instance corresponding to moduleID
- Throws:
SensorHubException
- if an error occurs during shutdown
-
stopModule
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- Parameters:
moduleID
- Local ID of module to enabletimeOut
- Maximum time to wait for shutdown to complete (or 0 to wait forever)- Returns:
- module Loaded module with the given moduleID
- Throws:
SensorHubException
- if an error occurs during shutdown
-
stopModuleAsync
public IModule<?> stopModuleAsync(String moduleID, IEventListener listener) throws SensorHubException 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- Parameters:
moduleID
- Local ID of module to stoplistener
- Listener to register for receiving the module's events- Returns:
- the module instance (may not yet be stopped when this method returns)
- Throws:
SensorHubException
- if no module with given ID can be found
-
stopModuleAsync
Stops the module asynchronously in a separate thread- Parameters:
module
- module instance to stop- Throws:
SensorHubException
-
restartModuleAsync
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- Parameters:
moduleID
- Local ID of module to restartlistener
- Listener to register for receiving the module's events- Throws:
SensorHubException
- if no module with given ID can be found
-
restartModuleAsync
Restarts the module asynchronously in a separate thread
This will actually called requestStop() and then requestStart()- Parameters:
module
- module instance to restart- Throws:
SensorHubException
-
updateModuleConfigAsync
Updates the configuration of the module with the given local ID- Parameters:
config
- new configuration (must contain the valid local ID of the module to update)- Throws:
SensorHubException
- if no module with given ID can be found
-
updateModuleConfigAsync
Updates the module configuration asynchronously in a separate thread- Parameters:
module
- module instance to updateconfig
- new module configuration- Throws:
SensorHubException
-
destroyModule
Removes the module with the given id- Parameters:
moduleID
- Local ID of module to delete- Throws:
SensorHubException
-
saveModulesConfiguration
public void saveModulesConfiguration()Save all modules current configuration to the repository -
saveConfiguration
Saves the given module configurations in the repository- Parameters:
configList
-
-
getLoadedModuleById
- Parameters:
moduleID
- local ID of desired module- Returns:
- module with given ID or null if not found
-
getLoadedModules
Description copied from interface:IModuleManager
Gets the list of all modules handled by this manager that are already loaded (i.e. enabled)- Specified by:
getLoadedModules
in interfaceIModuleManager<IModule<?>>
- Returns:
- list of module instances
-
getLoadedModules
Retrieves list of all loaded modules that are sub-types of the specified class- Parameters:
moduleType
- parent class of modules to search for- Returns:
- list of module instances of the specified type
-
getModuleByType
Find first module with the given type- Parameters:
moduleType
- parent class of modules to search for- Returns:
- The matching module or null if none were found
-
getModuleById
Description copied from interface:IModuleManager
Retrieves a module instance by its local ID- Specified by:
getModuleById
in interfaceIModuleManager<IModule<?>>
- Parameters:
moduleID
- Id of module to retrieve- Returns:
- direct reference to the module instance
- Throws:
SensorHubException
- if no module with given ID can be found
-
getModuleRef
public <T extends IModule<?>> WeakReference<T> getModuleRef(String moduleID) throws SensorHubException - Throws:
SensorHubException
-
getInstalledModuleTypes
Retrieves list of all installed module types- Returns:
- list of module providers (not the module themselves)
-
getInstalledModuleTypes
Retrieves list of all installed module types that are sub-types of the specified class- Parameters:
moduleClass
- parent class of modules to search for- Returns:
- list of module providers (not the module themselves)
-
shutdown
Shuts down all modules and the config repository- Parameters:
saveConfig
- If true, save current modules configsaveState
- If true, save current module state- Throws:
SensorHubException
-
getStateManager
Returns the default state manager for the given module- Parameters:
moduleID
-- Returns:
- the state manager or null if no module data folder is specified in config
-
getCoreStateManager
- Returns:
- the state manager for the 'core' module
-
getModuleDataFolder
Retrieves the folder where the module data should be stored- Parameters:
moduleID
- Local ID of module- Returns:
- File object representing the folder or null if none was specified
-
handleEvent
- Specified by:
handleEvent
in interfaceIEventListener
-
notifyWaitFutures
-
waitForModuleType
public <T> CompletableFuture<T> waitForModuleType(Class<T> moduleType, ModuleEvent.ModuleState requiredState) -
waitForModule
public <T extends IModule<?>> CompletableFuture<T> waitForModule(String id, ModuleEvent.ModuleState requiredState) -
waitForModule
-
postInit
-
getParentHub
-