From af3aadf04f0d6aff19fff99fe18c9b3feae2c529 Mon Sep 17 00:00:00 2001 From: Allen Wittenauer Date: Thu, 5 Feb 2015 19:09:37 -0800 Subject: [PATCH] HADOOP-6964. Allow compact property description in xml (Kengo Seki via aw) --- .../hadoop-common/CHANGES.txt | 3 + .../org/apache/hadoop/conf/Configuration.java | 18 +++++- .../apache/hadoop/conf/TestConfiguration.java | 55 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index aee3a23b60..b323f32f2c 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -160,6 +160,9 @@ Trunk (Unreleased) HADOOP-7713. dfs -count -q should label output column (Jonathan Allen via aw) + HADOOP-6964. Allow compact property description in xml (Kengo Seki + via aw) + BUG FIXES HADOOP-11473. test-patch says "-1 overall" even when all checks are +1 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 54ee46d995..8f98d0a89e 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 @@ -90,6 +90,7 @@ import org.apache.hadoop.util.StringUtils; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; +import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -2514,11 +2515,26 @@ private Resource loadResource(Properties properties, Resource wrapper, boolean q } if (!"property".equals(prop.getTagName())) LOG.warn("bad conf file: element not "); - NodeList fields = prop.getChildNodes(); + String attr = null; String value = null; boolean finalParameter = false; LinkedList source = new LinkedList(); + + Attr propAttr = prop.getAttributeNode("name"); + if (propAttr != null) + attr = StringInterner.weakIntern(propAttr.getValue()); + propAttr = prop.getAttributeNode("value"); + if (propAttr != null) + value = StringInterner.weakIntern(propAttr.getValue()); + propAttr = prop.getAttributeNode("final"); + if (propAttr != null) + finalParameter = "true".equals(propAttr.getValue()); + propAttr = prop.getAttributeNode("source"); + if (propAttr != null) + source.add(StringInterner.weakIntern(propAttr.getValue())); + + NodeList fields = prop.getChildNodes(); for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) 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 55bcdc6119..7b4fbb5ba3 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 @@ -209,6 +209,31 @@ public void testFinalParam() throws IOException { assertNull("my var is not final", conf2.get("my.var")); } + public void testCompactFormat() throws IOException { + out=new BufferedWriter(new FileWriter(CONFIG)); + startConfig(); + appendCompactFormatProperty("a", "b"); + appendCompactFormatProperty("c", "d", true); + appendCompactFormatProperty("e", "f", false, "g"); + endConfig(); + Path fileResource = new Path(CONFIG); + Configuration conf = new Configuration(false); + conf.addResource(fileResource); + + assertEquals("b", conf.get("a")); + + assertEquals("d", conf.get("c")); + Set s = conf.getFinalParameters(); + assertEquals(1, s.size()); + assertTrue(s.contains("c")); + + assertEquals("f", conf.get("e")); + String[] sources = conf.getPropertySources("e"); + assertEquals(2, sources.length); + assertEquals("g", sources[0]); + assertEquals(fileResource.toString(), sources[1]); + } + public static void assertEq(Object a, Object b) { System.out.println("assertEq: " + a + ", " + b); assertEquals(a, b); @@ -264,6 +289,36 @@ void appendProperty(String name, String val, boolean isFinal, out.write("\n"); } + void appendCompactFormatProperty(String name, String val) throws IOException { + appendCompactFormatProperty(name, val, false); + } + + void appendCompactFormatProperty(String name, String val, boolean isFinal) + throws IOException { + appendCompactFormatProperty(name, val, isFinal, null); + } + + void appendCompactFormatProperty(String name, String val, boolean isFinal, + String source) + throws IOException { + out.write("\n"); + } + public void testOverlay() throws IOException{ out=new BufferedWriter(new FileWriter(CONFIG)); startConfig();