From b6c2c9058e83116dcca46cd6934db3428f931347 Mon Sep 17 00:00:00 2001 From: Arun Suresh Date: Wed, 11 Oct 2017 15:21:21 -0700 Subject: [PATCH] HADOOP-13556. Change Configuration.getPropsWithPrefix to use getProps instead of iterator. (Larry McCay via asuresh) --- .../org/apache/hadoop/conf/Configuration.java | 9 ++++++--- .../org/apache/hadoop/conf/TestConfiguration.java | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) 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 28908530ac8..9d5bb1bb0de 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 @@ -2700,11 +2700,14 @@ public class Configuration implements Iterable>, * @return mapping of configuration properties with prefix stripped */ public Map getPropsWithPrefix(String confPrefix) { + Properties props = getProps(); + Enumeration e = props.propertyNames(); Map configMap = new HashMap<>(); - for (Map.Entry entry : this) { - String name = entry.getKey(); + String name = null; + while (e.hasMoreElements()) { + name = (String) e.nextElement(); if (name.startsWith(confPrefix)) { - String value = this.get(name); + String value = props.getProperty(name); name = name.substring(confPrefix.length()); configMap.put(name, value); } 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 a806b8cb670..52215dae647 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 @@ -2242,6 +2242,21 @@ public class TestConfiguration { FileUtil.fullyDelete(tmpDir); } + public void testGettingPropertiesWithPrefix() throws Exception { + Configuration conf = new Configuration(); + for (int i = 0; i < 10; i++) { + conf.set("prefix" + ".name" + i, "value"); + } + conf.set("different.prefix" + ".name", "value"); + Map props = conf.getPropsWithPrefix("prefix"); + assertEquals(props.size(), 10); + + // test call with no properties for a given prefix + props = conf.getPropsWithPrefix("none"); + assertNotNull(props.isEmpty()); + assertTrue(props.isEmpty()); + } + public static void main(String[] argv) throws Exception { junit.textui.TestRunner.main(new String[]{ TestConfiguration.class.getName()