You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2017/02/20 16:58:57 UTC

[07/12] ant git commit: Cleaning, proper classpath in JavaxScriptRunner

Cleaning, proper classpath in JavaxScriptRunner


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/49896d33
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/49896d33
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/49896d33

Branch: refs/heads/master
Commit: 49896d33693b9fb3b766757c566fc862a6f0c0cf
Parents: 1630d0c
Author: pyxide <op...@gmail.com>
Authored: Tue Jan 17 00:38:36 2017 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Mon Feb 20 17:41:37 2017 +0100

----------------------------------------------------------------------
 .../ant/taskdefs/optional/script/ScriptDef.java | 11 +++-
 .../apache/tools/ant/util/ScriptRunnerBase.java | 31 +++++-----
 .../tools/ant/util/ScriptRunnerHelper.java      | 26 ++++++--
 .../ant/util/optional/JavaxScriptRunner.java    | 65 +++++++-------------
 4 files changed, 68 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/49896d33/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
index 3f40a14..e45668c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
@@ -188,10 +188,15 @@ public class ScriptDef extends DefBase {
         }
 
         if (helper.getLanguage() == null) {
-            throw new BuildException("<scriptdef> requires a language attribute "
+            throw new BuildException("scriptdef requires a language attribute "
                 + "to specify the script language");
         }
 
+        if (helper.getSrc() == null && helper.getEncoding() != null) {
+            throw new BuildException("scriptdef requires a src attribute "
+                    + "if the encoding is set");
+        }
+
         // Check if need to set the loader
         if (getAntlibClassLoader() != null || hasCpDelegate()) {
             helper.setClassLoader(createLoader());
@@ -367,9 +372,9 @@ public class ScriptDef extends DefBase {
     }
 
     /**
-     * Defines the language (required).
+     * Defines the compilation feature (optional).
      *
-     * @param language the scripting language name for the script.
+     * @param compiled enables the script compilation if available.
      */
     public void setCompiled(boolean compiled) {
         helper.setCompiled(compiled);

http://git-wip-us.apache.org/repos/asf/ant/blob/49896d33/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java b/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
index ac08f98..ae0af33 100644
--- a/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
+++ b/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
@@ -25,7 +25,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -223,7 +223,6 @@ public abstract class ScriptRunnerBase {
 
     /**
      * Load the script from an external file; optional.
-     * @since Ant 1.10.1
      * @param file the file containing the script source.
      */
     public void setSrc(File file) {
@@ -231,6 +230,7 @@ public abstract class ScriptRunnerBase {
         if (!file.exists()) {
             throw new BuildException("file " + filename + " not found.");
         }
+
         InputStream in = null;
         try {
             in = new FileInputStream(file);
@@ -239,8 +239,15 @@ public abstract class ScriptRunnerBase {
             throw new BuildException("file " + filename + " not found.");
         }
 
+        final Charset charset;
+        if (null == encoding) {
+        	charset = null;
+        } else {
+        	charset = Charset.forName(encoding);
+        }
+
         try {
-            readSource(in, filename);
+            readSource(in, filename, charset);
         } finally {
              FileUtils.close(in);
         }
@@ -250,19 +257,18 @@ public abstract class ScriptRunnerBase {
      * Read some source in from the given reader
      * @param reader the reader; this is closed afterwards.
      * @param name the name to use in error messages
+     * @param charset the encoding for the reader, may be null.
      */
-    private void readSource(InputStream in, String name) {
+    private void readSource(InputStream in, String name, Charset charset) {
         Reader reader = null;
         try {
-            if (null == encoding) {
+            if (null == charset) {
                 reader = new InputStreamReader(in);
             } else {
-                reader = new InputStreamReader(in, encoding);
+                reader = new InputStreamReader(in, charset);
             }
             reader = new BufferedReader(reader);
             script += FileUtils.safeReadFully(reader);
-        } catch(UnsupportedEncodingException e) {
-            throw new BuildException("Failed to decode " + name + " with encoding " + encoding, e);
         } catch (IOException ex) {
             throw new BuildException("Failed to read " + name, ex);
         } finally {
@@ -278,7 +284,6 @@ public abstract class ScriptRunnerBase {
      */
     public void loadResource(Resource sourceResource) {
     	if(sourceResource instanceof StringResource) {
-    		// Note: StringResource uses UTF-8 by default to encode/decode, not the default platform encoding
     		script += ((StringResource) sourceResource).getValue();
     		return;
     	}
@@ -287,10 +292,6 @@ public abstract class ScriptRunnerBase {
     		return;
     	}
 
-    	// Concat resource
-    	
-    	// FileResource : OK for default encoding
-
         String name = sourceResource.toLongString();
         InputStream in = null;
         try {
@@ -299,11 +300,11 @@ public abstract class ScriptRunnerBase {
             throw new BuildException("Failed to open " + name, e);
         } catch (UnsupportedOperationException e) {
             throw new BuildException(
-                "Failed to open " + name + " -it is not readable", e);
+                "Failed to open " + name + " - it is not readable", e);
         }
 
         try {
-            readSource(in, name);
+            readSource(in, name, (Charset) null);
         } finally {
             FileUtils.close(in);
         }

http://git-wip-us.apache.org/repos/asf/ant/blob/49896d33/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java b/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
index 5cc23ab..528a565 100644
--- a/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
+++ b/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
@@ -58,7 +58,7 @@ public class ScriptRunnerHelper {
         runner.setCompiled(compiled);
 
         if (encoding != null) {
-        	// set it first, because runner.setSrc() loads immediately the file
+            // set it first, because runner.setSrc() loads immediately the file
             runner.setEncoding(encoding);
         }
         if (srcFile != null) {
@@ -116,14 +116,32 @@ public class ScriptRunnerHelper {
     }
 
     /**
+     * Get the external script file ; optional.
+     * @return the file containing the script source.
+     * @since Ant 1.10.1
+     */
+    public File getSrc() {
+        return srcFile;
+    }
+
+    /**
      * Set the encoding of the script from an external file ; optional.
      *
      * @param encoding the encoding of the file containing the script source.
      * @since Ant 1.10.1
      */
-	public void setEncoding(String encoding) {
-		this.encoding = encoding;
-	}
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    /**
+     * Get the external file encoding.
+     * @return the encoding of the file containing the script source.
+     * @since Ant 1.10.1
+     */
+    public String getEncoding() {
+        return encoding;
+    }
 
     /**
      * Add script text.

http://git-wip-us.apache.org/repos/asf/ant/blob/49896d33/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
index 2670b09..7209e28 100644
--- a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
+++ b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
@@ -31,11 +31,7 @@ import org.apache.tools.ant.util.ScriptRunnerBase;
  */
 public class JavaxScriptRunner extends ScriptRunnerBase {
     private ReflectWrapper engine;
-
-    /** Debug constant */
-    private static final boolean DEBUG = Boolean.getBoolean("JavaxScriptRunner.DEBUG");
-
-    private String compiledScriptRefName; 
+    private ReflectWrapper compiledScript;
 
     /**
      * Get the name of the manager prefix.
@@ -86,16 +82,14 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
         ClassLoader origLoader = replaceContextLoader();
         try {
 
-            if(DEBUG) System.out.println("-- JavaxScriptRunner.evaluateScript : compile enabled [" + getCompiled() + "]");
-
             if (getCompiled()) {
 
-            	if (null == compiledScriptRefName) {
-            		compiledScriptRefName = execName + ".compiledScript.0123456789";
-            	}
-                ReflectWrapper scriptRefObj = getProject().getReference(compiledScriptRefName);
+                final String compiledScriptRefName = execName + ".compiledScript.0123456789";
+                if (null == compiledScript) {
+                    compiledScript = getProject().getReference(compiledScriptRefName);
+                }
 
-                if (null == scriptRefObj) {
+                if (null == compiledScript) {
 
                     ReflectWrapper engine = createEngine();
                     if (engine == null) {
@@ -104,51 +98,36 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
                             + getLanguage());
                     }
 
-                    final Class engineClass = Class.forName("javax.script.ScriptEngine");
-                    final Class compilableClass = Class.forName("javax.script.Compilable");
+                    final Class engineClass = Class.forName("javax.script.ScriptEngine", true, getClass().getClassLoader());
+                    final Class compilableClass = Class.forName("javax.script.Compilable", true, getClass().getClassLoader());
                     final Object wrappedObject = engine.getObject();
 
-                    if (DEBUG) System.out.println("-- JavaxScriptRunner.evaluateScript : wrappedObject [" + wrappedObject.getClass().getName() + "]");
-                    if (engineClass.isAssignableFrom(wrappedObject.getClass()) && compilableClass.isAssignableFrom(wrappedObject.getClass())) {
-
-                        if(DEBUG) System.out.println("-- JavaxScriptRunner.evaluateScript : compilable [" + wrappedObject.getClass().getName() + "]");
+                    if (engineClass.isAssignableFrom(wrappedObject.getClass()) &&
+                        compilableClass.isAssignableFrom(wrappedObject.getClass())) {
 
-                        {
-                            getProject().log("compile script" + compiledScriptRefName, Project.MSG_VERBOSE);
+                        getProject().log("compile script " + execName, Project.MSG_VERBOSE);
 
-                            // compilable engine
-                            final Object compiledScript = engine.invoke("compile", String.class, getScript());
-                            scriptRefObj = new ReflectWrapper(compiledScript);
-                        }
-
-                        getProject().log("store compiled script, ref " + compiledScriptRefName, Project.MSG_DEBUG);
+                        final Object compiled = engine.invoke("compile", String.class, getScript());
+                        compiledScript = new ReflectWrapper(compiled);
 
                     } else {
-                        getProject().log("script compilation not available", Project.MSG_DEBUG);
-                        scriptRefObj = new ReflectWrapper(null);
+                        getProject().log("script compilation not available", Project.MSG_VERBOSE);
+                        compiledScript = new ReflectWrapper(null);
                     }
-
-                    getProject().addReference(compiledScriptRefName, scriptRefObj);
+                    getProject().addReference(compiledScriptRefName, compiledScript);
                 }
 
-                if (null != scriptRefObj.getObject()) {
+                if (null != compiledScript.getObject()) {
 
-                    if (DEBUG) System.out.println("-- JavaxScriptRunner.evaluateScript : execute compiled script");
-
-                    final Object simpleBindings;
-                    {
-                        final Class simpleBindingsClass  = Class.forName("javax.script.SimpleBindings");
-                        simpleBindings = simpleBindingsClass.newInstance();
-                    }
+                    final ReflectWrapper simpleBindings = new ReflectWrapper(getClass().getClassLoader(), "javax.script.SimpleBindings");
 
-                    applyBindings(new ReflectWrapper(simpleBindings));
-                    if (DEBUG) System.out.println("-- JavaxScriptRunner.evaluateScript : bindings applied");
+                    applyBindings(simpleBindings);
 
-                    getProject().log("run compiled script, ref " + compiledScriptRefName, Project.MSG_DEBUG);
+                    getProject().log("run compiled script " + execName, Project.MSG_DEBUG);
 
-                    final Class bindingsClass  = Class.forName("javax.script.Bindings");
+                    final Class bindingsClass  = Class.forName("javax.script.Bindings", true, getClass().getClassLoader());
 
-                    return scriptRefObj.invoke("eval", bindingsClass, simpleBindings);
+                    return compiledScript.invoke("eval", bindingsClass, simpleBindings.getObject());
                 }
             }