Package eu.cloudnetservice.driver.module
Class DefaultModuleProvider
java.lang.Object
eu.cloudnetservice.driver.module.DefaultModuleProvider
- All Implemented Interfaces:
ModuleProvider
@Singleton
@Provides(ModuleProvider.class)
public class DefaultModuleProvider
extends Object
implements ModuleProvider
Represents the default implementation of the module provider.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final dev.derklaro.aerogel.Elementprotected static final ModuleDependencyLoaderstatic final Pathstatic final Pathprotected static final eu.cloudnetservice.common.log.Loggerprivate static final dev.derklaro.aerogel.Elementprotected ModuleDependencyLoaderprotected Pathprotected ModuleProviderHandlerprotected final Collection<ModuleWrapper> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new default module provider by calling with the default library directory and the default dependency loader.DefaultModuleProvider(@NonNull Path moduleDirectory, @NonNull ModuleDependencyLoader moduleDependencyLoader) Creates a new default module provider with the given directory and dependency loader. -
Method Summary
Modifier and TypeMethodDescriptioncollectModuleProvidedRepositories(@NonNull ModuleConfiguration configuration) Collects all repositories provided in the givenModuleConfigurationto a standard url format.doLoadDependency(@NonNull ModuleDependency dependency, @NonNull ModuleConfiguration configuration, @Nullable ModuleProviderHandler handler, @NonNull Callable<URL> loader) Tries to load a module dependency.protected @NonNull Optional<ModuleWrapper>findModuleBySource(@NonNull URL fileSource) Finds a loaded module based on the given file source url.protected @NonNull Optional<ModuleConfiguration>findModuleConfiguration(@NonNull URL moduleFile) Finds the module.json file in the provided module file and deserializes it.loadAll()Loads all modules which files are located at the module directory.protected @NonNull eu.cloudnetservice.common.tuple.Tuple2<Set<URL>,Set<ModuleDependency>> loadDependencies(@NonNull Map<String, String> repos, @NonNull ModuleConfiguration configuration) Loads all dependencies defined in the module configuration file.loadModule(@NonNull URL url) Loads a module from the given url.loadModule(@NonNull Path path) Loads the module by the file provided by the given path.Get a module by the given name.Get the module dependency loader.voidmoduleDependencyLoader(@NonNull ModuleDependencyLoader moduleDependencyLoader) Sets the module dependency loader which should be used by this provider.Get the base directory of this module provider.voidmoduleDirectoryPath(@NonNull Path moduleDirectory) Sets the base directory of this module provider.Get the module provider handler of this provider or null when no handler is specified.voidmoduleProviderHandler(@Nullable ModuleProviderHandler moduleProviderHandler) Sets the module provider handler of this provider.modules()Get all loaded, started, stopped modules provided by this provider.Get all loaded, started, stopped modules provided by this provider which have the specific given group.voidnotifyPostModuleLifecycleChange(@NonNull ModuleWrapper wrapper, @NonNull ModuleLifeCycle lifeCycle) Called by anModuleWrapperwhen the module changed its lifecycle state.booleannotifyPreModuleLifecycleChange(@NonNull ModuleWrapper wrapper, @NonNull ModuleLifeCycle lifeCycle) Called by anModuleWrapperwhen the module is about to change its lifecycle state.Reloads all modules which are loaded by this provided and can change to the started state.startAll()Starts all modules which are loaded by this provided and can change to the started state.stopAll()Stops all modules which are loaded by this provided and can change to the stopped state.Unloads all modules which are loaded by this provided and can change to the unloaded state.
-
Field Details
-
DEFAULT_LIB_DIR
-
DEFAULT_MODULE_DIR
-
LOGGER
protected static final eu.cloudnetservice.common.log.Logger LOGGER -
DEFAULT_DEP_LOADER
-
MODULE_CONFIGURATION_ELEMENT
private static final dev.derklaro.aerogel.Element MODULE_CONFIGURATION_ELEMENT -
DATA_DIRECTORY_ELEMENT
private static final dev.derklaro.aerogel.Element DATA_DIRECTORY_ELEMENT -
modules
-
moduleDirectory
-
moduleProviderHandler
-
moduleDependencyLoader
-
-
Constructor Details
-
DefaultModuleProvider
public DefaultModuleProvider()Creates a new default module provider by calling with the default library directory and the default dependency loader.The default library directory is
DEFAULT_LIB_DIR. The default dependency loader isDEFAULT_DEP_LOADER -
DefaultModuleProvider
public DefaultModuleProvider(@NonNull @NonNull Path moduleDirectory, @NonNull @NonNull ModuleDependencyLoader moduleDependencyLoader) Creates a new default module provider with the given directory and dependency loader.- Parameters:
moduleDirectory- the directory where modules are stored.moduleDependencyLoader- the dependency loader to load all module dependencies.- Throws:
NullPointerException- if the given module directory or dependency loader is null.- See Also:
-
-
Method Details
-
moduleDirectoryPath
Get the base directory of this module provider. It will be used to provide the data directory for modules which did not specifically set a data directory.- Specified by:
moduleDirectoryPathin interfaceModuleProvider- Returns:
- the base directory of this module provider.
- See Also:
-
moduleDirectoryPath
Sets the base directory of this module provider. It will be used to provide the data directory for modules which did not specifically set a data directory.- Specified by:
moduleDirectoryPathin interfaceModuleProvider- Parameters:
moduleDirectory- the module directory to use.- See Also:
-
moduleProviderHandler
Get the module provider handler of this provider or null when no handler is specified.- Specified by:
moduleProviderHandlerin interfaceModuleProvider- Returns:
- the module provider handler of this provider or null.
- See Also:
-
moduleProviderHandler
Sets the module provider handler of this provider.- Specified by:
moduleProviderHandlerin interfaceModuleProvider- Parameters:
moduleProviderHandler- the new module provider to use or null when no handler should be used.- See Also:
-
moduleDependencyLoader
Get the module dependency loader. It's used to load all dependencies of all modules.This handler is by default
DefaultModuleDependencyLoader.- Specified by:
moduleDependencyLoaderin interfaceModuleProvider- Returns:
- the module provider handler used for this provider.
- See Also:
-
moduleDependencyLoader
Sets the module dependency loader which should be used by this provider.- Specified by:
moduleDependencyLoaderin interfaceModuleProvider- Parameters:
moduleDependencyLoader- the module dependency loader to use.- See Also:
-
modules
Get all loaded, started, stopped modules provided by this provider.- Specified by:
modulesin interfaceModuleProvider- Returns:
- an immutable set of all loaded, started, stopped modules provided by this provider.
- See Also:
-
modules
Get all loaded, started, stopped modules provided by this provider which have the specific given group.- Specified by:
modulesin interfaceModuleProvider- Parameters:
group- the group id of the modules to get.- Returns:
- an immutable set of all loaded, started, stopped modules provided by this provider which have the specific given group.
- See Also:
-
module
Get a module by the given name.- Specified by:
modulein interfaceModuleProvider- Parameters:
name- the name of the module to get.- Returns:
- the module associated with the name or null if no such module is loaded.
-
loadModule
Loads a module from the given url.- Specified by:
loadModulein interfaceModuleProvider- Parameters:
url- the url to load the module from.- Returns:
- the loaded module or null if checks failed or a module from this url is already loaded.
-
loadModule
Loads the module by the file provided by the given path.- Specified by:
loadModulein interfaceModuleProvider- Parameters:
path- the path to load the module from.- Returns:
- the loaded module or null if checks failed or a module from this path is already loaded.
- See Also:
-
loadAll
Description copied from interface:ModuleProviderLoads all modules which files are located at the module directory.- Specified by:
loadAllin interfaceModuleProvider- Returns:
- the same instance of the class, for chaining.
- See Also:
-
startAll
Starts all modules which are loaded by this provided and can change to the started state.- Specified by:
startAllin interfaceModuleProvider- Returns:
- the same instance of the class, for chaining.
- See Also:
-
reloadAll
Reloads all modules which are loaded by this provided and can change to the started state.- Specified by:
reloadAllin interfaceModuleProvider- Returns:
- the same instance of the class, for chaining.
- See Also:
-
stopAll
Stops all modules which are loaded by this provided and can change to the stopped state.- Specified by:
stopAllin interfaceModuleProvider- Returns:
- the same instance of the class, for chaining.
- See Also:
-
unloadAll
Unloads all modules which are loaded by this provided and can change to the unloaded state.- Specified by:
unloadAllin interfaceModuleProvider- Returns:
- the same instance of the class, for chaining.
- See Also:
-
notifyPreModuleLifecycleChange
public boolean notifyPreModuleLifecycleChange(@NonNull @NonNull ModuleWrapper wrapper, @NonNull @NonNull ModuleLifeCycle lifeCycle) Called by anModuleWrapperwhen the module is about to change its lifecycle state.- Specified by:
notifyPreModuleLifecycleChangein interfaceModuleProvider- Parameters:
wrapper- the wrapper which is changing the lifecycle.lifeCycle- the lifecycle the wrapper want's to change to.- Returns:
- If the wrapper is allowed to change the lifecycle to the provided lifecycle.
-
notifyPostModuleLifecycleChange
public void notifyPostModuleLifecycleChange(@NonNull @NonNull ModuleWrapper wrapper, @NonNull @NonNull ModuleLifeCycle lifeCycle) Called by anModuleWrapperwhen the module changed its lifecycle state.- Specified by:
notifyPostModuleLifecycleChangein interfaceModuleProvider- Parameters:
wrapper- the wrapper which changed the lifecycle.lifeCycle- the lifecycle the wrapper changed to.
-
findModuleConfiguration
@NonNull protected @NonNull Optional<ModuleConfiguration> findModuleConfiguration(@NonNull @NonNull URL moduleFile) throws IOException Finds the module.json file in the provided module file and deserializes it.- Parameters:
moduleFile- the module file to find the module configuration of.- Returns:
- the deserialized module configuration file located in the provided module file.
- Throws:
IOException- if an I/O or deserialize exception occurs.NullPointerException- if the given module file is null.
-
findModuleBySource
@NonNull protected @NonNull Optional<ModuleWrapper> findModuleBySource(@NonNull @NonNull URL fileSource) throws URISyntaxException Finds a loaded module based on the given file source url.- Parameters:
fileSource- the source to find the associated module with.- Returns:
- the associated module to the given url.
- Throws:
URISyntaxException- if the given URL is not formatted strictly according to RFC2396.NullPointerException- if the given file source is null.
-
collectModuleProvidedRepositories
@NonNull protected @NonNull Map<String,String> collectModuleProvidedRepositories(@NonNull @NonNull ModuleConfiguration configuration) Collects all repositories provided in the givenModuleConfigurationto a standard url format.- Parameters:
configuration- the module configuration to read the repositories from.- Returns:
- all configured repositories in a standardized way.
- Throws:
NullPointerException- if configuration is null.
-
loadDependencies
@NonNull protected @NonNull eu.cloudnetservice.common.tuple.Tuple2<Set<URL>,Set<ModuleDependency>> loadDependencies(@NonNull @NonNull Map<String, String> repos, @NonNull @NonNull ModuleConfiguration configuration) Loads all dependencies defined in the module configuration file.- Parameters:
repos- the repositories from which the dependencies can get loaded.configuration- the configuration of the module to load the dependencies of.- Returns:
- a pair, first are all the loaded dependencies; second are all the pending ones (module dependencies)
- Throws:
AssertionError- if one dependency can't be loaded.NullPointerException- if repos or configuration is null.
-
doLoadDependency
@NonNull protected @NonNull URL doLoadDependency(@NonNull @NonNull ModuleDependency dependency, @NonNull @NonNull ModuleConfiguration configuration, @Nullable @Nullable ModuleProviderHandler handler, @NonNull @NonNull Callable<URL> loader) Tries to load a module dependency. This method is fail-fast.- Parameters:
dependency- the dependency to load.configuration- the configuration from which the dependency was declared.handler- the provider handler if one is set and should be notified, else null.loader- the callback which will load the dependency.- Returns:
- the location of the loaded dependency in url form.
- Throws:
AssertionError- if loader fails to load the dependency.NullPointerException- if dependency, configuration, handler or loader is null.
-