You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by pe...@apache.org on 2006/12/03 23:53:19 UTC
svn commit: r481968 - in /ant/core/trunk/src/main/org/apache/tools/ant/util:
ScriptRunnerCreator.java ScriptRunnerHelper.java
Author: peterreilly
Date: Sun Dec 3 14:53:18 2006
New Revision: 481968
URL: http://svn.apache.org/viewvc?view=rev&rev=481968
Log:
a helper and creator class for script runners
Added:
ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java
ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
Added: ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java?view=auto&rev=481968
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java (added)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java Sun Dec 3 14:53:18 2006
@@ -0,0 +1,139 @@
+/*
+ * 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
+ *
+ * http://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;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+/**
+ * This is a helper class used by ScriptRunnerHelper to
+ * create a ScriptRunner based on a classloader and
+ * on a language.
+ */
+public class ScriptRunnerCreator {
+ private static final String AUTO = "auto";
+ private static final String OATAU = "org.apache.tools.ant.util";
+ private static final String UTIL_OPT = OATAU + ".optional";
+
+ private static final String BSF = "bsf";
+ private static final String BSF_PACK = "org.apache.bsf";
+ private static final String BSF_MANAGER = BSF_PACK + ".BSFManager";
+ 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_RUNNER = UTIL_OPT + ".JavaxScriptRunner";
+
+ private Project project;
+ private String manager;
+ private String language;
+ private ClassLoader scriptLoader = null;
+
+ /**
+ * Constructor for creator.
+ * @param project the current project.
+ */
+ public ScriptRunnerCreator(Project project) {
+ this.project = project;
+ }
+
+ /**
+ * 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.
+ */
+ public ScriptRunnerBase createRunner(
+ String manager, String language, ClassLoader classLoader) {
+ 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 + ")");
+ }
+ }
+
+ /**
+ * 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.
+ */
+ private ScriptRunnerBase createRunner(
+ String checkManager, String managerClass, String runnerClass) {
+ 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);
+ return runner;
+ } catch (Exception ex) {
+ ReflectUtil.throwBuildException(ex);
+ // NotReached
+ }
+
+ runner.setLanguage(language);
+ runner.setScriptClassLoader(scriptLoader);
+ return runner;
+ }
+}
Added: ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java?view=auto&rev=481968
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java (added)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java Sun Dec 3 14:53:18 2006
@@ -0,0 +1,191 @@
+/*
+ * 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
+ *
+ * http://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;
+
+import org.apache.tools.ant.ProjectComponent;
+import org.apache.tools.ant.types.Path;
+import java.io.File;
+import org.apache.tools.ant.types.Reference;
+
+
+/**
+ * A class to help in creating, setting and getting
+ * script runners.
+ */
+public class ScriptRunnerHelper {
+ private ClasspathUtils.Delegate cpDelegate = null;
+ private File srcFile;
+ private String manager = "auto";
+ private String language;
+ private String text;
+ private boolean setBeans = true;
+ private ProjectComponent projectComponent;
+ private ClassLoader scriptLoader = null;
+
+ /**
+ * Set the project component associated with this helper.
+ * @param component the project component that owns this helper.
+ */
+ public void setProjectComponent(ProjectComponent component) {
+ this.projectComponent = component;
+ }
+
+ /**
+ * Create and set text on a script.
+ * @return the created or reused script runner.
+ */
+ public ScriptRunnerBase getScriptRunner() {
+ ScriptRunnerBase runner = getRunner();
+ if (srcFile != null) {
+ runner.setSrc(srcFile);
+ }
+ if (text != null) {
+ runner.addText(text);
+ }
+ if (setBeans) {
+ runner.bindToComponent(projectComponent);
+ } else {
+ runner.bindToComponentMinimum(projectComponent);
+ }
+ return runner;
+ }
+
+ /**
+ * Classpath to be used when searching for classes and resources.
+ *
+ * @return an empty Path instance to be configured by Ant.
+ */
+ public Path createClasspath() {
+ return getClassPathDelegate().createClasspath();
+ }
+
+ /**
+ * Set the classpath to be used when searching for classes and resources.
+ *
+ * @param classpath an Ant Path object containing the search path.
+ */
+ public void setClasspath(Path classpath) {
+ getClassPathDelegate().setClasspath(classpath);
+ }
+
+ /**
+ * Set the classpath by reference.
+ *
+ * @param r a Reference to a Path instance to be used as the classpath
+ * value.
+ */
+ public void setClasspathRef(Reference r) {
+ getClassPathDelegate().setClasspathref(r);
+ }
+
+ /**
+ * Load the script from an external file ; optional.
+ *
+ * @param file the file containing the script source.
+ */
+ public void setSrc(File file) {
+ this.srcFile = file;
+ }
+
+ /**
+ * The script text.
+ *
+ * @param text a component of the script text to be added.
+ */
+ public void addText(String text) {
+ this.text = text;
+ }
+
+ /**
+ * Defines the script manager - defaults to "auto".
+ *
+ * @param manager the scripting manager - "bsf" or "javax" or "auto"
+ */
+ public void setManager(String manager) {
+ this.manager = manager;
+ }
+
+ /**
+ * Defines the language (required).
+ *
+ * @param language the scripting language name for the script.
+ */
+ public void setLanguage(String language) {
+ this.language = language;
+ }
+
+ /**
+ * Get the language.
+ * @return the scripting language.
+ */
+ public String getLanguage() {
+ return language;
+ }
+
+ /**
+ * Set the setbeans attribute.
+ * If this is true, <script> will create variables in the
+ * script instance for all
+ * properties, targets and references of the current project.
+ * It this is false, only the project and self variables will
+ * be set.
+ * The default is true.
+ * @param setBeans the value to set.
+ */
+ public void setSetBeans(boolean setBeans) {
+ this.setBeans = setBeans;
+ }
+
+ /**
+ * Used when called by scriptdef.
+ * @param loader the loader used by scriptdef.
+ */
+ public void setClassLoader(ClassLoader loader) {
+ scriptLoader = loader;
+ }
+
+
+ private ClassLoader generateClassLoader() {
+ if (scriptLoader != null) {
+ return scriptLoader;
+ }
+ if (cpDelegate == null) {
+ scriptLoader = getClass().getClassLoader();
+ return scriptLoader;
+ }
+
+ scriptLoader = cpDelegate.getClassLoader();
+ return scriptLoader;
+ }
+
+ private ClasspathUtils.Delegate getClassPathDelegate() {
+ if (cpDelegate == null) {
+ cpDelegate = ClasspathUtils.getDelegate(projectComponent);
+ }
+ return cpDelegate;
+ }
+
+ /**
+ * Get a script runner.
+ */
+ private ScriptRunnerBase getRunner() {
+ return new ScriptRunnerCreator(
+ projectComponent.getProject()).createRunner(
+ manager, language, generateClassLoader());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org