package com.mapreduce.network;

import static com.mapreduce.config.Config.DEFAULT_USER_SSH;
import static com.mapreduce.config.Config.JAVA;
import static com.mapreduce.config.Config.SLAVE_JAR_LOCATION;
import static com.mapreduce.config.Config.SSH;

import java.util.Random;

import com.mapreduce.config.Config;
import com.mapreduce.system.SystemCommand;
import com.mapreduce.system.SystemCommand.Result;

public class SlaveHelper {

  public static int chooseSlaveIndex(int taskNumber, int numberOfSlaves) {
    return taskNumber % numberOfSlaves;
  }

  public static int chooseRandomSlaveIndex(int numberOfSlaves) {
    Random rand = new Random();
    return rand.nextInt(numberOfSlaves);
  }

  public static Result executeRemote(String slaveName, String... arguments) {
    String[] args = new String[arguments.length + 2];
    args[0] = SSH;
    args[1] = String.format("%s@%s", DEFAULT_USER_SSH, slaveName);
    args[2] = "export MAP_REDUCE_HOME=" + Config.MAP_REDUCE_HOME + ";";
    args[2] += JAVA;
    args[2] += "-jar";
    args[2] += SLAVE_JAR_LOCATION;
    System.arraycopy(arguments, 0, args, 2, arguments.length);
    return SystemCommand.execute(args);
  }

  public static Result executeLocal(String... arguments) {
    String[] args = new String[arguments.length + 3];
    args[0] = JAVA;
    args[1] = "-jar";
    args[2] = SLAVE_JAR_LOCATION;
    System.arraycopy(arguments, 0, args, 3, arguments.length);
    return SystemCommand.execute(args);
  }

}

@@ -243,16 +243,41 @@
 g.SSH;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -843,17 +843,17 @@
 ength + 
-2
+3
 %5D;%0A    a
@@ -1042,20 +1042,22 @@
 %5B2%5D += %22
+ 
 -jar
+ 
 %22;%0A    a
@@ -1127,17 +1127,17 @@
 , args, 
-2
+3
 , argume
@@ -1141,32 +1141,79 @@
 uments.length);%0A
+    System.out.println(Arrays.toString(args));%0A
     return Syste

Fix remote execution