Skip to content

Commit 14c2c3d

Browse files
authored
YARN-9512. [JDK11] TestAuxServices#testCustomizedAuxServiceClassPath fails because of ClassCastException. (#1806)
1 parent d7c4f8a commit 14c2c3d

File tree

1 file changed

+15
-7
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager

1 file changed

+15
-7
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/TestAuxServices.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161
import java.util.Arrays;
6262
import java.util.Collection;
6363
import java.util.HashSet;
64-
import java.util.List;
6564
import java.util.Map;
6665
import java.util.Map.Entry;
6766
import java.util.Set;
67+
import java.util.stream.Collectors;
68+
6869
import org.apache.hadoop.conf.Configuration;
6970
import org.apache.hadoop.fs.FileStatus;
7071
import org.apache.hadoop.fs.FileSystem;
@@ -242,13 +243,20 @@ public ServiceC() {
242243
@Override
243244
public ByteBuffer getMetaData() {
244245
ClassLoader loader = Thread.currentThread().getContextClassLoader();
245-
URL[] urls = ((URLClassLoader)loader).getURLs();
246-
List<String> urlString = new ArrayList<String>();
247-
for (URL url : urls) {
248-
urlString.add(url.toString());
246+
try {
247+
URL[] urls = ((URLClassLoader) loader).getURLs();
248+
String joinedString = Arrays.stream(urls)
249+
.map(URL::toString)
250+
.collect(Collectors.joining(","));
251+
return ByteBuffer.wrap(joinedString.getBytes());
252+
} catch (ClassCastException e) {
253+
// In Java 11+, Thread.currentThread().getContextClassLoader()
254+
// returns jdk.internal.loader.ClassLoaders$AppClassLoader
255+
// by default and it cannot cast to URLClassLoader.
256+
// This exception does not happen when the custom class loader is
257+
// used from AuxiliaryServiceWithCustomClassLoader.
258+
return super.meta;
249259
}
250-
String joinedString = StringUtils.join(",", urlString);
251-
return ByteBuffer.wrap(joinedString.getBytes());
252260
}
253261
}
254262

0 commit comments

Comments
 (0)