Commits

krys...@nautilus.orbit  committed e951593

BAM-8356 processutils trim the environment setting on the first '='

  • Participants
  • Parent commits 4172249

Comments (0)

Files changed (3)

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

 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;
 
         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 {
+        Map<String, String> newEnv = new HashMap<String, String>(environment);
         int extraArgs = 4;
         String[] i18n = new String[cmdArray.length + extraArgs];
         i18n[0] = "cmd";
 
         ProcessBuilder pb = new ProcessBuilder(cmdArray);
         pb.directory(workingDir);
-        Map<String, String> env = pb.environment();
-        env.putAll(newEnv);
+        pb.environment().putAll(newEnv);
         return pb.start();
     }
 
         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();
     	}

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

 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import org.apache.log4j.Logger;
 import org.apache.log4j.Priority;
     }
 
     public ExternalProcessBuilder addMonitor(ProcessMonitor... monitors) {
-        for (ProcessMonitor mon : monitors) {
-            this.monitors.add(mon);
-        }
+        this.monitors.addAll(Arrays.asList(monitors));
         return this;
     }
 
                 process.addMonitor(monitor);
             }
         }
-        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(environment);
 
         return process;
     }

File 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;
 
 /**
             // using a java process at least gives us control over the encodings it uses. Java will use the default encoding (-Dfile.encoding) 
             // to convert command line arguments into Java Strings 
             List<String> echoCmd = Arrays.asList("java", "-Dfile.encoding=" + System.getProperty("file.encoding"), "com.atlassian.utils.process.Echo", echo);
-            
+            Map<String, String> echoEnv = new HashMap<String, String>()
+            {{
+                put("CLASSPATH", System.getProperty("java.class.path"));
+            }};
+
             // the command line arguments will be converted using the default encoding (file.encoding). 
             // The lineoutputhandler needs to use the same encoding to process the results. Otherwise, encoding errors will occur.
             ExternalProcess process = new ExternalProcessBuilder().command(echoCmd).timeout(2000)
                             System.err.println(line);
                         }
                     }).build();
-            process.setEnvironment(new String[] { "CLASSPATH=" + System.getProperty("java.class.path") });
+            process.setEnvironment(echoEnv);
             process.execute();
 
             // output the expected and actual strings as a list of bytes to