FileDocCategorySizeDatePackage
ScriptRunnerBase.javaAPI DocApache Ant 1.708913Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.util

ScriptRunnerBase

public abstract class ScriptRunnerBase extends Object
This is a common abstract base case for script runners. These classes need to implement executeScript, evaluateScript and supportsLanguage.
since
Ant 1.7.0

Fields Summary
private boolean
keepEngine
Whether to keep the engine between calls to execute/eval
private String
language
Script language
private String
script
Script content
private org.apache.tools.ant.Project
project
Project this runner is used in
private ClassLoader
scriptLoader
Classloader to be used when running the script.
private Map
beans
Beans to be provided to the script
Constructors Summary
Methods Summary
public voidaddBean(java.lang.String key, java.lang.Object bean)
Add a single object into the script context.

param
key the name in the context this object is to stored under.
param
bean the object to be stored in the script context.

        boolean isValid = key.length() > 0
            && Character.isJavaIdentifierStart(key.charAt(0));

        for (int i = 1; isValid && i < key.length(); i++) {
            isValid = Character.isJavaIdentifierPart(key.charAt(i));
        }

        if (isValid) {
            beans.put(key, bean);
        }
    
public voidaddBeans(java.util.Map dictionary)
Add a list of named objects to the list to be exported to the script

param
dictionary a map of objects to be placed into the script context indexed by String names.


                                                
        
        for (Iterator i = dictionary.keySet().iterator(); i.hasNext();) {
            String key = (String) i.next();
            try {
                Object val = dictionary.get(key);
                addBean(key, val);
            } catch (BuildException ex) {
                // The key is in the dictionary but cannot be retrieved
                // This is usually due references that refer to tasks
                // that have not been taskdefed in the current run.
                // Ignore
            }
        }
    
public voidaddText(java.lang.String text)
Set the script text.

param
text a component of the script text to be added.

        this.script += text;
    
public voidbindToComponent(org.apache.tools.ant.ProjectComponent component)
Bind the runner to a project component. Properties, targets and references are all added as beans; project is bound to project, and self to the component.

param
component to become self

        project = component.getProject();
        addBeans(project.getProperties());
        addBeans(project.getUserProperties());
        addBeans(project.getTargets());
        addBeans(project.getReferences());
        addBean("project", project);
        addBean("self", component);
    
public voidbindToComponentMinimum(org.apache.tools.ant.ProjectComponent component)
Bind the runner to a project component. The project and self are the only beans set.

param
component to become self

        project = component.getProject();
        addBean("project", project);
        addBean("self", component);
    
protected voidcheckLanguage()
Check if the language attribute is set.

throws
BuildException if it is not.

        if (language == null) {
            throw new BuildException(
                "script language must be specified");
        }
    
public voidclearScript()
Clear the current script text content.

        this.script = "";
    
public abstract java.lang.ObjectevaluateScript(java.lang.String execName)
Evalulate the script.

param
execName the name that will be passed to BSF for this script execution.
return
the result of evalulating the script.

public abstract voidexecuteScript(java.lang.String execName)
Do the work.

param
execName the name that will be passed to BSF for this script execution.

protected java.util.MapgetBeans()
Get the beans used for the script.

return
the map of beans.

        return beans;
    
public booleangetKeepEngine()
Get the keep engine attribute.

return
the attribute.

        return keepEngine;
    
public java.lang.StringgetLanguage()
Get the script language

return
the script language

        return language;
    
public abstract java.lang.StringgetManagerName()
Get the name of the manager prefix used for this scriptrunner.

return
the prefix string.

public org.apache.tools.ant.ProjectgetProject()
Get the project for this runner.

return
the project.

        return project;
    
public java.lang.StringgetScript()
Get the current script text content.

return
the script text.

        return script;
    
protected java.lang.ClassLoadergetScriptClassLoader()
Get the classloader used to load the script engine.

return
the classloader.

        return scriptLoader;
    
protected java.lang.ClassLoaderreplaceContextLoader()
Replace the current context classloader with the script context classloader.

return
the current context classloader.

        ClassLoader origContextClassLoader =
            Thread.currentThread().getContextClassLoader();
        if (getScriptClassLoader() == null) {
            setScriptClassLoader(getClass().getClassLoader());
        }
        Thread.currentThread().setContextClassLoader(getScriptClassLoader());
        return origContextClassLoader;
    
protected voidrestoreContextLoader(java.lang.ClassLoader origLoader)
Restore the context loader with the original context classloader. script context loader.

param
origLoader the original context classloader.

        Thread.currentThread().setContextClassLoader(
                 origLoader);
    
public voidsetKeepEngine(boolean keepEngine)
Whether to keep the script engine between calls.

param
keepEngine if true, keep the engine.

        this.keepEngine = keepEngine;
    
public voidsetLanguage(java.lang.String language)
Defines the language (required).

param
language the scripting language name for the script.

        this.language = language;
    
public voidsetProject(org.apache.tools.ant.Project project)
Set the project for this runner.

param
project the project.

        this.project = project;
    
public voidsetScriptClassLoader(java.lang.ClassLoader classLoader)
Set the script classloader.

param
classLoader the classloader to use.

        this.scriptLoader = classLoader;
    
public voidsetSrc(java.io.File file)
Load the script from an external file; optional.

param
file the file containing the script source.

        if (!file.exists()) {
            throw new BuildException("file " + file.getPath() + " not found.");
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            script += FileUtils.readFully(in);
        } catch (IOException ex) {
            throw new BuildException(ex);
        } finally {
            FileUtils.close(in);
        }
    
public abstract booleansupportsLanguage()
Check if a script engine can be created for this language.

return
true if a script engine can be created, false otherwise.