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
This commit is contained in:
parent
2bde5ccb81
commit
240fddcf17
@ -3581,7 +3581,15 @@ public static Class<? extends FileSystem> getFileSystemClass(String scheme,
|
|||||||
throw new UnsupportedFileSystemException("No FileSystem for scheme "
|
throw new UnsupportedFileSystemException("No FileSystem for scheme "
|
||||||
+ "\"" + scheme + "\"");
|
+ "\"" + scheme + "\"");
|
||||||
}
|
}
|
||||||
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.debug("FS for {} is {}", scheme, clazz);
|
LOGGER.debug("FS for {} is {}", scheme, clazz);
|
||||||
|
final String jarLocation = ClassUtil.findContainingJar(clazz);
|
||||||
|
if (jarLocation != null) {
|
||||||
|
LOGGER.debug("Jar location for {} : {}", clazz, jarLocation);
|
||||||
|
} else {
|
||||||
|
LOGGER.debug("Class location for {} : {}", clazz, ClassUtil.findClassLocation(clazz));
|
||||||
|
}
|
||||||
|
}
|
||||||
return clazz;
|
return clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,13 +36,25 @@ public class ClassUtil {
|
|||||||
* @return a jar file that contains the class, or null.
|
* @return a jar file that contains the class, or null.
|
||||||
*/
|
*/
|
||||||
public static String findContainingJar(Class<?> clazz) {
|
public static String findContainingJar(Class<?> clazz) {
|
||||||
ClassLoader loader = clazz.getClassLoader();
|
return findContainingResource(clazz.getClassLoader(), clazz.getName(), "jar");
|
||||||
String classFile = clazz.getName().replaceAll("\\.", "/") + ".class";
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the absolute location of the class.
|
||||||
|
*
|
||||||
|
* @param clazz the class to find.
|
||||||
|
* @return the class file with absolute location, or null.
|
||||||
|
*/
|
||||||
|
public static String findClassLocation(Class<?> clazz) {
|
||||||
|
return findContainingResource(clazz.getClassLoader(), clazz.getName(), "file");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String findContainingResource(ClassLoader loader, String clazz, String resource) {
|
||||||
|
String classFile = clazz.replaceAll("\\.", "/") + ".class";
|
||||||
try {
|
try {
|
||||||
for(final Enumeration<URL> itr = loader.getResources(classFile);
|
for (final Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) {
|
||||||
itr.hasMoreElements();) {
|
|
||||||
final URL url = itr.nextElement();
|
final URL url = itr.nextElement();
|
||||||
if ("jar".equals(url.getProtocol())) {
|
if (resource.equals(url.getProtocol())) {
|
||||||
String toReturn = url.getPath();
|
String toReturn = url.getPath();
|
||||||
if (toReturn.startsWith("file:")) {
|
if (toReturn.startsWith("file:")) {
|
||||||
toReturn = toReturn.substring("file:".length());
|
toReturn = toReturn.substring("file:".length());
|
||||||
|
@ -20,21 +20,47 @@
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestClassUtil {
|
public class TestClassUtil {
|
||||||
|
|
||||||
@Test(timeout=10000)
|
@Test(timeout=10000)
|
||||||
public void testFindContainingJar() {
|
public void testFindContainingJar() {
|
||||||
String containingJar = ClassUtil.findContainingJar(Logger.class);
|
String containingJar = ClassUtil.findContainingJar(Assertions.class);
|
||||||
Assert.assertNotNull("Containing jar not found for Logger",
|
Assertions
|
||||||
containingJar);
|
.assertThat(containingJar)
|
||||||
|
.describedAs("Containing jar for %s", Assertions.class)
|
||||||
|
.isNotNull();
|
||||||
File jarFile = new File(containingJar);
|
File jarFile = new File(containingJar);
|
||||||
Assert.assertTrue("Containing jar does not exist on file system ",
|
Assertions
|
||||||
jarFile.exists());
|
.assertThat(jarFile)
|
||||||
Assert.assertTrue("Incorrect jar file " + containingJar,
|
.describedAs("Containing jar %s", jarFile)
|
||||||
jarFile.getName().matches("reload4j.*[.]jar"));
|
.exists();
|
||||||
|
Assertions
|
||||||
|
.assertThat(jarFile.getName())
|
||||||
|
.describedAs("Containing jar name %s", jarFile.getName())
|
||||||
|
.matches("assertj-core.*[.]jar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 10000)
|
||||||
|
public void testFindContainingClass() {
|
||||||
|
String classFileLocation = ClassUtil.findClassLocation(ViewFileSystem.class);
|
||||||
|
Assertions
|
||||||
|
.assertThat(classFileLocation)
|
||||||
|
.describedAs("Class path for %s", ViewFileSystem.class)
|
||||||
|
.isNotNull();
|
||||||
|
File classFile = new File(classFileLocation);
|
||||||
|
Assertions
|
||||||
|
.assertThat(classFile)
|
||||||
|
.describedAs("Containing class file %s", classFile)
|
||||||
|
.exists();
|
||||||
|
Assertions
|
||||||
|
.assertThat(classFile.getName())
|
||||||
|
.describedAs("Containing class file name %s", classFile.getName())
|
||||||
|
.matches("ViewFileSystem.class");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user