1 18 package org.apache.tools.ant.util; 19 20 import org.apache.tools.ant.BuildException; 21 import org.apache.tools.ant.Project; 22 23 28 public class ScriptRunnerCreator { 29 private static final String AUTO = "auto"; 30 private static final String OATAU = "org.apache.tools.ant.util"; 31 private static final String UTIL_OPT = OATAU + ".optional"; 32 33 private static final String BSF = "bsf"; 34 private static final String BSF_PACK = "org.apache.bsf"; 35 private static final String BSF_MANAGER = BSF_PACK + ".BSFManager"; 36 private static final String BSF_RUNNER = UTIL_OPT + ".ScriptRunner"; 37 38 private static final String JAVAX = "javax"; 39 private static final String JAVAX_MANAGER = "javax.script.ScriptEngineManager"; 40 private static final String JAVAX_RUNNER = UTIL_OPT + ".JavaxScriptRunner"; 41 42 private Project project; 43 private String manager; 44 private String language; 45 private ClassLoader scriptLoader = null; 46 47 51 public ScriptRunnerCreator(Project project) { 52 this.project = project; 53 } 54 55 63 public ScriptRunnerBase createRunner( 64 String manager, String language, ClassLoader classLoader) { 65 this.manager = manager; 66 this.language = language; 67 this.scriptLoader = classLoader; 68 69 if (language == null) { 70 throw new BuildException("script language must be specified"); 71 } 72 if (!manager.equals(AUTO) && !manager.equals(JAVAX) && !manager.equals(BSF)) { 73 throw new BuildException( 74 "Unsupported language prefix " + manager); 75 } 76 77 81 ScriptRunnerBase ret = null; 82 ret = createRunner(BSF, BSF_MANAGER, BSF_RUNNER); 83 if (ret == null) { 84 ret = createRunner(JAVAX, JAVAX_MANAGER, JAVAX_RUNNER); 85 } 86 if (ret != null) { 87 return ret; 88 } 89 if (JAVAX.equals(manager)) { 90 throw new BuildException( 91 "Unable to load the script engine manager " 92 + "(" + JAVAX_MANAGER + ")"); 93 } else if (BSF.equals(manager)) { 94 throw new BuildException( 95 "Unable to load the BSF script engine manager " 96 + "(" + BSF_MANAGER + ")"); 97 } else { 98 throw new BuildException( 99 "Unable to load a script engine manager " 100 + "(" + BSF_MANAGER + " or " + JAVAX_MANAGER + ")"); 101 } 102 } 103 104 115 private ScriptRunnerBase createRunner( 116 String checkManager, String managerClass, String runnerClass) { 117 ScriptRunnerBase runner = null; 118 if (!manager.equals(AUTO) && !manager.equals(checkManager)) { 119 return null; 120 } 121 if (scriptLoader.getResource( 122 LoaderUtils.classNameToResource(managerClass)) == null) { 123 return null; 124 } 125 try { 126 runner = (ScriptRunnerBase) Class.forName( 127 runnerClass, true, scriptLoader).newInstance(); 128 runner.setProject(project); 129 } catch (Exception ex) { 130 ReflectUtil.throwBuildException(ex); 131 } 133 134 runner.setLanguage(language); 135 runner.setScriptClassLoader(scriptLoader); 136 return runner; 137 } 138 } 139 | Popular Tags |