MAPREDUCE-2472. Extra whitespace in mapred.child.java.opts breaks JVM initialization. Contributed by Aaron T. Myers.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/mapreduce/trunk@1099590 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 94bf9fb..18b9e56 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -692,6 +692,9 @@
     MAPREDUCE-2457. Job submission should inject group.name on the JobTracker
     (Alejandro Abdelnur via todd)
 
+    MAPREDUCE-2472. Extra whitespace in mapred.child.java.opts breaks JVM
+    initialization. (Aaron T. Myers via todd)
+
 Release 0.21.1 - Unreleased
 
   NEW FEATURES
diff --git a/src/java/org/apache/hadoop/mapred/TaskRunner.java b/src/java/org/apache/hadoop/mapred/TaskRunner.java
index 1b10e68..97afa26 100644
--- a/src/java/org/apache/hadoop/mapred/TaskRunner.java
+++ b/src/java/org/apache/hadoop/mapred/TaskRunner.java
@@ -331,6 +331,20 @@
   }
 
   /**
+   * Parse the given string and return an array of individual java opts. Split
+   * on whitespace and replace the special string "@taskid@" with the task ID
+   * given.
+   * 
+   * @param javaOpts The string to parse
+   * @param taskid The task ID to replace the special string with
+   * @return An array of individual java opts.
+   */
+  static String[] parseChildJavaOpts(String javaOpts, TaskAttemptID taskid) {
+    javaOpts = javaOpts.replace("@taskid@", taskid.toString());
+    return javaOpts.trim().split("\\s+");
+  }
+
+  /**
    * @param taskid
    * @param workDir
    * @param classPaths
@@ -375,10 +389,9 @@
     //    </value>
     //  </property>
     //
-    String javaOpts = getChildJavaOpts(conf, 
-                                       JobConf.DEFAULT_MAPRED_TASK_JAVA_OPTS);
-    javaOpts = javaOpts.replace("@taskid@", taskid.toString());
-    String [] javaOptsSplit = javaOpts.split(" ");
+    String[] javaOptsSplit = parseChildJavaOpts(getChildJavaOpts(conf,
+                                       JobConf.DEFAULT_MAPRED_TASK_JAVA_OPTS),
+                                       taskid);
     
     // Add java.library.path; necessary for loading native libraries.
     //
diff --git a/src/test/mapred/org/apache/hadoop/mapred/TestTaskChildOptsParsing.java b/src/test/mapred/org/apache/hadoop/mapred/TestTaskChildOptsParsing.java
new file mode 100644
index 0000000..d952ab8
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/mapred/TestTaskChildOptsParsing.java
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.mapred;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestTaskChildOptsParsing {
+  
+  @SuppressWarnings("deprecation")
+  private static final TaskAttemptID TASK_ID = new TaskAttemptID();
+  private static final String[] EXPECTED_RESULTS = new String[]{"-Dfoo=bar", "-Dbaz=biz"};
+  
+  private void performTest(String input) {
+    String[] result = TaskRunner.parseChildJavaOpts(input, TASK_ID);
+    assertArrayEquals(EXPECTED_RESULTS, result);
+  }
+  
+  @Test
+  public void testParseChildJavaOptsLeadingSpace() {
+    performTest(" -Dfoo=bar -Dbaz=biz");
+  }
+  
+  @Test
+  public void testParseChildJavaOptsTrailingSpace() {
+    performTest("-Dfoo=bar -Dbaz=biz ");
+  }
+  
+  @Test
+  public void testParseChildJavaOptsOneSpace() {
+    performTest("-Dfoo=bar -Dbaz=biz");
+  }
+  
+  @Test
+  public void testParseChildJavaOptsMulitpleSpaces() {
+    performTest("-Dfoo=bar  -Dbaz=biz");
+  }
+  
+  @Test
+  public void testParseChildJavaOptsOneTab() {
+    performTest("-Dfoo=bar\t-Dbaz=biz");
+  }
+  
+  @Test
+  public void testParseChildJavaOptsMultipleTabs() {
+    performTest("-Dfoo=bar\t\t-Dbaz=biz");
+  }
+}