FileDocCategorySizeDatePackage
JavaxScriptRunner.javaAPI DocApache Ant 1.704366Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.util.optional

JavaxScriptRunner

public class JavaxScriptRunner extends org.apache.tools.ant.util.ScriptRunnerBase
This class is used to run scripts using JSR 223.
since
Ant 1.7.0

Fields Summary
private org.apache.tools.ant.util.ReflectWrapper
engine
Constructors Summary
Methods Summary
private org.apache.tools.ant.util.ReflectWrappercreateEngine()

        if (engine != null) {
            return engine;
        }
        ReflectWrapper manager = new ReflectWrapper(
            getClass().getClassLoader(), "javax.script.ScriptEngineManager");
        Object e = manager.invoke(
            "getEngineByName", String.class, getLanguage());
        if (e == null) {
            return null;
        }
        ReflectWrapper ret = new ReflectWrapper(e);
        if (getKeepEngine()) {
            this.engine = ret;
        }
        return ret;
    
public java.lang.ObjectevaluateScript(java.lang.String execName)
Do the work to eval the script.

param
execName the name that will be passed to the scripting engine for this script execution.
return
the result of the evalulation
exception
BuildException if someting goes wrong exectuing the script.

        checkLanguage();
        ClassLoader origLoader = replaceContextLoader();
        try {
            ReflectWrapper engine = createEngine();
            if (engine == null) {
                throw new BuildException(
                    "Unable to create javax script engine for "
                    + getLanguage());
            }
            for (Iterator i = getBeans().keySet().iterator(); i.hasNext();) {
                String key = (String) i.next();
                Object value = getBeans().get(key);
                engine.invoke(
                    "put", String.class, key, Object.class, value);
            }
            // execute the script
            return engine.invoke("eval", String.class, getScript());
        } catch (Exception be) {
            Throwable t = be;
            Throwable te = (Throwable) ReflectUtil.invoke(be, "getCause");
            if (te != null) {
                if  (te instanceof BuildException) {
                    throw (BuildException) te;
                } else {
                    t = te;
                }
            }
            throw new BuildException(t);
        } finally {
            restoreContextLoader(origLoader);
        }
    
public voidexecuteScript(java.lang.String execName)
Do the work to run the script.

param
execName the name that will be passed to the scripting engine for this script execution.
exception
BuildException if someting goes wrong exectuing the script.

        evaluateScript(execName);
    
public java.lang.StringgetManagerName()
Get the name of the manager prefix.

return
"javax"

        return "javax";
    
public booleansupportsLanguage()
{@inheritDoc}.

        if (engine != null) {
            return true;
        }
        checkLanguage();
        ClassLoader origLoader = replaceContextLoader();
        try {
            return createEngine() != null;
        } catch (Exception ex) {
            return false;
        } finally {
            restoreContextLoader(origLoader);
        }