Commits

James Dumay committed e352bf6

BAM-8352 - should not convert from map -> string array -> map for env variables but rather use the same map the whole way down the stack.

Comments (0)

Files changed (3)

src/main/java/com/atlassian/utils/process/ExternalProcess.java

 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 public class ExternalProcess implements Watchdog {
     private String[] cmdArray;
     private File workingDir;
-    private String[] environment;
+    private Map<String, String> environment;
     private ProcessHandler handler;
     private Process process;
 
-    private List<ProcessMonitor> monitors = new ArrayList<ProcessMonitor>();
+    private final List<ProcessMonitor> monitors = new ArrayList<ProcessMonitor>();
     
     private ProcessException processException;
 
         this.workingDir = workingDir;
     }
 
-    public void setEnvironment(String[] environment) {
+    public void setEnvironment(Map<String, String> environment) {
         this.environment = environment;
     }
 
       * seems to indicate, can't find any definitive answer in the cmd docos). While this still isn't a true fix to the problem, for most situations this will be sufficient 
       * as the user typically won't use characters that aren't defined for his locale. But then again, they might..
      */
-    private Process createWinProcess(String[] cmdArray, String[] environment, File workingDir) throws IOException {
-        Map<String, String> newEnv = splitEnvironment(environment);
+    private Process createWinProcess(String[] cmdArray, Map<String, String> environment, File workingDir) throws IOException {
         int extraArgs = 4;
         String[] i18n = new String[cmdArray.length + extraArgs];
         i18n[0] = "cmd";
         for (int counter = 1; counter < cmdArray.length; counter++) {
             String envName = "JENV_" + counter;
             i18n[counter + extraArgs] = "%" + envName + "%";
-            newEnv.put(envName, quoteString(cmdArray[counter]));
+            environment.put(envName, quoteString(cmdArray[counter]));
         }
         cmdArray = i18n;
 
         ProcessBuilder pb = new ProcessBuilder(cmdArray);
         pb.directory(workingDir);
-        Map<String, String> env = pb.environment();
-        env.putAll(newEnv);
+        pb.environment().putAll(environment);
         return pb.start();
     }
 
-    private Map<String, String> splitEnvironment(final String[] environment)
-    {
-        Map<String, String> newEnv = new HashMap<String, String>();
-        if (environment == null) {
-            // inherit the environment of the current process
-            newEnv.putAll(System.getenv());
-        } else {
-            // add the env entries
-            for (String env: environment) {
-                String[] values = env.split("=");
-                if (values.length > 1) {
-                    newEnv.put(values[0], values[1]);
-                }
-            }
-        }
-        return newEnv;
-    }
-
-    private Process createProcess(String[] cmdArray, String[] environment, File workingDir) throws IOException {
+    private Process createProcess(String[] cmdArray, Map<String, String> environment, File workingDir) throws IOException {
     	if (isWindows()) {
     		return createWinProcess(cmdArray, environment, workingDir);
     	} else {
                     .command(cmdArray)
                     .directory(workingDir);
 
-            processBuilder.environment().putAll(splitEnvironment(environment));
+            processBuilder.environment().putAll(environment);
 
     		return processBuilder.start();
     	}

src/main/java/com/atlassian/utils/process/ExternalProcessBuilder.java

             }
         }
         if (!environment.isEmpty()) {
-            String[] env = new String[environment.size()];
-            int index = 0;
-            for (Entry<String, String> entry : environment.entrySet()) {
-                env[index++] = entry.getKey() + "=" + entry.getValue();
-            }
-            process.setEnvironment(env);
+            process.setEnvironment(new HashMap<String, String>(environment));
         }
 
         return process;

src/test/java/com/atlassian/utils/process/CallEcho.java

 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 /**
                             System.err.println(line);
                         }
                     }).build();
-            process.setEnvironment(new String[] { "CLASSPATH=" + System.getProperty("java.class.path") });
+            
+            Map<String, String> env = new HashMap<String, String>();
+            env.put("CLASSPATH", System.getProperty("java.class.path"));
+
+            process.setEnvironment(env);
             process.execute();
 
             // output the expected and actual strings as a list of bytes to