diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index aa93ffc58f..c4aaeeb391 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -334,6 +334,9 @@ Trunk (Unreleased) HADOOP-10586. KeyShell doesn't allow setting Options via CLI. (clamb via tucu) + HADOOP-10625. Trim configuration names when putting/getting them + to properties. (Wangda Tan via xgong) + OPTIMIZATIONS HADOOP-7761. Improve the performance of raw comparisons. (todd) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java index af345c27cc..2298d41722 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java @@ -566,6 +566,9 @@ public class Configuration implements Iterable>, */ private String[] handleDeprecation(DeprecationContext deprecations, String name) { + if (null != name) { + name = name.trim(); + } ArrayList names = new ArrayList(); if (isDeprecated(name)) { DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name); @@ -843,12 +846,12 @@ public class Configuration implements Iterable>, /** * Get the value of the name property, null if * no such property exists. If the key is deprecated, it returns the value of - * the first key which replaces the deprecated key and is not null + * the first key which replaces the deprecated key and is not null. * * Values are processed for variable expansion * before being returned. * - * @param name the property name. + * @param name the property name, will be trimmed before get value. * @return the value of the name or its replacing property, * or null if no such property exists. */ @@ -952,7 +955,8 @@ public class Configuration implements Iterable>, /** * Set the value of the name property. If * name is deprecated or there is a deprecated name associated to it, - * it sets the value to both names. + * it sets the value to both names. Name will be trimmed before put into + * configuration. * * @param name property name. * @param value property value. @@ -964,7 +968,8 @@ public class Configuration implements Iterable>, /** * Set the value of the name property. If * name is deprecated, it also sets the value to - * the keys that replace the deprecated key. + * the keys that replace the deprecated key. Name will be trimmed before put + * into configuration. * * @param name property name. * @param value property value. @@ -979,6 +984,7 @@ public class Configuration implements Iterable>, Preconditions.checkArgument( value != null, "The value of property " + name + " must not be null"); + name = name.trim(); DeprecationContext deprecations = deprecationContext.get(); if (deprecations.getDeprecatedKeyMap().isEmpty()) { getProps(); @@ -1064,7 +1070,7 @@ public class Configuration implements Iterable>, * If no such property exists, * then defaultValue is returned. * - * @param name property name. + * @param name property name, will be trimmed before get value. * @param defaultValue default value. * @return property value, or defaultValue if the property * doesn't exist. diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java index 1ce0b01085..60b86e4442 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java @@ -49,7 +49,7 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.net.NetUtils; import static org.apache.hadoop.util.PlatformName.IBM_JAVA; -import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.ObjectMapper; public class TestConfiguration extends TestCase { @@ -1003,6 +1003,14 @@ public class TestConfiguration extends TestCase { String resource; } + public void testGetSetTrimmedNames() throws IOException { + Configuration conf = new Configuration(false); + conf.set(" name", "value"); + assertEquals("value", conf.get("name")); + assertEquals("value", conf.get(" name")); + assertEquals("value", conf.getRaw(" name ")); + } + public void testDumpConfiguration () throws IOException { StringWriter outWriter = new StringWriter(); Configuration.dumpConfiguration(conf, outWriter);