ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1039400 - in /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs: ExecTask.java Execute.java Property.java optional/Rpm.java optional/jdepend/JDependTask.java optional/junit/JUnitTask.java
Date Fri, 26 Nov 2010 15:01:47 GMT
Author: bodewig
Date: Fri Nov 26 15:01:46 2010
New Revision: 1039400

URL: http://svn.apache.org/viewvc?rev=1039400&view=rev
Log:
provide a Map based method to access environment variables and use that

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ExecTask.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ExecTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ExecTask.java Fri Nov 26 15:01:46
2010
@@ -21,8 +21,9 @@ package org.apache.tools.ant.taskdefs;
 import java.io.File;
 import java.io.IOException;
 import java.util.Enumeration;
-import java.util.Vector;
 import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
@@ -454,14 +455,9 @@ public class ExecTask extends Task {
                 }
             }
             if (p == null) {
-                Vector envVars = Execute.getProcEnvironment();
-                Enumeration e = envVars.elements();
-                while (e.hasMoreElements()) {
-                    String line = (String) e.nextElement();
-                    if (isPath(line)) {
-                        p = new Path(getProject(), getPath(line));
-                        break;
-                    }
+                String path = getPath(Execute.getEnvironmentVariables());
+                if (path != null) {
+                    p = new Path(getProject(), path);
                 }
             }
             if (p != null) {
@@ -724,4 +720,9 @@ public class ExecTask extends Task {
     private String getPath(String line) {
         return line.substring("PATH=".length());
     }
+
+    private String getPath(Map/*<String, String>*/ map) {
+        String p = (String) map.get("PATH");
+        return p != null ? p : (String) map.get("Path");
+    }
 }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java Fri Nov 26 15:01:46
2010
@@ -26,8 +26,10 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.StringReader;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Vector;
 
@@ -73,7 +75,7 @@ public class Execute {
     private static String antWorkingDirectory = System.getProperty("user.dir");
     private static CommandLauncher vmLauncher = null;
     private static CommandLauncher shellLauncher = null;
-    private static Vector procEnvironment = null;
+    private static Map/*<String, String>*/ procEnvironment = null;
 
     /** Used to destroy processes when the VM exits. */
     private static ProcessDestroyer processDestroyer = new ProcessDestroyer();
@@ -150,27 +152,26 @@ public class Execute {
     /**
      * Find the list of environment variables for this process.
      *
-     * @return a vector containing the environment variables.
-     * The vector elements are strings formatted like variable = value.
+     * @return a map containing the environment variables.
+     * @since Ant 1.8.2
      */
-    public static synchronized Vector getProcEnvironment() {
+    public static synchronized Map/*<String,String>*/ getEnvironmentVariables() {
         if (procEnvironment != null) {
             return procEnvironment;
         }
-        procEnvironment = new Vector();
-        if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_5)) {
+        if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_5)
+            && !Os.isFamily("openvms")) {
             try {
-                Map/*<String,String>*/ env = (Map) System.class.getMethod("getenv",
new Class[0]).invoke(null, new Object[0]);
-                Iterator it = env.entrySet().iterator();
-                while (it.hasNext()) {
-                    Map.Entry entry = (Map.Entry) it.next();
-                    procEnvironment.add(entry.getKey() + "=" + entry.getValue());
-                }
+                procEnvironment = (Map) System.class
+                    .getMethod("getenv", new Class[0])
+                    .invoke(null, new Object[0]);
                 return procEnvironment;
             } catch (Exception x) {
                 x.printStackTrace();
             }
         }
+
+        procEnvironment = new LinkedHashMap();
         try {
             ByteArrayOutputStream out = new ByteArrayOutputStream();
             Execute exe = new Execute(new PumpStreamHandler(out));
@@ -185,7 +186,7 @@ public class Execute {
                 new BufferedReader(new StringReader(toString(out)));
 
             if (Os.isFamily("openvms")) {
-                procEnvironment = addVMSLogicals(procEnvironment, in);
+                procEnvironment = getVMSLogicals(in);
                 return procEnvironment;
             }
             String var = null;
@@ -202,14 +203,17 @@ public class Execute {
                 } else {
                     // New env var...append the previous one if we have it.
                     if (var != null) {
-                        procEnvironment.addElement(var);
+                        int eq = var.indexOf("=");
+                        procEnvironment.put(var.substring(0, eq),
+                                            var.substring(eq + 1));
                     }
                     var = line;
                 }
             }
             // Since we "look ahead" before adding, there's one last env var.
             if (var != null) {
-                procEnvironment.addElement(var);
+                int eq = var.indexOf("=");
+                procEnvironment.put(var.substring(0, eq), var.substring(eq + 1));
             }
         } catch (java.io.IOException exc) {
             exc.printStackTrace();
@@ -219,6 +223,23 @@ public class Execute {
     }
 
     /**
+     * Find the list of environment variables for this process.
+     *
+     * @return a vector containing the environment variables.
+     * The vector elements are strings formatted like variable = value.
+     * @deprecated use #getEnvironmentVariables instead
+     */
+    public static synchronized Vector getProcEnvironment() {
+        Vector v = new Vector();
+        Iterator it = getEnvironmentVariables().entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry entry = (Map.Entry) it.next();
+            v.add(entry.getKey() + "=" + entry.getValue());
+        }
+        return v;
+    }
+
+    /**
      * This is the operation to get our environment.
      * It is a notorious troublespot pre-Java1.5, and should be approached
      * with extreme caution.
@@ -639,36 +660,41 @@ public class Execute {
         if (Os.isFamily("openvms")) {
             return env;
         }
-        Vector osEnv = (Vector) getProcEnvironment().clone();
+        Map/*<String, String>*/ osEnv =
+            new LinkedHashMap(getEnvironmentVariables());
         for (int i = 0; i < env.length; i++) {
             String keyValue = env[i];
-            // Get key including "="
-            String key = keyValue.substring(0, keyValue.indexOf('=') + 1);
-            if (environmentCaseInSensitive) {
-                // Nb: using default locale as key is a env name
-                key = key.toLowerCase();
-            }
-            int size = osEnv.size();
+            String key = keyValue.substring(0, keyValue.indexOf('='));
             // Find the key in the current enviroment copy
             // and remove it.
-            for (int j = 0; j < size; j++) {
-                String osEnvItem = (String) osEnv.elementAt(j);
-                String convertedItem = environmentCaseInSensitive
-                    ? osEnvItem.toLowerCase() : osEnvItem;
-                if (convertedItem.startsWith(key)) {
-                    osEnv.removeElementAt(j);
-                    if (environmentCaseInSensitive) {
+
+            // Try without changing case first
+            if (osEnv.remove(key) == null && environmentCaseInSensitive) {
+                // not found, maybe perform a case insensitive search
+
+                // Nb: using default locale as key is a env name
+                key = key.toLowerCase();
+
+                for (Iterator it = osEnv.keySet().iterator(); it.hasNext(); ) {
+                    String osEnvItem = (String) it.next();
+                    if (osEnvItem.toLowerCase().equals(key)) {
                         // Use the original casiness of the key
-                        keyValue = osEnvItem.substring(0, key.length())
-                            + keyValue.substring(key.length());
+                        key = osEnvItem;
+                        break;
                     }
-                    break;
                 }
             }
+
             // Add the key to the enviromnent copy
-            osEnv.addElement(keyValue);
+            osEnv.put(key, keyValue.substring(key.length() + 1));
         }
-        return (String[]) (osEnv.toArray(new String[osEnv.size()]));
+
+        ArrayList l = new ArrayList();
+        for (Iterator it = osEnv.entrySet().iterator(); it.hasNext(); ) {
+            Map.Entry entry = (Map.Entry) it.next();
+            l.add(entry.getKey() + "=" + entry.getValue());
+        }
+        return (String[]) (l.toArray(new String[osEnv.size()]));
     }
 
     /**
@@ -711,17 +737,17 @@ public class Execute {
     }
 
     /**
-     * This method is VMS specific and used by getProcEnvironment().
+     * This method is VMS specific and used by getEnvironmentVariables().
      *
-     * Parses VMS logicals from <code>in</code> and adds them to
-     * <code>environment</code>.  <code>in</code> is expected to
be the
+     * Parses VMS logicals from <code>in</code> and returns them as a Map.
+     * <code>in</code> is expected to be the
      * output of "SHOW LOGICAL".  The method takes care of parsing the output
      * correctly as well as making sure that a logical defined in multiple
      * tables only gets added from the highest order table.  Logicals with
      * multiple equivalence names are mapped to a variable with multiple
      * values separated by a comma (,).
      */
-    private static Vector addVMSLogicals(Vector environment, BufferedReader in)
+    private static Map getVMSLogicals(BufferedReader in)
         throws IOException {
         HashMap logicals = new HashMap();
         String logName = null, logValue = null, newLogName;
@@ -755,11 +781,7 @@ public class Execute {
         if (logName != null) {
             logicals.put(logName, logValue);
         }
-        for (Iterator i = logicals.keySet().iterator(); i.hasNext();) {
-            String logical = (String) i.next();
-            environment.add(logical + "=" + logicals.get(logical));
-        }
-        return environment;
+        return logicals;
     }
 
     /**

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java Fri Nov 26 15:01:46
2010
@@ -664,16 +664,10 @@ public class Property extends Task {
             prefix += ".";
         }
         log("Loading Environment " + prefix, Project.MSG_VERBOSE);
-        Vector osEnv = Execute.getProcEnvironment();
-        for (Enumeration e = osEnv.elements(); e.hasMoreElements();) {
-            String entry = (String) e.nextElement();
-            int pos = entry.indexOf('=');
-            if (pos == -1) {
-                log("Ignoring: " + entry, Project.MSG_WARN);
-            } else {
-                props.put(prefix + entry.substring(0, pos),
-                          entry.substring(pos + 1));
-            }
+        Map osEnv = Execute.getEnvironmentVariables();
+        for (Iterator e = osEnv.entrySet().iterator(); e.hasNext(); ) {
+            Map.Entry entry = (Map.Entry) e.next();
+            props.put(prefix + entry.getKey(), entry.getValue());
         }
         addProperties(props);
     }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java Fri Nov 26 15:01:46
2010
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.util.Enumeration;
+import java.util.Map;
 import java.util.Vector;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
@@ -44,10 +45,9 @@ import org.apache.tools.ant.types.Path;
  */
 public class Rpm extends Task {
 
-    private static final String PATH1 = "PATH=";
-    private static final String PATH2 = "Path=";
-    private static final String PATH3 = "path=";
-    private static final int PATH_LEN = PATH1.length();
+    private static final String PATH1 = "PATH";
+    private static final String PATH2 = "Path";
+    private static final String PATH3 = "path";
 
     /**
      * the spec file
@@ -317,13 +317,12 @@ public class Rpm extends Task {
      * @since 1.6
      */
     protected String guessRpmBuildCommand() {
-        Vector env = Execute.getProcEnvironment();
-        String path = null;
-        for (Enumeration e = env.elements(); e.hasMoreElements();) {
-            String var = (String) e.nextElement();
-            if (var.startsWith(PATH1) || var.startsWith(PATH2) || var.startsWith(PATH3))
{
-                path = var.substring(PATH_LEN);
-                break;
+        Map/*<String, String>*/ env = Execute.getEnvironmentVariables();
+        String path = (String) env.get(PATH1);
+        if (path == null) {
+            path = (String) env.get(PATH2);
+            if (path == null) {
+                path = (String) env.get(PATH3);
             }
         }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java
Fri Nov 26 15:01:46 2010
@@ -24,8 +24,8 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.util.Map;
 import java.util.Vector;
-import java.util.Enumeration;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
@@ -593,16 +593,11 @@ public class JDependTask extends Task {
         }
 
         if (includeRuntime) {
-            Vector v = Execute.getProcEnvironment();
-            Enumeration e = v.elements();
-            while (e.hasMoreElements()) {
-                String s = (String) e.nextElement();
-                if (s.startsWith("CLASSPATH=")) {
-                    commandline.createClasspath(getProject()).createPath()
-                        .append(new Path(getProject(),
-                                         s.substring("CLASSPATH=".length()
-                                                     )));
-                }
+            Map/*<String, String>*/ env = Execute.getEnvironmentVariables();
+            String cp = (String) env.get("CLASSPATH");
+            if (cp != null) {
+                commandline.createClasspath(getProject()).createPath()
+                    .append(new Path(getProject(), cp));
             }
             log("Implicitly adding " + runtimeClasses + " to CLASSPATH",
                 Project.MSG_VERBOSE);

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java?rev=1039400&r1=1039399&r2=1039400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java Fri
Nov 26 15:01:46 2010
@@ -132,7 +132,7 @@ public class JUnitTask extends Task {
 
     private static final String LINE_SEP
         = System.getProperty("line.separator");
-    private static final String CLASSPATH = "CLASSPATH=";
+    private static final String CLASSPATH = "CLASSPATH";
     private CommandlineJava commandline;
     private Vector tests = new Vector();
     private Vector batchTests = new Vector();
@@ -1114,16 +1114,11 @@ public class JUnitTask extends Task {
      */
     private void checkIncludeAntRuntime(CommandlineJava cmd) {
         if (includeAntRuntime) {
-            Vector v = Execute.getProcEnvironment();
-            Enumeration e = v.elements();
-            while (e.hasMoreElements()) {
-                String s = (String) e.nextElement();
-                if (s.startsWith(CLASSPATH)) {
-                    cmd.createClasspath(getProject()).createPath()
-                        .append(new Path(getProject(),
-                                         s.substring(CLASSPATH.length()
-                                                     )));
-                }
+            Map/*<String, String>*/ env = Execute.getEnvironmentVariables();
+            String cp = (String) env.get(CLASSPATH);
+            if (cp != null) {
+                cmd.createClasspath(getProject()).createPath()
+                    .append(new Path(getProject(), cp));
             }
             log("Implicitly adding " + antRuntimeClasses + " to CLASSPATH",
                 Project.MSG_VERBOSE);



Mime
View raw message