Skip to content

Commit acec539

Browse files
virajjasanisteveloughran
authored andcommitted
HADOOP-18931. FileSystem.getFileSystemClass() to log the jar the .class came from (#6197)
Set the log level of logger org.apache.hadoop.fs.FileSystem to DEBUG to see this. Contributed by Viraj Jasani
1 parent 4eeb103 commit acec539

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3581,7 +3581,15 @@ public static Class<? extends FileSystem> getFileSystemClass(String scheme,
35813581
throw new UnsupportedFileSystemException("No FileSystem for scheme "
35823582
+ "\"" + scheme + "\"");
35833583
}
3584-
LOGGER.debug("FS for {} is {}", scheme, clazz);
3584+
if (LOGGER.isDebugEnabled()) {
3585+
LOGGER.debug("FS for {} is {}", scheme, clazz);
3586+
final String jarLocation = ClassUtil.findContainingJar(clazz);
3587+
if (jarLocation != null) {
3588+
LOGGER.debug("Jar location for {} : {}", clazz, jarLocation);
3589+
} else {
3590+
LOGGER.debug("Class location for {} : {}", clazz, ClassUtil.findClassLocation(clazz));
3591+
}
3592+
}
35853593
return clazz;
35863594
}
35873595

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ClassUtil.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ public class ClassUtil {
3636
* @return a jar file that contains the class, or null.
3737
*/
3838
public static String findContainingJar(Class<?> clazz) {
39-
ClassLoader loader = clazz.getClassLoader();
40-
String classFile = clazz.getName().replaceAll("\\.", "/") + ".class";
39+
return findContainingResource(clazz.getClassLoader(), clazz.getName(), "jar");
40+
}
41+
42+
/**
43+
* Find the absolute location of the class.
44+
*
45+
* @param clazz the class to find.
46+
* @return the class file with absolute location, or null.
47+
*/
48+
public static String findClassLocation(Class<?> clazz) {
49+
return findContainingResource(clazz.getClassLoader(), clazz.getName(), "file");
50+
}
51+
52+
private static String findContainingResource(ClassLoader loader, String clazz, String resource) {
53+
String classFile = clazz.replaceAll("\\.", "/") + ".class";
4154
try {
42-
for(final Enumeration<URL> itr = loader.getResources(classFile);
43-
itr.hasMoreElements();) {
55+
for (final Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) {
4456
final URL url = itr.nextElement();
45-
if ("jar".equals(url.getProtocol())) {
57+
if (resource.equals(url.getProtocol())) {
4658
String toReturn = url.getPath();
4759
if (toReturn.startsWith("file:")) {
4860
toReturn = toReturn.substring("file:".length());

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestClassUtil.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,47 @@
2020

2121
import java.io.File;
2222

23-
import org.junit.Assert;
23+
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
2424

25-
import org.apache.log4j.Logger;
25+
import org.assertj.core.api.Assertions;
2626
import org.junit.Test;
2727

2828
public class TestClassUtil {
29+
2930
@Test(timeout=10000)
3031
public void testFindContainingJar() {
31-
String containingJar = ClassUtil.findContainingJar(Logger.class);
32-
Assert.assertNotNull("Containing jar not found for Logger",
33-
containingJar);
32+
String containingJar = ClassUtil.findContainingJar(Assertions.class);
33+
Assertions
34+
.assertThat(containingJar)
35+
.describedAs("Containing jar for %s", Assertions.class)
36+
.isNotNull();
3437
File jarFile = new File(containingJar);
35-
Assert.assertTrue("Containing jar does not exist on file system ",
36-
jarFile.exists());
37-
Assert.assertTrue("Incorrect jar file " + containingJar,
38-
jarFile.getName().matches("reload4j.*[.]jar"));
38+
Assertions
39+
.assertThat(jarFile)
40+
.describedAs("Containing jar %s", jarFile)
41+
.exists();
42+
Assertions
43+
.assertThat(jarFile.getName())
44+
.describedAs("Containing jar name %s", jarFile.getName())
45+
.matches("assertj-core.*[.]jar");
46+
}
47+
48+
@Test(timeout = 10000)
49+
public void testFindContainingClass() {
50+
String classFileLocation = ClassUtil.findClassLocation(ViewFileSystem.class);
51+
Assertions
52+
.assertThat(classFileLocation)
53+
.describedAs("Class path for %s", ViewFileSystem.class)
54+
.isNotNull();
55+
File classFile = new File(classFileLocation);
56+
Assertions
57+
.assertThat(classFile)
58+
.describedAs("Containing class file %s", classFile)
59+
.exists();
60+
Assertions
61+
.assertThat(classFile.getName())
62+
.describedAs("Containing class file name %s", classFile.getName())
63+
.matches("ViewFileSystem.class");
3964
}
65+
4066
}

0 commit comments

Comments
 (0)