You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by iw...@apache.org on 2019/12/10 01:31:12 UTC

[hadoop] branch trunk updated: HDFS-14522. Allow compact property description in xml in httpfs. (#1737)

This is an automated email from the ASF dual-hosted git repository.

iwasakims pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 4dffd81  HDFS-14522. Allow compact property description in xml in httpfs. (#1737)
4dffd81 is described below

commit 4dffd81bb75efaa5742d2246354ebdc86cbd1aab
Author: Masatake Iwasaki <iw...@apache.org>
AuthorDate: Tue Dec 10 10:30:57 2019 +0900

    HDFS-14522. Allow compact property description in xml in httpfs. (#1737)
---
 .../apache/hadoop/lib/util/ConfigurationUtils.java | 68 +---------------------
 .../hadoop/lib/util/TestConfigurationUtils.java    | 20 ++++++-
 .../resources/test-compact-format-property.xml     | 18 ++++++
 3 files changed, 36 insertions(+), 70 deletions(-)

diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/util/ConfigurationUtils.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/util/ConfigurationUtils.java
index 6611dd2..baecce4 100644
--- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/util/ConfigurationUtils.java
+++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/util/ConfigurationUtils.java
@@ -20,17 +20,7 @@ package org.apache.hadoop.lib.util;
 
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.conf.Configuration;
-import org.w3c.dom.DOMException;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
-import org.xml.sax.SAXException;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Map;
@@ -98,62 +88,6 @@ public abstract class ConfigurationUtils {
    * @throws IOException thrown if the configuration could not be read.
    */
   public static void load(Configuration conf, InputStream is) throws IOException {
-    try {
-      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
-      // ignore all comments inside the xml file
-      docBuilderFactory.setIgnoringComments(true);
-      DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
-      Document doc = builder.parse(is);
-      parseDocument(conf, doc);
-    } catch (SAXException e) {
-      throw new IOException(e);
-    } catch (ParserConfigurationException e) {
-      throw new IOException(e);
-    }
-  }
-
-  // Canibalized from FileSystemAccess <code>Configuration.loadResource()</code>.
-  private static void parseDocument(Configuration conf, Document doc) throws IOException {
-    try {
-      Element root = doc.getDocumentElement();
-      if (!"configuration".equals(root.getTagName())) {
-        throw new IOException("bad conf file: top-level element not <configuration>");
-      }
-      NodeList props = root.getChildNodes();
-      for (int i = 0; i < props.getLength(); i++) {
-        Node propNode = props.item(i);
-        if (!(propNode instanceof Element)) {
-          continue;
-        }
-        Element prop = (Element) propNode;
-        if (!"property".equals(prop.getTagName())) {
-          throw new IOException("bad conf file: element not <property>");
-        }
-        NodeList fields = prop.getChildNodes();
-        String attr = null;
-        String value = null;
-        for (int j = 0; j < fields.getLength(); j++) {
-          Node fieldNode = fields.item(j);
-          if (!(fieldNode instanceof Element)) {
-            continue;
-          }
-          Element field = (Element) fieldNode;
-          if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
-            attr = ((Text) field.getFirstChild()).getData().trim();
-          }
-          if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
-            value = ((Text) field.getFirstChild()).getData();
-          }
-        }
-
-        if (attr != null && value != null) {
-          conf.set(attr, value);
-        }
-      }
-
-    } catch (DOMException e) {
-      throw new IOException(e);
-    }
+    conf.addResource(is);
   }
-
 }
diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/util/TestConfigurationUtils.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/util/TestConfigurationUtils.java
index 925edc5..b868d0b 100644
--- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/util/TestConfigurationUtils.java
+++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/util/TestConfigurationUtils.java
@@ -44,11 +44,13 @@ public class TestConfigurationUtils {
   }
 
 
-  @Test(expected = IOException.class)
-  public void constructorsFail3() throws Exception {
-    InputStream is = new ByteArrayInputStream("<xonfiguration></xonfiguration>".getBytes());
+  @Test
+  public void constructors3() throws Exception {
+    InputStream is = new ByteArrayInputStream(
+        "<xxx><property name=\"key1\" value=\"val1\"/></xxx>".getBytes());
     Configuration conf = new Configuration(false);
     ConfigurationUtils.load(conf, is);
+    assertEquals("val1", conf.get("key1"));
   }
 
   @Test
@@ -124,4 +126,16 @@ public class TestConfigurationUtils {
     assertEquals(conf.get("user.name"), "foo");
   }
 
+  @Test
+  public void testCompactFormatProperty() throws IOException {
+    final String testfile = "test-compact-format-property.xml";
+    Configuration conf = new Configuration(false);
+    assertEquals(0, conf.size());
+    ConfigurationUtils.load(conf,
+        Thread.currentThread()
+            .getContextClassLoader().getResource(testfile).openStream());
+    assertEquals(2, conf.size());
+    assertEquals("val1", conf.get("key.1"));
+    assertEquals("val2", conf.get("key.2"));
+  }
 }
diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/test-compact-format-property.xml b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/test-compact-format-property.xml
new file mode 100644
index 0000000..f216c4c
--- /dev/null
+++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/test-compact-format-property.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed 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.
+-->
+<configuration>
+  <property name="key.1" value="val1" />
+  <property name="key.2" value="val2" />
+</configuration>


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org