From 4bea96f9a84cee89d07dfa97b892f6fb3ed1e125 Mon Sep 17 00:00:00 2001 From: Jitendra Pandey Date: Thu, 22 Mar 2018 14:08:18 -0700 Subject: [PATCH] HADOOP-14067. VersionInfo should load version-info.properties from its own classloader. Contributed by Thejas M Nair. --- .../org/apache/hadoop/util/ThreadUtil.java | 26 +++++++++++++++++-- .../org/apache/hadoop/util/VersionInfo.java | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java index 6444428a27..2cda8a443e 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java @@ -53,8 +53,7 @@ public static void sleepAtLeastIgnoreInterrupts(long millis) { * Convenience method that returns a resource as inputstream from the * classpath. *

- * It first attempts to use the Thread's context classloader and if not - * set it uses the class' classloader. + * Uses the Thread's context classloader to load resource. * * @param resourceName resource to retrieve. * @@ -68,6 +67,27 @@ public static InputStream getResourceAsStream(String resourceName) throw new IOException("Can not read resource file '" + resourceName + "' 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. + *

+ * + * @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); if (is == null) { throw new IOException("Can not read resource file '" + @@ -75,4 +95,6 @@ public static InputStream getResourceAsStream(String resourceName) } return is; } + + } diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/VersionInfo.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/VersionInfo.java index ca09050088..ea835023e8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/VersionInfo.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/VersionInfo.java @@ -43,7 +43,8 @@ protected VersionInfo(String component) { String versionInfoFile = component + "-version-info.properties"; InputStream is = null; try { - is = ThreadUtil.getResourceAsStream(versionInfoFile); + is = ThreadUtil.getResourceAsStream(VersionInfo.class.getClassLoader(), + versionInfoFile); info.load(is); } catch (IOException ex) { LoggerFactory.getLogger(getClass()).warn("Could not read '" +