sleep.runtime
public class ScriptLoader extends Object
The ScriptLoader is a convienence container for instantiating and managing ScriptInstances.
ScriptLoader loader = new ScriptLoader(); ScriptInstance script = loader.loadScript("script.sl"); script.runScript();
The above will load the file script.sl and then execute it immediately.
Installation of loadable bridges you create can also be managed by the ScriptLoader.
A loadable bridge is installed into the language by adding it to a script loader class. There are two types of bridges. The two types are specific and global bridges.
The load and unload methods for a specific bridge are executed for every script load and unload, no matter what.
A global bridge is installed once for each script environment. If scripts are sharing an environment there is no sense in loading stuff into the environment more than once. This is why global bridges exist.
An example of adding a loadable bridge to a script loader:
ScriptLoader loader = new ScriptLoader() loader.addSpecificBridge(new MyLoadableBridge());
This class contains several methods to either load or compile a script. Loading a script instantiates a script environment, registers the script with the script loader, and registers all of the appropriate bridges with the script on top of compiling the script.
To compile a script means to produce a runnable Block of code. On its own a Block is not really runnable as a script environment is needed. For functions like eval(), include(), etc.. it makes sense to compile a script as one may want to run the block of code within the environment of the calling script. Using the compile method saves on the overhead of unnecessary script environment creation and bridge registration.
Hopefully this helped to clarify things. :)
See Also: Loadable ScriptInstance
Field Summary | |
---|---|
protected LinkedList | bridgesg
global bridges |
protected LinkedList | bridgess
specific bridges |
protected static HashMap | BLOCK_CACHE
cache for parsed scripts mantained (optionally) by the script loader. |
protected boolean | disableConversions |
protected LinkedList | loadedScripts
loaded scripts |
protected LinkedList | paths
path to search for jar files imported using [import * from: *] syntax |
protected HashMap | scripts
loaded scripts except referable by key |
Constructor Summary | |
---|---|
ScriptLoader()
initializes the script loader |
Method Summary | |
---|---|
void | addGlobalBridge(Loadable l)
A global bridge is loaded into an environment once and only once. |
void | addSpecificBridge(Loadable l)
A specific bridge is loaded into *every* script regardless of wether or not the environment is shared. |
Block | compileScript(String name, InputStream stream) compiles a script using the specified stream as a source |
Block | compileScript(File file)
compiles the specified script file |
Block | compileScript(String fileName)
compiles the specified script file |
Block | compileScript(String name, String code) compiles the specified script into a runnable block |
String | getCharset() |
ScriptEnvironment | getFirstScriptEnvironment()
Convienence method to return the script environment of the first script tht was loaded, returns null if no scripts are loaded |
LinkedList | getScripts()
Returns a linked list of all loaded ScriptInstance objects |
HashMap | getScriptsByKey()
Returns a HashMap with all loaded scripts, the key is a string which is just the filename, the value is a ScriptInstance
object |
Set | getScriptsToLoad(Set configured)
A convienence method to determine the set of scripts to "load" based on a passed in set of scripts that are currently
configured. |
Set | getScriptsToUnload(Set configured)
A convienence method to determine the set of scripts to "unload" based on a passed in set of scripts that are currently
configured. |
protected void | initDefaultBridges()
method call to initialize the default bridges, if you want to change the default bridges subclass this class and
override this method |
boolean | isCharsetConversions() |
boolean | isLoaded(String name)
Determines wether or not the script is loaded by checking if the specified key exists in the script db. |
ScriptInstance | loadScript(String name, Block code, Hashtable env) |
ScriptInstance | loadScript(String name, String code, Hashtable env) loads the specified script |
ScriptInstance | loadScript(String name, InputStream stream) loads a script from the specified inputstream |
ScriptInstance | loadScript(String name, InputStream stream, Hashtable env) loads a script from the specified input stream using the specified hashtable as a shared environment |
ScriptInstance | loadScript(String fileName)
Loads the specified script file |
ScriptInstance | loadScript(String fileName, Hashtable env)
Loads the specified script file, uses the specified hashtable for the environment |
ScriptInstance | loadScript(File file, Hashtable env)
Loads the specified script file, uses the specified hashtable for the environment |
ScriptInstance | loadScript(File file)
Loads the specified script file |
ScriptInstance | loadSerialized(File script, Hashtable env)
Load a serialized version of the script iff a serialized version exists, and its modification time is greater than the
modification time of the script. |
ScriptInstance | loadSerialized(String name, InputStream stream, Hashtable env)
Loads a serialized script from the specified input stream with the specified name |
static void | saveSerialized(ScriptInstance si)
Saves a serialized version of the compiled script to scriptname.bin. |
static void | saveSerialized(ScriptInstance si, OutputStream stream)
Saves a serialized version of the ScriptInstance si to the specified output stream |
void | setCharset(String charset)
If charset conversion is enabled and charset is set, then the stream will be read using specified charset.
|
void | setCharsetConversion(boolean b)
Java by default maps characters from an 8bit ascii file to an internal 32bit unicode representation. |
HashMap | setGlobalCache(boolean setting)
The Sleep script loader can optionally cache parsed script files once they are loaded. |
void | unloadScript(String filename)
unload a script |
void | unloadScript(ScriptInstance script)
unload a script |
Parameters: script a file object pointing to the script file...
Parameters: charset The name of a supported java.nio.charset.Charset
charset