org.schmant.app.js
Class SunJavaScriptExecutor

java.lang.Object
  extended by org.schmant.app.js.SunJavaScriptExecutor
All Implemented Interfaces:
ClosureRunner, ThreadContextInitializer, ScriptExecutor, FlatteningListDecorator

public final class SunJavaScriptExecutor
extends Object
implements ScriptExecutor, FlatteningListDecorator, ThreadContextInitializer, ClosureRunner

This script executor runs JavaScript build scripts using the JavaScript support bundled in Sun JDK 6+.

Since:
1.1
Author:
Karl Gustafsson

Constructor Summary
SunJavaScriptExecutor()
           
 
Method Summary
 void init(NamedReadableFile scriptFile, LauncherSettings ls, Report r)
          Initialize the script executor.
 boolean isClosure(Object o)
          Is the object a closure that can be run?
 Runnable proxyRunnable(Runnable r)
          Proxy the Runnable that the thread is configured to run with a runnable that sets up the thread context.
 Object runClosure(Object closure, Object... arguments)
          Run the closure and return the result.
 void runDefaultPreparationScript(Report r)
          Run the default environment preparation script.
 void runPreparationScript(File script, Report r)
          Run the preparation script in the file.
 Object runScriptFromFile(Object f)
           
 Object runWithDefaultWrapperScript()
          Run the script using the default wrapper script, if any.
 void setBindings(Bindings bnd, int scope)
          Set the variables stored in the bindings in the specified scope.
 void setGlobalVariable(String name, Object value)
          Set the value of a global variable in the script.
 void tearDown()
          Tear down the script executor when Schmant terminates.
<T> int
tryAdd(FlatteningList<T> list, int index, Object o, ObjectTransformer<T> transformer)
          If o is an object that this decorator handles, add it to the list.
<T> int
tryAdd(FlatteningList<T> list, int index, T o)
          If o is an object that this decorator handles, add it to the list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SunJavaScriptExecutor

public SunJavaScriptExecutor()
Method Detail

init

public void init(NamedReadableFile scriptFile,
                 LauncherSettings ls,
                 Report r)
Description copied from interface: ScriptExecutor
Initialize the script executor. This method is responsible for registering the script executor with different Schmant extension points such as FlatteningList.registerDecorator(org.schmant.support.util.FlatteningListDecorator), TaskExecutorThreadFactory.registerThreadContextInitializer(org.schmant.run.ThreadContextInitializer) and ClosureSupport.registerClosureRunner(org.schmant.lang.ClosureRunner).

Specified by:
init in interface ScriptExecutor
Parameters:
scriptFile - The script file that is run.
ls - The command line settings.
r - The report.
See Also:
ScriptExecutor.tearDown()

tearDown

public void tearDown()
Description copied from interface: ScriptExecutor
Tear down the script executor when Schmant terminates. This method should deregister the script executor from the extension points that ScriptExecutor.init(NamedReadableFile, LauncherSettings, Report) registered with.

Specified by:
tearDown in interface ScriptExecutor

runScriptFromFile

public Object runScriptFromFile(Object f)
                         throws IOException
Throws:
IOException

runDefaultPreparationScript

public void runDefaultPreparationScript(Report r)
                                 throws IOException
Description copied from interface: ScriptExecutor
Run the default environment preparation script. This should register global functions that the build script may use.

Specified by:
runDefaultPreparationScript in interface ScriptExecutor
Parameters:
r - A report to log to.
Throws:
IOException

runPreparationScript

public void runPreparationScript(File script,
                                 Report r)
                          throws IOException
Description copied from interface: ScriptExecutor
Run the preparation script in the file.

Specified by:
runPreparationScript in interface ScriptExecutor
Parameters:
script - The script file.
r - A report to log to.
Throws:
IOException

runWithDefaultWrapperScript

public Object runWithDefaultWrapperScript()
                                   throws IOException
Description copied from interface: ScriptExecutor
Run the script using the default wrapper script, if any.

Specified by:
runWithDefaultWrapperScript in interface ScriptExecutor
Returns:
The value returned from the script.
Throws:
IOException

setBindings

public void setBindings(Bindings bnd,
                        int scope)
Description copied from interface: ScriptExecutor
Set the variables stored in the bindings in the specified scope. For now the scope is always global.

Specified by:
setBindings in interface ScriptExecutor
Parameters:
bnd - The bindings containing a map of variables to set.
scope - The scope to set the variables in. See ScriptContext. For now this parameter can be ignored and all variables set to the global scope.

setGlobalVariable

public void setGlobalVariable(String name,
                              Object value)
Description copied from interface: ScriptExecutor
Set the value of a global variable in the script.

Specified by:
setGlobalVariable in interface ScriptExecutor
Parameters:
name - The name of the variable.
value - The variable value.

tryAdd

public <T> int tryAdd(FlatteningList<T> list,
                      int index,
                      T o)
Description copied from interface: FlatteningListDecorator
If o is an object that this decorator handles, add it to the list.

Specified by:
tryAdd in interface FlatteningListDecorator
Type Parameters:
T - The type of objects in the list.
Parameters:
list - The list to add the object to.
index - The index for the new object in the list. If this argument is -1, add the object to the end of the list.
o - The object to add.
Returns:
The total number of objects added to the list. If this decorator does not handle the supplied object, this method should return -1.

tryAdd

public <T> int tryAdd(FlatteningList<T> list,
                      int index,
                      Object o,
                      ObjectTransformer<T> transformer)
Description copied from interface: FlatteningListDecorator
If o is an object that this decorator handles, add it to the list.

Specified by:
tryAdd in interface FlatteningListDecorator
Type Parameters:
T - The type of objects in the list.
Parameters:
list - The list to add the object to.
index - The index for the new object in the list. If this argument is -1, add the object to the end of the list.
o - The object to add.
transformer - The transformer used to transform the object into the list's type before it is added.
Returns:
The total number of objects added to the list. If this decorator does not handle the supplied object, this method should return -1.

proxyRunnable

public Runnable proxyRunnable(Runnable r)
Description copied from interface: ThreadContextInitializer
Proxy the Runnable that the thread is configured to run with a runnable that sets up the thread context.

Specified by:
proxyRunnable in interface ThreadContextInitializer
Parameters:
r - The runnable that the thread is configured to run.
Returns:
A runnable that proxies the supplied runnable in order to set up the thread context before that runnable is run.

isClosure

public boolean isClosure(Object o)
Description copied from interface: ClosureRunner
Is the object a closure that can be run?

Specified by:
isClosure in interface ClosureRunner
Parameters:
o - The object.
Returns:
true if the object is a closure.

runClosure

public Object runClosure(Object closure,
                         Object... arguments)
                  throws SchmantException
Description copied from interface: ClosureRunner
Run the closure and return the result.

Specified by:
runClosure in interface ClosureRunner
Parameters:
closure - The closure to run.
arguments - Arguments to the closure.
Returns:
The result from running the closure. This should be the expected value and not some kind of script language wrapper object, such as PyObject.
Throws:
SchmantException - On errors.