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

ScriptRunner

public class ScriptRunner extends org.apache.tools.ant.util.ScriptRunnerBase
This class is used to run BSF scripts

Fields Summary
private org.apache.bsf.BSFEngine
engine
private org.apache.bsf.BSFManager
manager
Constructors Summary
Methods Summary
private org.apache.bsf.BSFManagercreateManager()

        if (manager != null) {
            return manager;
        }
        BSFManager m = new BSFManager();
        m.setClassLoader(getScriptClassLoader());
        if (getKeepEngine()) {
            BSFEngine e = manager.loadScriptingEngine(getLanguage());
            this.manager = m;
            this.engine  = e;
        }
        return m;
    
private voiddeclareBeans(org.apache.bsf.BSFManager m)

        for (Iterator i = getBeans().keySet().iterator(); i.hasNext();) {
            String key = (String) i.next();
            Object value = getBeans().get(key);
            if (value != null) {
                m.declareBean(key, value, value.getClass());
            } else {
                // BSF uses a hashtable to store values
                // so cannot declareBean with a null value
                // So need to remove any bean of this name as
                // that bean should not be visible
                m.undeclareBean(key);
            }
        }
    
public java.lang.ObjectevaluateScript(java.lang.String execName)
Do the work.

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

        checkLanguage();
        ClassLoader origLoader = replaceContextLoader();
        try {
            BSFManager m = createManager();
            declareBeans(m);
            // execute the script
            if (engine == null) {
                return m.eval(getLanguage(), execName, 0, 0, getScript());
            } else {
                return engine.eval(execName, 0, 0, getScript());
            }
        } catch (BSFException be) {
            throwBuildException(be);
            // NotReached
            return null;
        } finally {
            restoreContextLoader(origLoader);
        }
    
public voidexecuteScript(java.lang.String execName)
Do the work.

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

        checkLanguage();
        ClassLoader origLoader = replaceContextLoader();
        try {
            BSFManager m = createManager();
            declareBeans(m);
            // execute the script
            if (engine == null) {
                m.exec(getLanguage(), execName, 0, 0, getScript());
            } else {
                engine.exec(execName, 0, 0, getScript());
            }
        } catch (BSFException be) {
            throwBuildException(be);
        } finally {
            restoreContextLoader(origLoader);
        }
    
public java.lang.StringgetManagerName()
Get the name of the manager prefix.

return
"bsf"

        return "bsf";
    
public booleansupportsLanguage()
Check if bsf supports the language.

return
true if bsf can create an engine for this language.

        Hashtable table = (Hashtable) ReflectUtil.getField(
            new BSFManager(), "registeredEngines");
        String engineClassName = (String) table.get(getLanguage());
        if (engineClassName == null) {
            getProject().log(
                "This is no BSF engine class for language '"
                + getLanguage() + "'",
                Project.MSG_VERBOSE);
            return false;
        }
        try {
            getScriptClassLoader().loadClass(engineClassName);
            return true;
        } catch (Throwable ex) {
            getProject().log(
                "unable to create BSF engine class for language '"
                + getLanguage() + "'",
                ex,
                Project.MSG_VERBOSE);
            return false;
        }
    
private voidthrowBuildException(org.apache.bsf.BSFException be)
Throw a buildException in place of a BSFException.

param
be BSFException to convert.
throws
BuildException the conveted exception.

        Throwable t = be;
        Throwable te = be.getTargetException();
        if (te != null) {
            if  (te instanceof BuildException) {
                throw (BuildException) te;
            } else {
                t = te;
            }
        }
        throw new BuildException(t);