Commits

James Dumay committed 0bf9ad8

BAM-8297 have option to disable windows encoding work around. Use lists instead of arrays.

  • Participants
  • Parent commits 800e46b

Comments (0)

Files changed (2)

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

 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
  */
 public class ExternalProcess implements Watchdog {
     private boolean useWindowsEncodingWorkaround = true;
-    private String[] cmdArray;
+    private List<String> command;
     private File workingDir;
     private Map<String, String> environment;
     private ProcessHandler handler;
 
     /**
      * Process an external command.
-     * @param cmdArray the command and its arguments as separate elements
+     * @param command the command and its arguments as separate elements
      * @param handler The handler for this execution. The handler supports the required IO
      *                operations
      */
-    public ExternalProcess(String[] cmdArray, ProcessHandler handler) {
-        setCommand(cmdArray);
+    public ExternalProcess(String[] command, ProcessHandler handler) {
+        setCommand(Arrays.asList(command));
         setHandler(handler);
     }
 
      * @param handler The process handler to manage the execution of this process.
      */
     public ExternalProcess(List<String> command, ProcessHandler handler) {
-        setCommand(command.toArray(new String[command.size()]));
+        setCommand(command);
         setHandler(handler);
     }
 
      */
     public ExternalProcess(String commandLine, ProcessHandler handler) {
         String[] cmdArray = ProcessUtils.tokenizeCommand(commandLine);
-        setCommand(cmdArray);
+        setCommand(Arrays.asList(cmdArray));
         setHandler(handler);
     }
 
         this.handler = handler;
     }
 
-    private void setCommand(String[] cmdArray) {
-        this.cmdArray = cmdArray;
+    private void setCommand(List<String> command) {
+        this.command = command;
     }
 
     public void setWorkingDir(File workingDir) {
       * 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, Map<String, String> environment, File workingDir) throws IOException {
+    private Process createWinProcess(List<String> command, Map<String, String> environment, File workingDir) throws IOException {
         Map<String, String> newEnv = environment != null ? new HashMap<String, String>(environment) : new HashMap<String, String>();
-        int extraArgs = 4;
-        String[] i18n = new String[cmdArray.length + extraArgs];
-        i18n[0] = "cmd";
-        i18n[1] = "/A"; // use ANSI encoding 
-        i18n[2] = "/C";
-        i18n[3] = "call";
-        i18n[extraArgs] = cmdArray[0];
+
+        List<String> newCommand = new ArrayList<String>();
+        newCommand.add("cmd");
+        newCommand.add("/A");
+        newCommand.add("/C");
+        newCommand.add("call");
 
         if (useWindowsEncodingWorkaround)
         {
-            for (int counter = 1; counter < cmdArray.length; counter++) {
-                String envName = "JENV_" + counter;
-                i18n[counter + extraArgs] = "%" + envName + "%";
-                newEnv.put(envName, quoteString(cmdArray[counter]));
+            for (int counter = 1; counter < command.size(); counter++) {
+                final String envName = "JENV_" + counter;
+                newCommand.add("%" + envName + "%");
+                newEnv.put(envName, quoteString(command.get(counter)));
             }
         }
-        cmdArray = i18n;
+        else
+        {
+            newCommand.addAll(command);
+        }
 
-
-        ProcessBuilder pb = new ProcessBuilder(cmdArray);
+        ProcessBuilder pb = new ProcessBuilder(newCommand);
         pb.directory(workingDir);
         if (environment != null)
         {
         return pb.start();
     }
 
-    private Process createProcess(String[] cmdArray, Map<String, String> environment, File workingDir) throws IOException {
+    private Process createProcess(List<String> command, Map<String, String> environment, File workingDir) throws IOException {
     	if (isWindows()) {
-    		return createWinProcess(cmdArray, environment, workingDir);
+    		return createWinProcess(command, environment, workingDir);
     	} else {
             //But what about BeOS?!
             final ProcessBuilder processBuilder = new ProcessBuilder()
-                    .command(cmdArray)
+                    .command(command)
                     .directory(workingDir);
 
             if (environment != null)
     public void start() {
         try {
             this.startTime = System.currentTimeMillis();
-            this.process = createProcess(cmdArray, environment, workingDir);
+            this.process = createProcess(command, environment, workingDir);
             setupIOPumps();
         } catch (IOException e) {
             processException = new ProcessException(e);
 
     public String getCommandLine() {
         StringBuilder sb = new StringBuilder();
-        for (String s : cmdArray) {
+        for (String s : command) {
             sb.append(s);
             sb.append(" ");
         }

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

 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
             h = plugHandler;
         }
 
-        ExternalProcess process = new ExternalProcess(command, h);
+        ExternalProcess process = new ExternalProcess(new LinkedList<String>(command), h);
         if (timeout > 0L) {
             process.setTimeout(timeout);
         }