Commits

James Dumay committed a00fb77 Merge

BAM-8352 Merge with Krystian

Comments (0)

Files changed (3)

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

       * 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, 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";
         for (int counter = 1; counter < cmdArray.length; counter++) {
             String envName = "JENV_" + counter;
             i18n[counter + extraArgs] = "%" + envName + "%";
-            environment.put(envName, quoteString(cmdArray[counter]));
+            newEnv.put(envName, quoteString(cmdArray[counter]));
         }
         cmdArray = i18n;
 
         ProcessBuilder pb = new ProcessBuilder(cmdArray);
         pb.directory(workingDir);
-        pb.environment().putAll(prepareEnvironment(environment));
+        pb.environment().putAll(environment);
         return pb.start();
     }
 
                     .command(cmdArray)
                     .directory(workingDir);
 
-            processBuilder.environment().putAll(prepareEnvironment(environment));
+            processBuilder.environment().putAll(environment);
 
     		return processBuilder.start();
     	}
     }
-
-    /**
-     * Adds quotes around the value of the environment variable to correctly escape spaces for non-windows platforms.
-     * @param environment
-     * @return environment
-     */
-    private Map<String, String> prepareEnvironment(Map<String, String> environment)
-    {
-        if (!isWindows())
-        {
-            final Map<String, String> newEnvironment = new HashMap<String, String>();
-            for (Map.Entry<String, String> entry : environment.entrySet())
-            {
-                final String newValue = "\"" + entry.getValue() + "\"";
-                newEnvironment.put(entry.getKey(), newValue);
-            }
-            return newEnvironment;
-        }
-        return environment;
-    }
     
     /**
      * Start the external process and setup the IO pump threads needed to

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;
     }
 

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

             // 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();
-            
-            Map<String, String> env = new HashMap<String, String>();
-            env.put("CLASSPATH", System.getProperty("java.class.path"));
-
-            process.setEnvironment(env);
+            process.setEnvironment(echoEnv);
             process.execute();
 
             // output the expected and actual strings as a list of bytes to
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.