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

ScriptRunnerCreator

public class ScriptRunnerCreator extends Object
This is a helper class used by ScriptRunnerHelper to create a ScriptRunner based on a classloader and on a language.

Fields Summary
private static final String
AUTO
private static final String
OATAU
private static final String
UTIL_OPT
private static final String
BSF
private static final String
BSF_PACK
private static final String
BSF_MANAGER
private static final String
BSF_RUNNER
private static final String
JAVAX
private static final String
JAVAX_MANAGER
private static final String
JAVAX_RUNNER
private org.apache.tools.ant.Project
project
private String
manager
private String
language
private ClassLoader
scriptLoader
Constructors Summary
public ScriptRunnerCreator(org.apache.tools.ant.Project project)
Constructor for creator.

param
project the current project.


                 
       
        this.project = project;
    
Methods Summary
public ScriptRunnerBasecreateRunner(java.lang.String manager, java.lang.String language, java.lang.ClassLoader classLoader)
Create a ScriptRunner.

param
manager the script manager ("auto" | "bsf" | "javax")
param
language the language.
param
classLoader the classloader to use
return
the created script runner.
throws
BuildException if unable to create the ScriptRunner.

        this.manager      = manager;
        this.language     = language;
        this.scriptLoader = classLoader;

        if (language == null) {
            throw new BuildException("script language must be specified");
        }
        if (!manager.equals(AUTO) && !manager.equals(JAVAX) && !manager.equals(BSF)) {
                throw new BuildException(
                    "Unsupported language prefix " + manager);
        }

        // Check for bsf first then javax
        // This version does not check if the scriptManager
        // supports the language.

        ScriptRunnerBase ret = null;
        ret = createRunner(BSF, BSF_MANAGER, BSF_RUNNER);
        if (ret == null) {
            ret = createRunner(JAVAX, JAVAX_MANAGER, JAVAX_RUNNER);
        }
        if (ret != null) {
            return ret;
        }
        if (JAVAX.equals(manager)) {
            throw new BuildException(
                "Unable to load the script engine manager "
                + "(" + JAVAX_MANAGER + ")");
        } else if (BSF.equals(manager)) {
            throw new BuildException(
                "Unable to load the BSF script engine manager "
                + "(" + BSF_MANAGER + ")");
        } else {
            throw new BuildException(
                "Unable to load a script engine manager "
                + "(" + BSF_MANAGER + " or " + JAVAX_MANAGER + ")");
        }
    
private ScriptRunnerBasecreateRunner(java.lang.String checkManager, java.lang.String managerClass, java.lang.String runnerClass)
Create a script runner if the scriptManager matches the passed in manager. This checks if the script manager exists in the scriptLoader classloader and if so it creates and returns the script runner.

param
checkManager check if the manager matchs this value.
param
mangagerClass the name of the script manager class.
param
runnerClass the name of ant's script runner for this manager.
return
the script runner class.
throws
BuildException if there is a problem creating the runner class.

        ScriptRunnerBase runner = null;
        if (!manager.equals(AUTO) && !manager.equals(checkManager)) {
            return null;
        }
        if (scriptLoader.getResource(
                LoaderUtils.classNameToResource(managerClass)) == null) {
            return null;
        }
        try {
            runner = (ScriptRunnerBase) Class.forName(
                runnerClass, true, scriptLoader).newInstance();
            runner.setProject(project);
        } catch (Exception ex) {
            ReflectUtil.throwBuildException(ex);
            // NotReached
        }

        runner.setLanguage(language);
        runner.setScriptClassLoader(scriptLoader);
        return runner;