@@ -24,6 +24,7 @@ import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import org.apache.tools.ant.util.ScriptManager; | |||||
import org.apache.tools.ant.util.ScriptRunnerHelper; | import org.apache.tools.ant.util.ScriptRunnerHelper; | ||||
/** | /** | ||||
@@ -58,10 +59,19 @@ public class Script extends Task { | |||||
* | * | ||||
* @param manager the scripting manager. | * @param manager the scripting manager. | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
helper.setManager(manager); | helper.setManager(manager); | ||||
} | } | ||||
/** | |||||
* Set the script manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
helper.setManager(manager); | |||||
} | |||||
/** | /** | ||||
* Defines the language (required). | * Defines the language (required). | ||||
* | * | ||||
@@ -35,6 +35,7 @@ import org.apache.tools.ant.ProjectHelper; | |||||
import org.apache.tools.ant.taskdefs.DefBase; | import org.apache.tools.ant.taskdefs.DefBase; | ||||
import org.apache.tools.ant.types.ResourceCollection; | import org.apache.tools.ant.types.ResourceCollection; | ||||
import org.apache.tools.ant.util.ClasspathUtils; | import org.apache.tools.ant.util.ClasspathUtils; | ||||
import org.apache.tools.ant.util.ScriptManager; | |||||
import org.apache.tools.ant.util.ScriptRunnerBase; | import org.apache.tools.ant.util.ScriptRunnerBase; | ||||
import org.apache.tools.ant.util.ScriptRunnerHelper; | import org.apache.tools.ant.util.ScriptRunnerHelper; | ||||
@@ -353,10 +354,19 @@ public class ScriptDef extends DefBase { | |||||
* | * | ||||
* @param manager the scripting manager. | * @param manager the scripting manager. | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
helper.setManager(manager); | helper.setManager(manager); | ||||
} | } | ||||
/** | |||||
* Set the script manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
helper.setManager(manager); | |||||
} | |||||
/** | /** | ||||
* Defines the language (required). | * Defines the language (required). | ||||
* | * | ||||
@@ -23,6 +23,7 @@ import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.ProjectComponent; | import org.apache.tools.ant.ProjectComponent; | ||||
import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import org.apache.tools.ant.util.ScriptManager; | |||||
import org.apache.tools.ant.util.ScriptRunnerBase; | import org.apache.tools.ant.util.ScriptRunnerBase; | ||||
import org.apache.tools.ant.util.ScriptRunnerHelper; | import org.apache.tools.ant.util.ScriptRunnerHelper; | ||||
@@ -83,10 +84,19 @@ public abstract class AbstractScriptComponent extends ProjectComponent { | |||||
* | * | ||||
* @param manager the scripting manager. | * @param manager the scripting manager. | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
helper.setManager(manager); | helper.setManager(manager); | ||||
} | } | ||||
/** | |||||
* Set the script manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
helper.setManager(manager); | |||||
} | |||||
/** | /** | ||||
* Defines the language (required). | * Defines the language (required). | ||||
* | * | ||||
@@ -25,6 +25,7 @@ import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.filters.TokenFilter; | import org.apache.tools.ant.filters.TokenFilter; | ||||
import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import org.apache.tools.ant.util.ScriptManager; | |||||
import org.apache.tools.ant.util.ScriptRunnerBase; | import org.apache.tools.ant.util.ScriptRunnerBase; | ||||
import org.apache.tools.ant.util.ScriptRunnerHelper; | import org.apache.tools.ant.util.ScriptRunnerHelper; | ||||
@@ -137,9 +138,19 @@ public class ScriptFilter extends TokenFilter.ChainableReaderFilter { | |||||
* | * | ||||
* @param manager the scripting manager. | * @param manager the scripting manager. | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
helper.setManager(manager); | helper.setManager(manager); | ||||
} | } | ||||
/** | |||||
* Set the script manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
helper.setManager(manager); | |||||
} | |||||
/** | /** | ||||
* Set the classpath to be used when searching for classes and resources. | * Set the classpath to be used when searching for classes and resources. | ||||
* | * | ||||
@@ -24,6 +24,7 @@ import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import org.apache.tools.ant.types.selectors.BaseSelector; | import org.apache.tools.ant.types.selectors.BaseSelector; | ||||
import org.apache.tools.ant.util.ScriptManager; | |||||
import org.apache.tools.ant.util.ScriptRunnerBase; | import org.apache.tools.ant.util.ScriptRunnerBase; | ||||
import org.apache.tools.ant.util.ScriptRunnerHelper; | import org.apache.tools.ant.util.ScriptRunnerHelper; | ||||
@@ -70,10 +71,19 @@ public class ScriptSelector extends BaseSelector { | |||||
* | * | ||||
* @param manager the scripting manager. | * @param manager the scripting manager. | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
helper.setManager(manager); | helper.setManager(manager); | ||||
} | } | ||||
/** | |||||
* Set the script manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
helper.setManager(manager); | |||||
} | |||||
/** | /** | ||||
* Defines the language (required). | * Defines the language (required). | ||||
* | * | ||||
@@ -0,0 +1,25 @@ | |||||
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* https://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
* | |||||
*/ | |||||
package org.apache.tools.ant.util; | |||||
/** | |||||
* Script manager {@code enum}. | |||||
*/ | |||||
public enum ScriptManager { | |||||
auto, bsf, javax; | |||||
} |
@@ -17,6 +17,13 @@ | |||||
*/ | */ | ||||
package org.apache.tools.ant.util; | package org.apache.tools.ant.util; | ||||
import java.util.Collections; | |||||
import java.util.EnumMap; | |||||
import java.util.EnumSet; | |||||
import java.util.Map; | |||||
import java.util.Set; | |||||
import java.util.stream.Collectors; | |||||
import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
import org.apache.tools.ant.MagicNames; | import org.apache.tools.ant.MagicNames; | ||||
import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
@@ -26,22 +33,74 @@ import org.apache.tools.ant.Project; | |||||
* create a ScriptRunner based on a classloader and on a language. | * create a ScriptRunner based on a classloader and on a language. | ||||
*/ | */ | ||||
public class ScriptRunnerCreator { | public class ScriptRunnerCreator { | ||||
private static final String AUTO = "auto"; | |||||
private static class ScriptRunnerFactory { | |||||
final String managerClass; | |||||
final String runnerClass; | |||||
ScriptRunnerFactory(String managerClass, String runnerClass) { | |||||
this.managerClass = managerClass; | |||||
this.runnerClass = runnerClass; | |||||
} | |||||
boolean validateManager(Project project, String language, ClassLoader scriptLoader) { | |||||
try { | |||||
Class.forName(managerClass, true, scriptLoader); | |||||
return true; | |||||
} catch (Exception ex) { | |||||
return false; | |||||
} | |||||
} | |||||
ScriptRunnerBase getRunner(Project project, String language, ClassLoader scriptLoader) { | |||||
if (!validateManager(project, language, scriptLoader)) { | |||||
return null; | |||||
} | |||||
final ScriptRunnerBase runner; | |||||
try { | |||||
runner = Class.forName(runnerClass, true, scriptLoader) | |||||
.asSubclass(ScriptRunnerBase.class).getDeclaredConstructor().newInstance(); | |||||
runner.setProject(project); | |||||
} catch (Exception ex) { | |||||
throw ReflectUtil.toBuildException(ex); | |||||
} | |||||
runner.setLanguage(language); | |||||
runner.setScriptClassLoader(scriptLoader); | |||||
return runner; | |||||
} | |||||
} | |||||
private static final Map<ScriptManager, ScriptRunnerFactory> RUNNER_FACTORIES; | |||||
private static final String UTIL_OPT = MagicNames.ANT_CORE_PACKAGE + ".util.optional"; | private static final String UTIL_OPT = MagicNames.ANT_CORE_PACKAGE + ".util.optional"; | ||||
private static final String BSF = "bsf"; | |||||
private static final String BSF_PACK = "org.apache.bsf"; | private static final String BSF_PACK = "org.apache.bsf"; | ||||
private static final String BSF_MANAGER = BSF_PACK + ".BSFManager"; | private static final String BSF_MANAGER = BSF_PACK + ".BSFManager"; | ||||
private static final String BSF_RUNNER = UTIL_OPT + ".ScriptRunner"; | private static final String BSF_RUNNER = UTIL_OPT + ".ScriptRunner"; | ||||
private static final String JAVAX = "javax"; | |||||
private static final String JAVAX_MANAGER = "javax.script.ScriptEngineManager"; | private static final String JAVAX_MANAGER = "javax.script.ScriptEngineManager"; | ||||
private static final String JAVAX_RUNNER = UTIL_OPT + ".JavaxScriptRunner"; | private static final String JAVAX_RUNNER = UTIL_OPT + ".JavaxScriptRunner"; | ||||
private Project project; | |||||
private String manager; | |||||
private String language; | |||||
private ClassLoader scriptLoader = null; | |||||
static { | |||||
final Map<ScriptManager, ScriptRunnerFactory> m = new EnumMap<>(ScriptManager.class); | |||||
m.put(ScriptManager.bsf, new ScriptRunnerFactory(BSF_MANAGER, BSF_RUNNER) { | |||||
@Override | |||||
boolean validateManager(Project project, String language, ClassLoader scriptLoader) { | |||||
if (scriptLoader.getResource(LoaderUtils.classNameToResource(BSF_MANAGER)) == null) { | |||||
return false; | |||||
} | |||||
new ScriptFixBSFPath().fixClassLoader(scriptLoader, language); | |||||
return true; | |||||
} | |||||
}); | |||||
m.put(ScriptManager.javax, new ScriptRunnerFactory(JAVAX_MANAGER, JAVAX_RUNNER)); | |||||
RUNNER_FACTORIES = Collections.unmodifiableMap(m); | |||||
} | |||||
private Project project; | |||||
/** | /** | ||||
* Constructor for creator. | * Constructor for creator. | ||||
@@ -58,17 +117,29 @@ public class ScriptRunnerCreator { | |||||
* @param classLoader the classloader to use | * @param classLoader the classloader to use | ||||
* @return the created script runner. | * @return the created script runner. | ||||
* @throws BuildException if unable to create the ScriptRunner. | * @throws BuildException if unable to create the ScriptRunner. | ||||
* @deprecated Use {@link #createRunner(ScriptManager,String,ClassLoader)} instead | |||||
*/ | |||||
@Deprecated | |||||
public synchronized ScriptRunnerBase createRunner(String manager, String language, | |||||
ClassLoader classLoader) { | |||||
return createRunner(ScriptManager.valueOf(manager), language, classLoader); | |||||
} | |||||
/** | |||||
* Create a ScriptRunner. | |||||
* @param manager the {@link ScriptManager} | |||||
* @param language the language. | |||||
* @param classLoader the classloader to use | |||||
* @return the created script runner. | |||||
* @throws BuildException if unable to create the ScriptRunner. | |||||
*/ | */ | ||||
public synchronized ScriptRunnerBase createRunner( | |||||
String manager, String language, ClassLoader classLoader) { | |||||
this.manager = manager; | |||||
this.language = language; | |||||
this.scriptLoader = classLoader; | |||||
public synchronized ScriptRunnerBase createRunner(ScriptManager manager, String language, | |||||
ClassLoader classLoader) { | |||||
if (language == null) { | if (language == null) { | ||||
throw new BuildException("script language must be specified"); | throw new BuildException("script language must be specified"); | ||||
} | } | ||||
if (!manager.equals(AUTO) && !manager.equals(JAVAX) && !manager.equals(BSF)) { | |||||
if (manager == null) { | |||||
throw new BuildException("Unsupported language prefix " + manager); | throw new BuildException("Unsupported language prefix " + manager); | ||||
} | } | ||||
@@ -76,64 +147,16 @@ public class ScriptRunnerCreator { | |||||
// This version does not check if the scriptManager | // This version does not check if the scriptManager | ||||
// supports the language. | // 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 + ")"); | |||||
} | |||||
if (BSF.equals(manager)) { | |||||
throw new BuildException( | |||||
"Unable to load the BSF script engine manager " + "(" + BSF_MANAGER + ")"); | |||||
} | |||||
throw new BuildException("Unable to load a script engine manager " | |||||
+ "(" + BSF_MANAGER + " or " + JAVAX_MANAGER + ")"); | |||||
} | |||||
/** | |||||
* 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 matches this value. | |||||
* @param managerClass 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. | |||||
*/ | |||||
private ScriptRunnerBase createRunner( | |||||
String checkManager, String managerClass, String runnerClass) { | |||||
ScriptRunnerBase runner = null; | |||||
if (!manager.equals(AUTO) && !manager.equals(checkManager)) { | |||||
return null; | |||||
} | |||||
if (managerClass.equals(BSF_MANAGER)) { | |||||
if (scriptLoader.getResource(LoaderUtils.classNameToResource(managerClass)) == null) { | |||||
return null; | |||||
} | |||||
new ScriptFixBSFPath().fixClassLoader(scriptLoader, language); | |||||
final Set<ScriptManager> managers; | |||||
if (manager == ScriptManager.auto) { | |||||
managers = EnumSet.complementOf(EnumSet.of(ScriptManager.auto)); | |||||
} else { | } else { | ||||
try { | |||||
Class.forName(managerClass, true, scriptLoader); | |||||
} catch (Exception ex) { | |||||
return null; | |||||
} | |||||
} | |||||
try { | |||||
runner = (ScriptRunnerBase) Class.forName( | |||||
runnerClass, true, scriptLoader).getDeclaredConstructor().newInstance(); | |||||
runner.setProject(project); | |||||
} catch (Exception ex) { | |||||
throw ReflectUtil.toBuildException(ex); | |||||
managers = EnumSet.of(manager); | |||||
} | } | ||||
runner.setLanguage(language); | |||||
runner.setScriptClassLoader(scriptLoader); | |||||
return runner; | |||||
return managers.stream().map(RUNNER_FACTORIES::get) | |||||
.map(f -> f.getRunner(project, language, classLoader)).findFirst() | |||||
.orElseThrow(() -> new BuildException( | |||||
managers.stream().map(RUNNER_FACTORIES::get).map(f -> f.managerClass).collect( | |||||
Collectors.joining("|", "Unable to load script engine manager (", ")")))); | |||||
} | } | ||||
} | } |
@@ -32,9 +32,9 @@ public class ScriptRunnerHelper { | |||||
private ClasspathUtils.Delegate cpDelegate = null; | private ClasspathUtils.Delegate cpDelegate = null; | ||||
private File srcFile; | private File srcFile; | ||||
private String encoding; | private String encoding; | ||||
private String manager = "auto"; | |||||
private String language; | private String language; | ||||
private String text; | private String text; | ||||
private ScriptManager manager = ScriptManager.auto; | |||||
private boolean compiled = false; | private boolean compiled = false; | ||||
private boolean setBeans = true; | private boolean setBeans = true; | ||||
private ProjectComponent projectComponent; | private ProjectComponent projectComponent; | ||||
@@ -157,8 +157,17 @@ public class ScriptRunnerHelper { | |||||
* | * | ||||
* @param manager the scripting manager - "bsf" or "javax" or "auto" | * @param manager the scripting manager - "bsf" or "javax" or "auto" | ||||
*/ | */ | ||||
@Deprecated | |||||
public void setManager(String manager) { | public void setManager(String manager) { | ||||
this.manager = manager; | |||||
setManager(manager == null ? null : ScriptManager.valueOf(manager)); | |||||
} | |||||
/** | |||||
* Set the manager. | |||||
* @param manager | |||||
*/ | |||||
public void setManager(ScriptManager manager) { | |||||
this.manager = manager == null ? ScriptManager.auto : manager; | |||||
} | } | ||||
/** | /** | ||||