Fix embedded JDK LICENSE handling for JDK 9 PiperOrigin-RevId: 196583301 Change-Id: Id36d75b447971e8ab9118ab46c1fa54caaee948d
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/LicenseCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/LicenseCommand.java index 7ad512a..a7ca2c6 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/commands/LicenseCommand.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/LicenseCommand.java
@@ -13,7 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.runtime.commands; -import com.google.common.io.Files; +import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.analysis.NoBuildEvent; import com.google.devtools.build.lib.runtime.BlazeCommand; import com.google.devtools.build.lib.runtime.BlazeCommandResult; @@ -22,11 +22,15 @@ import com.google.devtools.build.lib.util.ExitCode; import com.google.devtools.build.lib.util.ResourceFileLoader; import com.google.devtools.build.lib.util.io.OutErr; -import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.common.options.OptionsParser; import com.google.devtools.common.options.OptionsProvider; import java.io.IOException; -import java.util.TreeSet; +import java.io.UncheckedIOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; /** A command that prints an embedded license text. */ @Command( @@ -38,15 +42,8 @@ ) public class LicenseCommand implements BlazeCommand { - private static final TreeSet<String> JAVA_LICENSE_FILES = - new TreeSet<>(String.CASE_INSENSITIVE_ORDER); - - static { - JAVA_LICENSE_FILES.add("ASSEMBLY_EXCEPTION"); - JAVA_LICENSE_FILES.add("DISCLAIMER"); - JAVA_LICENSE_FILES.add("LICENSE"); - JAVA_LICENSE_FILES.add("THIRD_PARTY_README"); - } + private static final ImmutableSet<String> JAVA_LICENSE_FILES = + ImmutableSet.of("ASSEMBLY_EXCEPTION", "DISCLAIMER", "LICENSE", "THIRD_PARTY_README"); public static boolean isSupported() { return ResourceFileLoader.resourceExists(LicenseCommand.class, "LICENSE"); @@ -67,16 +64,24 @@ } Path bundledJdk = - env.getDirectories().getEmbeddedBinariesRoot().getRelative("embedded_tools/jdk"); - if (bundledJdk.exists()) { + env.getDirectories() + .getEmbeddedBinariesRoot() + .getRelative("embedded_tools/jdk") + .getPathFile() + .toPath(); + if (Files.exists(bundledJdk)) { outErr.printOutLn( "This binary comes with a bundled JDK, which contains the following license files:\n"); printJavaLicenseFiles(outErr, bundledJdk); } Path bundledJre = - env.getDirectories().getEmbeddedBinariesRoot().getRelative("embedded_tools/jre"); - if (bundledJre.exists()) { + env.getDirectories() + .getEmbeddedBinariesRoot() + .getRelative("embedded_tools/jre") + .getPathFile() + .toPath(); + if (Files.exists(bundledJre)) { outErr.printOutLn( "This binary comes with a bundled JRE, which contains the following license files:\n"); printJavaLicenseFiles(outErr, bundledJre); @@ -87,15 +92,22 @@ private static void printJavaLicenseFiles(OutErr outErr, Path bundledJdkOrJre) { try { - for (Path path : bundledJdkOrJre.getDirectoryEntries()) { - if (JAVA_LICENSE_FILES.contains(path.getBaseName().toLowerCase())) { - outErr.printOutLn(path.getPathString() + ":\n"); - Files.copy(path.getPathFile(), outErr.getOutputStream()); - outErr.printOutLn("\n"); - } - } + Files.walkFileTree( + bundledJdkOrJre, + new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) + throws IOException { + if (JAVA_LICENSE_FILES.contains(path.getFileName().toString())) { + outErr.printOutLn(path + ":\n"); + Files.copy(path, outErr.getOutputStream()); + outErr.printOutLn("\n"); + } + return super.visitFile(path, basicFileAttributes); + } + }); } catch (IOException e) { - throw new IllegalStateException( + throw new UncheckedIOException( "I/O error while trying to print license file of bundled JDK or JRE: " + e.getMessage(), e); }