Skip to content

Commit 397adc9

Browse files
committed
HDFS-14522. Allow compact property description in xml in httpfs.
1 parent ccca5f4 commit 397adc9

File tree

2 files changed

+25
-70
lines changed

2 files changed

+25
-70
lines changed

hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/util/ConfigurationUtils.java

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@
2020

2121
import org.apache.hadoop.classification.InterfaceAudience;
2222
import org.apache.hadoop.conf.Configuration;
23-
import org.w3c.dom.DOMException;
24-
import org.w3c.dom.Document;
25-
import org.w3c.dom.Element;
26-
import org.w3c.dom.Node;
27-
import org.w3c.dom.NodeList;
28-
import org.w3c.dom.Text;
29-
import org.xml.sax.SAXException;
3023

31-
import javax.xml.parsers.DocumentBuilder;
32-
import javax.xml.parsers.DocumentBuilderFactory;
33-
import javax.xml.parsers.ParserConfigurationException;
3424
import java.io.IOException;
3525
import java.io.InputStream;
3626
import java.util.Map;
@@ -98,62 +88,6 @@ public static Configuration resolve(Configuration conf) {
9888
* @throws IOException thrown if the configuration could not be read.
9989
*/
10090
public static void load(Configuration conf, InputStream is) throws IOException {
101-
try {
102-
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
103-
// ignore all comments inside the xml file
104-
docBuilderFactory.setIgnoringComments(true);
105-
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
106-
Document doc = builder.parse(is);
107-
parseDocument(conf, doc);
108-
} catch (SAXException e) {
109-
throw new IOException(e);
110-
} catch (ParserConfigurationException e) {
111-
throw new IOException(e);
112-
}
113-
}
114-
115-
// Canibalized from FileSystemAccess <code>Configuration.loadResource()</code>.
116-
private static void parseDocument(Configuration conf, Document doc) throws IOException {
117-
try {
118-
Element root = doc.getDocumentElement();
119-
if (!"configuration".equals(root.getTagName())) {
120-
throw new IOException("bad conf file: top-level element not <configuration>");
121-
}
122-
NodeList props = root.getChildNodes();
123-
for (int i = 0; i < props.getLength(); i++) {
124-
Node propNode = props.item(i);
125-
if (!(propNode instanceof Element)) {
126-
continue;
127-
}
128-
Element prop = (Element) propNode;
129-
if (!"property".equals(prop.getTagName())) {
130-
throw new IOException("bad conf file: element not <property>");
131-
}
132-
NodeList fields = prop.getChildNodes();
133-
String attr = null;
134-
String value = null;
135-
for (int j = 0; j < fields.getLength(); j++) {
136-
Node fieldNode = fields.item(j);
137-
if (!(fieldNode instanceof Element)) {
138-
continue;
139-
}
140-
Element field = (Element) fieldNode;
141-
if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
142-
attr = ((Text) field.getFirstChild()).getData().trim();
143-
}
144-
if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
145-
value = ((Text) field.getFirstChild()).getData();
146-
}
147-
}
148-
149-
if (attr != null && value != null) {
150-
conf.set(attr, value);
151-
}
152-
}
153-
154-
} catch (DOMException e) {
155-
throw new IOException(e);
156-
}
91+
conf.addResource(is);
15792
}
158-
15993
}

hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/util/TestConfigurationUtils.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertNull;
2323

24+
import java.io.BufferedWriter;
2425
import java.io.ByteArrayInputStream;
2526
import java.io.IOException;
2627
import java.io.InputStream;
28+
import java.io.StringWriter;
2729

2830
import org.apache.hadoop.conf.Configuration;
2931
import org.junit.Test;
@@ -44,11 +46,12 @@ public void constructors() throws Exception {
4446
}
4547

4648

47-
@Test(expected = IOException.class)
48-
public void constructorsFail3() throws Exception {
49-
InputStream is = new ByteArrayInputStream("<xonfiguration></xonfiguration>".getBytes());
49+
@Test
50+
public void constructors3() throws Exception {
51+
InputStream is = new ByteArrayInputStream("<xonfiguration><property name=\"key1\" value=\"val1\"/></xonfiguration>".getBytes());
5052
Configuration conf = new Configuration(false);
5153
ConfigurationUtils.load(conf, is);
54+
assertEquals(conf.get("key1"), "val1");
5255
}
5356

5457
@Test
@@ -124,4 +127,22 @@ public void testVarResolutionAndSysProps() {
124127
assertEquals(conf.get("user.name"), "foo");
125128
}
126129

130+
@Test
131+
public void testCompactFormatProperty() throws IOException {
132+
Configuration conf = new Configuration(false);
133+
assertEquals(conf.size(), 0);
134+
StringWriter writer = new StringWriter();
135+
BufferedWriter out = new BufferedWriter(writer);
136+
out.write("<?xml version=\"1.0\"?>\n");
137+
out.write("<configuration>\n");
138+
out.write("<property name=\"key1\" value=\"val1\"/>");
139+
out.write("</configuration>\n");
140+
out.flush();
141+
out.close();
142+
InputStream is = new ByteArrayInputStream(writer.toString().getBytes());
143+
conf = new Configuration(false);
144+
ConfigurationUtils.load(conf, is);
145+
assertEquals(conf.size(), 1);
146+
assertEquals(conf.get("key1"), "val1");
147+
}
127148
}

0 commit comments

Comments
 (0)