Class ModuleRegistry

java.lang.Object
org.sensorhub.impl.module.ModuleRegistry
All Implemented Interfaces:
IEventListener, IModuleManager<IModule<?>>

public class ModuleRegistry extends Object implements IModuleManager<IModule<?>>, 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.

Since:
Sep 2, 2013
  • Field Details

  • Constructor Details

  • Method Details

    • loadAllModules

      public void loadAllModules() throws SensorHubException
      Loads all enabled modules from configuration entries provided by the specified IModuleConfigRepository
      Throws:
      SensorHubException
    • isDataStoreModule

      protected boolean isDataStoreModule(ModuleConfig config) throws SensorHubException
      Throws:
      SensorHubException
    • loadModule

      public IModule<?> loadModule(ModuleConfig config) throws SensorHubException
      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

      public IModule<?> loadModule(ModuleConfig config, long timeOut) throws SensorHubException
      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 module
      timeOut - 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 module
      listener - 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

      public <T> Class<T> findClass(String className) throws SensorHubException
      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

      public <T> T loadClass(String className) throws SensorHubException
      Instantiate any class by reflection using the default constructor.
      The class is first loaded using findClass(String).
      Parameters:
      className - Fully qualified name of the class to instantiate
      Returns:
      New object instantiated
      Throws:
      SensorHubException
    • findModuleClass

      public <T> Class<T> findModuleClass(String className) throws SensorHubException
      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

      public <T> T loadModuleClass(String className) throws SensorHubException
      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 submodule
      config - Sub module configuration class
      init - 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 class
      init - If set to true, also initialize the module
      Returns:
      The module instance
      Throws:
      SensorHubException
    • createModuleConfig

      public ModuleConfigBase createModuleConfig(IModuleProvider provider) throws SensorHubException
      Creates a new module config class using information from a module provider
      Parameters:
      provider -
      Returns:
      the new configuration class
      Throws:
      SensorHubException
    • isModuleLoaded

      public boolean isModuleLoaded(String moduleID)
      Specified by:
      isModuleLoaded in interface IModuleManager<IModule<?>>
      Parameters:
      moduleID - local ID of module
      Returns:
      true if module is loaded, false otherwise
    • unloadModule

      public void unloadModule(String moduleID) throws SensorHubException
      Unloads a module instance.
      This causes the module to be removed from registry but its last saved configuration is kept as-is. Call saveConfiguration(ModuleConfig...) first if you want to keep the current config.
      Parameters:
      moduleID -
      Throws:
      SensorHubException
    • initModule

      public IModule<?> initModule(String moduleID) throws SensorHubException
      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

      public IModule<?> initModule(String moduleID, long timeOut) throws SensorHubException
      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 initialize
      timeOut - 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 initialize
      listener - 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

      public void initModuleAsync(IModule<?> module) throws SensorHubException
      Initializes the module asynchronously in a separate thread
      Parameters:
      module - module instance to initialize
      Throws:
      SensorHubException
    • startModule

      public IModule<?> startModule(String moduleID) throws SensorHubException
      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

      public IModule<?> startModule(String moduleID, long timeOut) throws SensorHubException
      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 start
      timeOut - 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 start
      listener - 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

      public void startModuleAsync(IModule<?> module) throws SensorHubException
      Starts the module asynchronously in a separate thread
      Parameters:
      module - module instance to start
      Throws:
      SensorHubException
    • stopModule

      public IModule<?> stopModule(String moduleID) throws SensorHubException
      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

      public IModule<?> stopModule(String moduleID, long timeOut) throws SensorHubException
      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 enable
      timeOut - 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 stop
      listener - 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

      public void stopModuleAsync(IModule<?> module) throws SensorHubException
      Stops the module asynchronously in a separate thread
      Parameters:
      module - module instance to stop
      Throws:
      SensorHubException
    • restartModuleAsync

      public void restartModuleAsync(String moduleID, IEventListener listener) throws SensorHubException
      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 restart
      listener - Listener to register for receiving the module's events
      Throws:
      SensorHubException - if no module with given ID can be found
    • restartModuleAsync

      public void restartModuleAsync(IModule<?> module) throws SensorHubException
      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

      public void updateModuleConfigAsync(ModuleConfig config) throws SensorHubException
      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

      public void updateModuleConfigAsync(IModule module, ModuleConfig config) throws SensorHubException
      Updates the module configuration asynchronously in a separate thread
      Parameters:
      module - module instance to update
      config - new module configuration
      Throws:
      SensorHubException
    • destroyModule

      public void destroyModule(String moduleID) throws SensorHubException
      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

      public void saveConfiguration(ModuleConfig... configList)
      Saves the given module configurations in the repository
      Parameters:
      configList -
    • getLoadedModuleById

      public IModule<?> getLoadedModuleById(String moduleID)
      Parameters:
      moduleID - local ID of desired module
      Returns:
      module with given ID or null if not found
    • getLoadedModules

      public Collection<IModule<?>> 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 interface IModuleManager<IModule<?>>
      Returns:
      list of module instances
    • getLoadedModules

      public <T> Collection<T> getLoadedModules(Class<T> moduleType)
      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

      public <T> T getModuleByType(Class<T> moduleType)
      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

      public IModule<?> getModuleById(String moduleID) throws SensorHubException
      Description copied from interface: IModuleManager
      Retrieves a module instance by its local ID
      Specified by:
      getModuleById in interface IModuleManager<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

      public Collection<IModuleProvider> getInstalledModuleTypes()
      Retrieves list of all installed module types
      Returns:
      list of module providers (not the module themselves)
    • getInstalledModuleTypes

      public Collection<IModuleProvider> getInstalledModuleTypes(Class<?> moduleClass)
      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

      public void shutdown(boolean saveConfig, boolean saveState) throws SensorHubException
      Shuts down all modules and the config repository
      Parameters:
      saveConfig - If true, save current modules config
      saveState - If true, save current module state
      Throws:
      SensorHubException
    • getStateManager

      public IModuleStateManager getStateManager(String moduleID)
      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

      public IModuleStateManager getCoreStateManager()
      Returns:
      the state manager for the 'core' module
    • getModuleDataFolder

      public File getModuleDataFolder(String moduleID)
      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

      public void handleEvent(Event e)
      Specified by:
      handleEvent in interface IEventListener
    • notifyWaitFutures

      protected void notifyWaitFutures(ModuleEvent e)
    • 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

      public <T> CompletableFuture<T> waitForModule(Predicate<IModule<?>> predicate)
    • postInit

      protected void postInit(IModule<?> module)
    • getParentHub

      public ISensorHub getParentHub()