public class ModuleManager
extends java.lang.Object
Main manager for the modules framework. ModuleManager does all the work connecting modules with each other, solving dependencies, loading configuration and setting up the whole framework.
To use this, just make a new instance of ModuleManager and then either load a configuration file or add some modules manually, initialise them, and finally start the modules. You can also first add some modules manually and then load the rest with a configuration file. You typically do this to establish some kind of an attachment point for the modules or to add a logging module or some such beforehand.
You may also add variables before loading the configuration file, these can then be used in the configuration file. For example, if you know something about the environment, such as the network port the service is running, the working directory or something similar, you could add these as variables. Then the configuration file can use them instead of having them hardcoded.
After adding the modules, you have to initialise them. Initialisation of modules needs a map of module properties. If you read the modules from a configuration file, the properties will also have been read and are stored for later use. When you call initAllModules, all modules that are not yet initialised will be initialised with the parameters read from the configuration file. Prior to this, you can initialise modules manually if you wish.
After initialisation, you need to start the modules. Typically you will call autostartModules, which will start all modules that were configured to be started automatically. Alternatively, you can also use startAllModules which will start every single module that hasn't been started yet. Or you can always start modules individually too with startModule or startModuleWithDependencies. startModule will throw an exception if the dependencies haven't been started yet whereas the other method simply starts them too.
http://wandora.orgwiki/Wandora_modules_framework for additional documentation about the modules framework.Modifier and Type | Class and Description |
---|---|
static class |
ModuleManager.ModuleSettings
A class that contains all the different module settings.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.HashMap<Module,java.util.ArrayList<Module>> |
isRequiredBy
Modules that are used by some other running module are put here so that
they cannot be stopped before the depending module is stopped.
|
protected org.apache.commons.logging.Log |
log
Logger used by the manager.
|
protected ListenerList<ModuleListener> |
moduleListeners |
protected java.util.HashMap<Module,java.util.HashMap<java.lang.String,java.lang.Object>> |
moduleParams
Initialisation parameters that will be passed to module init method, read
from the config file.
|
protected java.util.ArrayList<Module> |
modules
A list containing all the modules this manager takes care of.
|
protected java.util.HashMap<Module,ModuleManager.ModuleSettings> |
moduleSettings
Other settings about modules besides the parameters.
|
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> |
variables
Variables used in the configuration file.
|
private static javax.xml.xpath.XPath |
xpath |
Constructor and Description |
---|
ModuleManager() |
Modifier and Type | Method and Description |
---|---|
void |
addModule(Module module)
Adds a module to this manager with default settings and no module
parameters.
|
void |
addModule(Module module,
java.util.HashMap<java.lang.String,java.lang.Object> params)
Adds a module to this manager with the given module parameters and
default settings.
|
void |
addModule(Module module,
java.util.HashMap<java.lang.String,java.lang.Object> params,
ModuleManager.ModuleSettings settings)
Adds a module to this manager with the given module parameters and
settings.
|
void |
addModule(Module module,
ModuleManager.ModuleSettings settings)
Adds a module to this manager with the given module settings but
empty module parameters.
|
void |
addModule(Module module,
java.lang.String name)
Adds a module to this manager with default settings and no module
parameters but with the provided name.
|
void |
addModuleListener(ModuleListener l)
Add a listener that will be notified about changes in modules in this manager.
|
void |
autostartModules()
Starts all modules that have not yet been started and that were set to
start automatically in module settings.
|
void |
autostartModules(java.util.Collection<Module> modules)
In a given modules collection, starts all modules that have not
yet been started and that were set to start automatically in module
settings.
|
<A extends Module> |
findModule(java.lang.Class<A> cls)
Finds a module that is of the specified class or one extending it.
|
<A extends Module> |
findModule(Module context,
java.lang.Class<A> cls)
Finds a module that is of the specified class or one extending it.
|
<A extends Module> |
findModule(Module context,
java.lang.String instanceName,
java.lang.Class<A> cls)
Finds a module that is of the specified class or one extending it.
|
<A extends Module> |
findModule(java.lang.String instanceName,
java.lang.Class<A> cls)
Finds a module that is of the specified class or one extending it.
|
<A extends Module> |
findModules(java.lang.Class<A> cls)
Finds all modules that are of the specified class or one extending it.
|
java.util.ArrayList<Module> |
getAllModules()
Returns a list of all modules added to this manager.
|
org.apache.commons.logging.Log |
getLogger()
Returns the logger used for manager logging.
|
java.lang.String |
getModuleName(Module m)
Gets the module name.
|
ModuleManager.ModuleSettings |
getModuleSettings(Module m)
Gets module settings.
|
java.lang.String |
getVariable(java.lang.String key)
Gets the value of a defined variable.
|
void |
initAllModules()
Initialises all modules that have not yet been initialised.
|
boolean |
isModuleAutoStart(Module m)
Checks if module is to be started automatically.
|
java.lang.String |
moduleToString(java.lang.Class<?> cls,
java.lang.String instanceName)
Creates a string representation of a module class.
|
java.lang.String |
moduleToString(Module module)
Creates a string representation of a module.
|
<A extends Module> |
optionalModule(java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Look for a module that will be used if found but is not required.
|
<A extends Module> |
optionalModule(Module context,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Look for a module that will be used if found but is not required.
|
<A extends Module> |
optionalModule(Module context,
java.lang.String instanceName,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Look for a module that will be used if found but is not required.
|
<A extends Module> |
optionalModule(java.lang.String instanceName,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Look for a module that will be used if found but is not required.
|
<A extends Module> |
optionalModules(java.lang.Class<A> cls,
java.util.Collection<Module> dependencies) |
java.util.Collection<Module> |
parseXMLConfigElement(org.w3c.dom.Node doc,
java.lang.String source)
Parses an XML element as if it was the root element of
a full configuration file.
|
java.util.Collection<Module> |
parseXMLInclude(org.w3c.dom.Element e)
Parses an handles include element.
|
Tuples.T3<Module,java.util.HashMap<java.lang.String,java.lang.Object>,ModuleManager.ModuleSettings> |
parseXMLModuleElement(org.w3c.dom.Element e,
java.lang.String source)
Reads a module element and returns the module, its module
parameters and module settings.
|
static ModuleManager.ModuleSettings |
parseXMLModuleSettings(org.w3c.dom.Element e,
java.lang.String source)
Reads module settings from a module element.
|
java.util.HashMap<java.lang.String,java.lang.Object> |
parseXMLOptionsElement(org.w3c.dom.Element e)
Reads all the options inside the module element.
|
java.lang.Object |
parseXMLParamElement(org.w3c.dom.Element e)
Parses a single param element and returns its value.
|
void |
parseXMLVariable(org.w3c.dom.Element e,
java.util.HashMap<java.lang.String,java.lang.String> variables)
Parses a variable element and defines that variable for future use.
|
java.util.Collection<Module> |
readXMLOptionsFile(java.lang.String optionsFileName)
Reads and parses a whole xml configuration file.
|
void |
removeModule(Module module)
Removes a module from the manager.
|
void |
removeModuleListener(ModuleListener l)
Removes a previously registered module listener.
|
java.lang.String |
replaceVariables(java.lang.String value,
java.util.HashMap<java.lang.String,java.lang.String> variables)
Performs variable replacement on a string.
|
<A extends Module> |
requireModule(java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Find a required module and throw a MissingDependencyException if
one cannot be found.
|
<A extends Module> |
requireModule(Module context,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Find a required module and throw a MissingDependencyException if
one cannot be found.
|
<A extends Module> |
requireModule(Module context,
java.lang.String instanceName,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Find a required module and throw a MissingDependencyException if
one cannot be found.
|
<A extends Module> |
requireModule(java.lang.String instanceName,
java.lang.Class<A> cls,
java.util.Collection<Module> dependencies)
Find a required module and throw a MissingDependencyException if
one cannot be found.
|
void |
setLogging(org.apache.commons.logging.Log log)
Sets the logging for the module manager itself.
|
void |
setVariable(java.lang.String key,
java.lang.String value)
Sets a variable.
|
void |
startAllModules()
Starts all modules that have not yet been started.
|
void |
startModule(Module module)
Starts a module.
|
void |
startModuleWithDependencies(Module module)
Starts a module as well as all modules it depends on.
|
void |
stopAllModules()
Stops all running modules.
|
void |
stopCascading(Module module)
Stops a single module along with all modules that depend on it.
|
void |
stopModule(Module module)
Stops a single module, but fails if other modules depend on it.
|
protected java.util.ArrayList<Module> modules
protected java.util.HashMap<Module,java.util.HashMap<java.lang.String,java.lang.Object>> moduleParams
protected java.util.HashMap<Module,ModuleManager.ModuleSettings> moduleSettings
protected org.apache.commons.logging.Log log
protected ListenerList<ModuleListener> moduleListeners
protected java.util.HashMap<Module,java.util.ArrayList<Module>> isRequiredBy
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> variables
private static javax.xml.xpath.XPath xpath
public ModuleManager.ModuleSettings getModuleSettings(Module m)
public boolean isModuleAutoStart(Module m)
public java.lang.String getModuleName(Module m)
public void addModuleListener(ModuleListener l)
public void removeModuleListener(ModuleListener l)
public void setLogging(org.apache.commons.logging.Log log)
public org.apache.commons.logging.Log getLogger()
public java.util.ArrayList<Module> getAllModules()
public <A extends Module> A findModule(java.lang.Class<A> cls)
cls
- The class or interface that the module has to implement or extend or
be a direct instance of.public <A extends Module> A findModule(Module context, java.lang.Class<A> cls)
context
- The module which is requesting the module. This may affect
what module is returned.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.public <A extends Module> A findModule(Module context, java.lang.String instanceName, java.lang.Class<A> cls)
context
- The module which is requesting the module. This may affect
what module is returned.instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.public <A extends Module> A findModule(java.lang.String instanceName, java.lang.Class<A> cls)
instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.public <A extends Module> java.util.ArrayList<A> findModules(java.lang.Class<A> cls)
cls
- The class or interface that the module has to implement or extend or
be a direct instance of.public <A extends Module> A requireModule(java.lang.Class<A> cls, java.util.Collection<Module> dependencies) throws MissingDependencyException
cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.MissingDependencyException
public <A extends Module> A requireModule(Module context, java.lang.Class<A> cls, java.util.Collection<Module> dependencies) throws MissingDependencyException
context
- The module which is requesting the module. This may affect
what module is returned.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.MissingDependencyException
public <A extends Module> A requireModule(Module context, java.lang.String instanceName, java.lang.Class<A> cls, java.util.Collection<Module> dependencies) throws MissingDependencyException
context
- The module which is requesting the module. This may affect
what module is returned.instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.MissingDependencyException
public <A extends Module> A requireModule(java.lang.String instanceName, java.lang.Class<A> cls, java.util.Collection<Module> dependencies) throws MissingDependencyException
instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.MissingDependencyException
public <A extends Module> A optionalModule(java.lang.Class<A> cls, java.util.Collection<Module> dependencies)
cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.public <A extends Module> A optionalModule(Module context, java.lang.Class<A> cls, java.util.Collection<Module> dependencies)
context
- The module which is requesting the module. This may affect
what module is returned.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.public <A extends Module> A optionalModule(Module context, java.lang.String instanceName, java.lang.Class<A> cls, java.util.Collection<Module> dependencies)
context
- The module which is requesting the module. This may affect
what module is returned.instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.public <A extends Module> A optionalModule(java.lang.String instanceName, java.lang.Class<A> cls, java.util.Collection<Module> dependencies)
instanceName
- The instance name of the requested module.cls
- The class or interface that the module has to implement or extend or
be a direct instance of.dependencies
- The collection where all dependencies are gathered and
where the found module is added.public <A extends Module> java.util.ArrayList<A> optionalModules(java.lang.Class<A> cls, java.util.Collection<Module> dependencies)
public void addModule(Module module)
module
- The module to be added.public void addModule(Module module, java.lang.String name)
module
- The module to be added.name
- The module name;public void addModule(Module module, ModuleManager.ModuleSettings settings)
module
- The module to be added.settings
- The settings for the module.public void addModule(Module module, java.util.HashMap<java.lang.String,java.lang.Object> params)
module
- The module to be added.params
- The module parameters.public void addModule(Module module, java.util.HashMap<java.lang.String,java.lang.Object> params, ModuleManager.ModuleSettings settings)
module
- The module to be added.params
- The module parameters.settings
- The module settings.public void removeModule(Module module) throws ModuleException
module
- The module to be removed.ModuleInUseException
ModuleException
public java.lang.String moduleToString(java.lang.Class<?> cls, java.lang.String instanceName)
cls
- The class of the module.instanceName
- The instance name of the module or null.public java.lang.String moduleToString(Module module)
module
- The module.public void initAllModules() throws ModuleException
ModuleException
public void autostartModules() throws ModuleException
ModuleException
public void autostartModules(java.util.Collection<Module> modules) throws ModuleException
ModuleException
public void startAllModules() throws ModuleException
ModuleException
public void stopAllModules()
public void stopCascading(Module module) throws ModuleException
module
- The module to be stopped.ModuleException
public void stopModule(Module module) throws ModuleException
module
- The module to be stopped.ModuleException
public void startModuleWithDependencies(Module module) throws ModuleException
module
- The module to be started.ModuleException
public void startModule(Module module) throws ModuleException
module
- The module to be started.ModuleException
public java.lang.Object parseXMLParamElement(org.w3c.dom.Element e) throws java.lang.ReflectiveOperationException, java.lang.IllegalArgumentException, javax.script.ScriptException
e
- The xml param element.java.lang.ReflectiveOperationException
java.lang.IllegalArgumentException
javax.script.ScriptException
public java.util.HashMap<java.lang.String,java.lang.Object> parseXMLOptionsElement(org.w3c.dom.Element e) throws java.lang.ReflectiveOperationException, javax.script.ScriptException
e
- The module element contents of which are to be processed.java.lang.ReflectiveOperationException
javax.script.ScriptException
public static ModuleManager.ModuleSettings parseXMLModuleSettings(org.w3c.dom.Element e, java.lang.String source) throws java.lang.ClassNotFoundException
e
- The module element.source
- The source identifier for the module, could be the file name
where the module comes from or something else.java.lang.ClassNotFoundException
public Tuples.T3<Module,java.util.HashMap<java.lang.String,java.lang.Object>,ModuleManager.ModuleSettings> parseXMLModuleElement(org.w3c.dom.Element e, java.lang.String source) throws java.lang.ReflectiveOperationException, javax.script.ScriptException
e
- The module element.source
- The source identifier for the module, could be the file name
where the module comes from or something else.java.lang.ReflectiveOperationException
javax.script.ScriptException
public java.lang.String getVariable(java.lang.String key)
key
- The name of the variable.public java.lang.String replaceVariables(java.lang.String value, java.util.HashMap<java.lang.String,java.lang.String> variables)
value
- The string in which variable replacement will be done.variables
- A map containing all the defined variables.public void setVariable(java.lang.String key, java.lang.String value)
key
- The name of the variable to set.value
- The value of the variable or null to remove the definition.public void parseXMLVariable(org.w3c.dom.Element e, java.util.HashMap<java.lang.String,java.lang.String> variables)
e
- The variable element.variables
- The variable map into which the variable is defined.public java.util.Collection<Module> parseXMLInclude(org.w3c.dom.Element e)
e
- The include element.public java.util.Collection<Module> parseXMLConfigElement(org.w3c.dom.Node doc, java.lang.String source)
doc
- The element which contains the config definitions.source
- The source identifier, e.g. file name, where this element
came from.public java.util.Collection<Module> readXMLOptionsFile(java.lang.String optionsFileName)
optionsFileName
- Copyright 2004-2015 Wandora Team