Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Commit 1cf4665

Browse files
committed
Make classloader handling more compatible with JDK 9
From http://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html: The application class loader is no longer an instance of java.net.URLClassLoader (an implementation detail that was never specified in previous releases). Code that assumes that ClassLoader::getSytemClassLoader returns a URLClassLoader object will need to be updated. Note that Java SE and the JDK do not provide an API for applications or libraries to dynamically augment the class path at run-time.
1 parent 85556d0 commit 1cf4665

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

sdk/src/main/java/com/google/cloud/dataflow/sdk/runners/DataflowPipelineRunner.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@
136136
import com.google.common.base.Function;
137137
import com.google.common.base.Joiner;
138138
import com.google.common.base.Optional;
139+
import com.google.common.base.Splitter;
140+
import com.google.common.base.StandardSystemProperty;
139141
import com.google.common.base.Strings;
140142
import com.google.common.base.Utf8;
141143
import com.google.common.collect.ForwardingMap;
@@ -3198,23 +3200,30 @@ public String toString() {
31983200
* @return A list of absolute paths to the resources the class loader uses.
31993201
*/
32003202
protected static List<String> detectClassPathResourcesToStage(ClassLoader classLoader) {
3201-
if (!(classLoader instanceof URLClassLoader)) {
3203+
List<String> files = new ArrayList<>();
3204+
if (classLoader == ClassLoader.getSystemClassLoader()) {
3205+
for (String element :
3206+
Splitter.on(File.pathSeparatorChar)
3207+
.split(StandardSystemProperty.JAVA_CLASS_PATH.value())) {
3208+
files.add(new File(element).getAbsolutePath());
3209+
}
3210+
} else if (classLoader instanceof URLClassLoader) {
3211+
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
3212+
try {
3213+
files.add(new File(url.toURI()).getAbsolutePath());
3214+
} catch (IllegalArgumentException | URISyntaxException e) {
3215+
String message = String.format("Unable to convert url (%s) to file.", url);
3216+
LOG.error(message);
3217+
throw new IllegalArgumentException(message, e);
3218+
}
3219+
}
3220+
} else {
32023221
String message = String.format("Unable to use ClassLoader to detect classpath elements. "
3203-
+ "Current ClassLoader is %s, only URLClassLoaders are supported.", classLoader);
3222+
+ "Current ClassLoader is %s, only URLClassLoaders and the system ClassLoader are "
3223+
+ "supported.", classLoader);
32043224
LOG.error(message);
32053225
throw new IllegalArgumentException(message);
32063226
}
3207-
3208-
List<String> files = new ArrayList<>();
3209-
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
3210-
try {
3211-
files.add(new File(url.toURI()).getAbsolutePath());
3212-
} catch (IllegalArgumentException | URISyntaxException e) {
3213-
String message = String.format("Unable to convert url (%s) to file.", url);
3214-
LOG.error(message);
3215-
throw new IllegalArgumentException(message, e);
3216-
}
3217-
}
32183227
return files;
32193228
}
32203229

0 commit comments

Comments
 (0)