Skip to content

Commit e216bd2

Browse files
iwasakimsRogPodge
authored andcommitted
HDFS-14522. Allow compact property description in xml in httpfs. (apache#1737)
1 parent a57caf1 commit e216bd2

File tree

3 files changed

+36
-70
lines changed

3 files changed

+36
-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: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public void constructors() throws Exception {
4444
}
4545

4646

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

5456
@Test
@@ -124,4 +126,16 @@ public void testVarResolutionAndSysProps() {
124126
assertEquals(conf.get("user.name"), "foo");
125127
}
126128

129+
@Test
130+
public void testCompactFormatProperty() throws IOException {
131+
final String testfile = "test-compact-format-property.xml";
132+
Configuration conf = new Configuration(false);
133+
assertEquals(0, conf.size());
134+
ConfigurationUtils.load(conf,
135+
Thread.currentThread()
136+
.getContextClassLoader().getResource(testfile).openStream());
137+
assertEquals(2, conf.size());
138+
assertEquals("val1", conf.get("key.1"));
139+
assertEquals("val2", conf.get("key.2"));
140+
}
127141
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<configuration>
16+
<property name="key.1" value="val1" />
17+
<property name="key.2" value="val2" />
18+
</configuration>

0 commit comments

Comments
 (0)