HADOOP-17088. Failed to load XInclude files with relative path. (#2097)

Contributed by Yushi Hayasaka.

Change-Id: I8aad5143c34fb831bef0077f7b659643f8ae073a
This commit is contained in:
crossfire 2020-09-22 03:10:51 +09:00 committed by Steve Loughran
parent 7e642ec5a3
commit c3cb86ba42
No known key found for this signature in database
GPG Key ID: D22CF846DBB162A0
2 changed files with 43 additions and 1 deletions

View File

@ -40,6 +40,8 @@ import java.io.Writer;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
@ -3243,7 +3245,15 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
File href = new File(confInclude); File href = new File(confInclude);
if (!href.isAbsolute()) { if (!href.isAbsolute()) {
// Included resources are relative to the current resource // Included resources are relative to the current resource
File baseFile = new File(name).getParentFile(); File baseFile;
try {
baseFile = new File(new URI(name));
} catch (IllegalArgumentException | URISyntaxException e) {
baseFile = new File(name);
}
baseFile = baseFile.getParentFile();
href = new File(baseFile, href.getPath()); href = new File(baseFile, href.getPath());
} }
if (!href.exists()) { if (!href.exists()) {

View File

@ -1062,6 +1062,38 @@ public class TestConfiguration {
new File(new File(relConfig).getParent()).delete(); new File(new File(relConfig).getParent()).delete();
} }
@Test
public void testRelativeIncludesWithLoadingViaUri() throws Exception {
tearDown();
File configFile = new File("./tmp/test-config.xml");
File configFile2 = new File("./tmp/test-config2.xml");
new File(configFile.getParent()).mkdirs();
out = new BufferedWriter(new FileWriter(configFile2));
startConfig();
appendProperty("a", "b");
endConfig();
out = new BufferedWriter(new FileWriter(configFile));
startConfig();
// Add the relative path instead of the absolute one.
startInclude(configFile2.getName());
endInclude();
appendProperty("c", "d");
endConfig();
// verify that the includes file contains all properties
Path fileResource = new Path(configFile.toURI());
conf.addResource(fileResource);
assertEquals("b", conf.get("a"));
assertEquals("d", conf.get("c"));
// Cleanup
configFile.delete();
configFile2.delete();
new File(configFile.getParent()).delete();
}
@Test @Test
public void testIntegerRanges() { public void testIntegerRanges() {
Configuration conf = new Configuration(); Configuration conf = new Configuration();