sleep.runtime
Class ScriptLoader

java.lang.Object
  extended bysleep.runtime.ScriptLoader

public class ScriptLoader
extends Object

The ScriptLoader is a convienence container for instantiating and managing ScriptInstances.

To load a script from a file and run it:

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());
 

See Also:
Loadable, ScriptInstance

Field Summary
protected static HashMap BLOCK_CACHE
          cache for parsed scripts mantained (optionally) by the script loader.
protected  LinkedList bridgesg
          global bridges
protected  LinkedList bridgess
          specific bridges
protected  boolean disableConversions
           
protected  LinkedList loadedScripts
          loaded scripts
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.
 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
 ScriptInstance loadScript(File file)
          Loads the specified script file
 ScriptInstance loadScript(File file, Hashtable env)
          Loads the specified script file, uses the specified hashtable for the environment
 ScriptInstance loadScript(String fileName)
          Loads the specified script file
 ScriptInstance loadScript(String name, Block code, Hashtable env)
           
 ScriptInstance loadScript(String fileName, Hashtable env)
          Loads the specified script file, uses the specified hashtable for the environment
 ScriptInstance loadScript(String name, InputStream stream)
           
 ScriptInstance loadScript(String name, InputStream stream, Hashtable env)
           
 ScriptInstance loadScript(String name, String code, Hashtable env)
           
 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 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(ScriptInstance script)
          unload a script
 void unloadScript(String filename)
          unload a script
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_CACHE

protected static HashMap BLOCK_CACHE
cache for parsed scripts mantained (optionally) by the script loader.


loadedScripts

protected LinkedList loadedScripts
loaded scripts


scripts

protected HashMap scripts
loaded scripts except referable by key


bridgesg

protected LinkedList bridgesg
global bridges


bridgess

protected LinkedList bridgess
specific bridges


disableConversions

protected boolean disableConversions
Constructor Detail

ScriptLoader

public ScriptLoader()
initializes the script loader

Method Detail

setGlobalCache

public HashMap setGlobalCache(boolean setting)
The Sleep script loader can optionally cache parsed script files once they are loaded. This is useful if you will have several script loader instances loading the same script files in isolated objects.


initDefaultBridges

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


addGlobalBridge

public void addGlobalBridge(Loadable l)
A global bridge is loaded into an environment once and only once. This way if the environment is shared among multiple script instances this will save on both memory and script load time


addSpecificBridge

public void addSpecificBridge(Loadable l)
A specific bridge is loaded into *every* script regardless of wether or not the environment is shared. Useful for modifying the script instance while it is being in processed. Specific bridges are the first thing that happens after the script code is parsed


getScriptsByKey

public 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


getFirstScriptEnvironment

public ScriptEnvironment getFirstScriptEnvironment()
Convienence method to return the script environment of the first script tht was loaded, returns null if no scripts are loaded


getScripts

public LinkedList getScripts()
Returns a linked list of all loaded ScriptInstance objects


loadSerialized

public ScriptInstance loadSerialized(File script,
                                     Hashtable env)
                              throws IOException,
                                     ClassNotFoundException
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. Also handles the muss and fuss of reserializing the script if it has to reload the script. Personally I didn't find much of a startup time decrease when loading the scripts serialized versus parsing them each time. Theres a command 'bload' in the console to benchmark loading a script normally versus serialized. Try it.

Parameters:
script - a file object pointing to the script file...
Throws:
IOException
ClassNotFoundException

loadSerialized

public ScriptInstance loadSerialized(String name,
                                     InputStream stream,
                                     Hashtable env)
                              throws IOException,
                                     ClassNotFoundException
Loads a serialized script from the specified input stream with the specified name

Throws:
IOException
ClassNotFoundException

saveSerialized

public static void saveSerialized(ScriptInstance si)
                           throws IOException
Saves a serialized version of the compiled script to scriptname.bin.

Throws:
IOException

saveSerialized

public static void saveSerialized(ScriptInstance si,
                                  OutputStream stream)
                           throws IOException
Saves a serialized version of the ScriptInstance si to the specified output stream

Throws:
IOException

loadScript

public ScriptInstance loadScript(String name,
                                 Block code,
                                 Hashtable env)

loadScript

public ScriptInstance loadScript(String name,
                                 String code,
                                 Hashtable env)
                          throws YourCodeSucksException
Throws:
YourCodeSucksException

loadScript

public ScriptInstance loadScript(String name,
                                 InputStream stream)
                          throws YourCodeSucksException,
                                 IOException
Throws:
YourCodeSucksException
IOException

loadScript

public ScriptInstance loadScript(String name,
                                 InputStream stream,
                                 Hashtable env)
                          throws YourCodeSucksException,
                                 IOException
Throws:
YourCodeSucksException
IOException

loadScript

public ScriptInstance loadScript(String fileName)
                          throws IOException,
                                 YourCodeSucksException
Loads the specified script file

Throws:
IOException
YourCodeSucksException

loadScript

public ScriptInstance loadScript(String fileName,
                                 Hashtable env)
                          throws IOException,
                                 YourCodeSucksException
Loads the specified script file, uses the specified hashtable for the environment

Throws:
IOException
YourCodeSucksException

loadScript

public ScriptInstance loadScript(File file,
                                 Hashtable env)
                          throws IOException,
                                 YourCodeSucksException
Loads the specified script file, uses the specified hashtable for the environment

Throws:
IOException
YourCodeSucksException

loadScript

public ScriptInstance loadScript(File file)
                          throws IOException,
                                 YourCodeSucksException
Loads the specified script file

Throws:
IOException
YourCodeSucksException

unloadScript

public void unloadScript(String filename)
unload a script


unloadScript

public void unloadScript(ScriptInstance script)
unload a script


getScriptsToUnload

public 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. The configured scripts are compared to the loaded scripts. Scripts that are loaded but not configured are determined to be in need of unloading. The return Set contains String objects of the script names. The passed in Set is expected to be the same thing (a bunch of Strings).


getScriptsToLoad

public 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. The configured scripts are compared to the loaded scripts. Scripts that are configured but not loaded are determined to be in need of loading. The return Set contains String objects of the script names. The passed in Set is expected to be the same thing (a bunch of Strings).


setCharsetConversion

public void setCharsetConversion(boolean b)
Java by default maps characters from an 8bit ascii file to an internal 32bit unicode representation. How this mapping is done is called a character set encoding. Sometimes this conversion can frustrate scripters making them say "hey, I didn't put that character in my script". You can use this option to ensure sleep disables charset conversions for scripts loaded with this script loader