public class ScriptModule extends AbstractModule
A base module implementation that adds some scripting functionality into the module. Most of the other modules derive from this instead of from AbstractModule directly, and it may be a good idea for you to do so as well, even if you don't immediately need any of the scripting functions. By deriving from ScriptModule, you can later add scripts in the configuration file that are executed when your module is initialised, started or stopped.
To add scripts in the configuration file, define them in parameters with names startScript, stopScript or initScript. The scripts will then be ran at module start, stop or init, respectively. You may also specify the script engine to use with scriptEngine parameter, otherwise the default engine will be used, which is probably Mozilla Rhino ECMA Script.
Some variables are added to the script's environment before it's executed. moduleManager will contain the module manager, scriptModule contains this module object and persistentObjects contains a HashMap where you can store any other objects you would like to refer to later. The persistentObjects is created at module init and will not be cleared during the lifetime of the module. However, the objects stored in there are not saved to disk or any other persistent storage, they are persistent only during the lifetime of the module object.
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
engine |
protected java.lang.String |
initScript |
protected java.util.HashMap<java.lang.String,java.lang.Object> |
persistentObjects |
protected ScriptManager |
scriptManager |
protected java.lang.String |
startScript |
protected java.lang.String |
stopScript |
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
ScriptModule() |
Modifier and Type | Method and Description |
---|---|
protected javax.script.ScriptEngine |
getScriptEngine() |
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
getDependencies, isInitialized, isRunning, requireLogging, toString
protected java.lang.String engine
protected java.lang.String startScript
protected java.lang.String stopScript
protected java.lang.String initScript
protected ScriptManager scriptManager
protected java.util.HashMap<java.lang.String,java.lang.Object> persistentObjects
public void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
Module
Initialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init
in interface Module
init
in class AbstractModule
manager
- The module manager handling this module. You may keep a
reference to it if needed.ModuleException
protected javax.script.ScriptEngine getScriptEngine()
public void start(ModuleManager manager) throws ModuleException
Module
start
in interface Module
start
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class AbstractModule
manager
- The module manager handling this module.Copyright 2004-2015 Wandora Team