HBASE-12491 TableMapReduceUtil.findContainingJar() NPE

This commit is contained in:
stack 2014-11-29 09:34:00 -08:00
parent 555e78005d
commit b94b0e9ca7
1 changed files with 19 additions and 16 deletions

View File

@ -900,25 +900,28 @@ public class TableMapReduceUtil {
private static String findContainingJar(Class<?> my_class, Map<String, String> packagedClasses) private static String findContainingJar(Class<?> my_class, Map<String, String> packagedClasses)
throws IOException { throws IOException {
ClassLoader loader = my_class.getClassLoader(); ClassLoader loader = my_class.getClassLoader();
String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; String class_file = my_class.getName().replaceAll("\\.", "/") + ".class";
// first search the classpath if (loader != null) {
for (Enumeration<URL> itr = loader.getResources(class_file); itr.hasMoreElements();) { // first search the classpath
URL url = itr.nextElement(); for (Enumeration<URL> itr = loader.getResources(class_file); itr.hasMoreElements();) {
if ("jar".equals(url.getProtocol())) { URL url = itr.nextElement();
String toReturn = url.getPath(); if ("jar".equals(url.getProtocol())) {
if (toReturn.startsWith("file:")) { String toReturn = url.getPath();
toReturn = toReturn.substring("file:".length()); if (toReturn.startsWith("file:")) {
toReturn = toReturn.substring("file:".length());
}
// URLDecoder is a misnamed class, since it actually decodes
// x-www-form-urlencoded MIME type rather than actual
// URL encoding (which the file path has). Therefore it would
// decode +s to ' 's which is incorrect (spaces are actually
// either unencoded or encoded as "%20"). Replace +s first, so
// that they are kept sacred during the decoding process.
toReturn = toReturn.replaceAll("\\+", "%2B");
toReturn = URLDecoder.decode(toReturn, "UTF-8");
return toReturn.replaceAll("!.*$", "");
} }
// URLDecoder is a misnamed class, since it actually decodes
// x-www-form-urlencoded MIME type rather than actual
// URL encoding (which the file path has). Therefore it would
// decode +s to ' 's which is incorrect (spaces are actually
// either unencoded or encoded as "%20"). Replace +s first, so
// that they are kept sacred during the decoding process.
toReturn = toReturn.replaceAll("\\+", "%2B");
toReturn = URLDecoder.decode(toReturn, "UTF-8");
return toReturn.replaceAll("!.*$", "");
} }
} }