HADOOP-14067. VersionInfo should load version-info.properties from its own classloader. Contributed by Thejas M Nair.
(cherry picked from commit 91b98ad03a
)
This commit is contained in:
parent
7580c529a1
commit
7155ef28f9
|
@ -53,8 +53,7 @@ public class ThreadUtil {
|
||||||
* Convenience method that returns a resource as inputstream from the
|
* Convenience method that returns a resource as inputstream from the
|
||||||
* classpath.
|
* classpath.
|
||||||
* <p>
|
* <p>
|
||||||
* It first attempts to use the Thread's context classloader and if not
|
* Uses the Thread's context classloader to load resource.
|
||||||
* set it uses the class' classloader.
|
|
||||||
*
|
*
|
||||||
* @param resourceName resource to retrieve.
|
* @param resourceName resource to retrieve.
|
||||||
*
|
*
|
||||||
|
@ -68,6 +67,27 @@ public class ThreadUtil {
|
||||||
throw new IOException("Can not read resource file '" + resourceName +
|
throw new IOException("Can not read resource file '" + resourceName +
|
||||||
"' because class loader of the current thread is null");
|
"' because class loader of the current thread is null");
|
||||||
}
|
}
|
||||||
|
return getResourceAsStream(cl, resourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience method that returns a resource as inputstream from the
|
||||||
|
* classpath using given classloader.
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* @param cl ClassLoader to be used to retrieve resource.
|
||||||
|
* @param resourceName resource to retrieve.
|
||||||
|
*
|
||||||
|
* @throws IOException thrown if resource cannot be loaded
|
||||||
|
* @return inputstream with the resource.
|
||||||
|
*/
|
||||||
|
public static InputStream getResourceAsStream(ClassLoader cl,
|
||||||
|
String resourceName)
|
||||||
|
throws IOException {
|
||||||
|
if (cl == null) {
|
||||||
|
throw new IOException("Can not read resource file '" + resourceName +
|
||||||
|
"' because given class loader is null");
|
||||||
|
}
|
||||||
InputStream is = cl.getResourceAsStream(resourceName);
|
InputStream is = cl.getResourceAsStream(resourceName);
|
||||||
if (is == null) {
|
if (is == null) {
|
||||||
throw new IOException("Can not read resource file '" +
|
throw new IOException("Can not read resource file '" +
|
||||||
|
@ -75,4 +95,6 @@ public class ThreadUtil {
|
||||||
}
|
}
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,8 @@ public class VersionInfo {
|
||||||
String versionInfoFile = component + "-version-info.properties";
|
String versionInfoFile = component + "-version-info.properties";
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
try {
|
try {
|
||||||
is = ThreadUtil.getResourceAsStream(versionInfoFile);
|
is = ThreadUtil.getResourceAsStream(VersionInfo.class.getClassLoader(),
|
||||||
|
versionInfoFile);
|
||||||
info.load(is);
|
info.load(is);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LoggerFactory.getLogger(getClass()).warn("Could not read '" +
|
LoggerFactory.getLogger(getClass()).warn("Could not read '" +
|
||||||
|
|
Loading…
Reference in New Issue