diff --git a/java/client/src/org/openqa/selenium/io/BUILD.bazel b/java/client/src/org/openqa/selenium/io/BUILD.bazel index 7a70f5d64dee8..f6c6d608ad3fe 100644 --- a/java/client/src/org/openqa/selenium/io/BUILD.bazel +++ b/java/client/src/org/openqa/selenium/io/BUILD.bazel @@ -4,6 +4,9 @@ java_library( visibility = [ "//java/client/src/org/openqa/selenium/os:__pkg__", "//java/client/src/org/openqa/selenium/remote:__pkg__", + "//java/client/src/org/openqa/selenium/tools/jar:__pkg__", + "//java/client/src/org/openqa/selenium/tools/javadoc:__pkg__", + "//java/client/src/org/openqa/selenium/tools/modules:__pkg__", "//java/client/test/org/openqa/selenium/io:__pkg__", ], deps = [ diff --git a/java/client/src/org/openqa/selenium/tools/jar/BUILD.bazel b/java/client/src/org/openqa/selenium/tools/jar/BUILD.bazel index d2bfb59e8697d..08ee62106b6bb 100644 --- a/java/client/src/org/openqa/selenium/tools/jar/BUILD.bazel +++ b/java/client/src/org/openqa/selenium/tools/jar/BUILD.bazel @@ -11,5 +11,6 @@ java_binary( "//visibility:public", ], deps = [ + "//java/client/src/org/openqa/selenium/io", ], ) diff --git a/java/client/src/org/openqa/selenium/tools/jar/MergeJars.java b/java/client/src/org/openqa/selenium/tools/jar/MergeJars.java index 8a47363665af0..d687ae31ccc7b 100644 --- a/java/client/src/org/openqa/selenium/tools/jar/MergeJars.java +++ b/java/client/src/org/openqa/selenium/tools/jar/MergeJars.java @@ -18,6 +18,7 @@ package org.openqa.selenium.tools.jar; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -46,6 +47,8 @@ import static java.util.zip.Deflater.BEST_COMPRESSION; import static java.util.zip.ZipOutputStream.DEFLATED; +import org.openqa.selenium.io.TemporaryFilesystem; + public class MergeJars { // File time is taken from the epoch (1970-01-01T00:00:00Z), but zip files @@ -91,7 +94,8 @@ public static void main(String[] args) throws IOException { // To keep things simple, we expand all the inputs jars into a single directory, // merge the manifests, and then create our own zip. - Path temp = Files.createTempDirectory("mergejars"); + File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("mergejars", ""); + Path temp = tempDir.toPath(); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); @@ -215,6 +219,8 @@ public static void main(String[] args) throws IOException { } }); } + + TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(tempDir); } private static JarEntry resetTime(JarEntry entry) { diff --git a/java/client/src/org/openqa/selenium/tools/javadoc/BUILD.bazel b/java/client/src/org/openqa/selenium/tools/javadoc/BUILD.bazel index 2c10ce280362e..ccc10eae5703d 100644 --- a/java/client/src/org/openqa/selenium/tools/javadoc/BUILD.bazel +++ b/java/client/src/org/openqa/selenium/tools/javadoc/BUILD.bazel @@ -7,6 +7,7 @@ java_binary( "-target", "11", ], deps = [ + "//java/client/src/org/openqa/selenium/io", "//java/client/src/org/openqa/selenium/tools/zip", ], visibility = [ diff --git a/java/client/src/org/openqa/selenium/tools/javadoc/JavadocJarMaker.java b/java/client/src/org/openqa/selenium/tools/javadoc/JavadocJarMaker.java index 037a03ea3ddfc..c8be7fabe268b 100644 --- a/java/client/src/org/openqa/selenium/tools/javadoc/JavadocJarMaker.java +++ b/java/client/src/org/openqa/selenium/tools/javadoc/JavadocJarMaker.java @@ -17,6 +17,7 @@ package org.openqa.selenium.tools.javadoc; +import org.openqa.selenium.io.TemporaryFilesystem; import org.openqa.selenium.tools.zip.StableZipEntry; import javax.tools.DocumentationTool; @@ -82,98 +83,90 @@ public static void main(String[] args) throws IOException { throw new IllegalArgumentException("The output jar location must be specified via the --out flag"); } - Set tempDirs = new HashSet<>(); - Path dir = Files.createTempDirectory("javadocs"); + TemporaryFilesystem tmpFS = TemporaryFilesystem.getDefaultTmpFS(); + Set tempDirs = new HashSet<>(); + File dir = tmpFS.createTempDir("javadocs", ""); tempDirs.add(dir); - try { - DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); - try (StandardJavaFileManager fileManager = tool.getStandardFileManager(null, Locale.getDefault(), UTF_8)) { - fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, List.of(dir.toFile())); - fileManager.setLocation(StandardLocation.CLASS_PATH, classpath.stream().map(Path::toFile).collect(Collectors.toSet())); - - Set sources = new HashSet<>(); - Set topLevelPackages = new HashSet<>(); - - Path unpackTo = Files.createTempDirectory("unpacked-sources"); - tempDirs.add(unpackTo); - Set fileNames = new HashSet<>(); - readSourceFiles(unpackTo, fileManager, sourceJars, sources, topLevelPackages, fileNames); - - // True if we're just exporting a set of modules - if (sources.isEmpty()) { - try (OutputStream os = Files.newOutputStream(out); - ZipOutputStream zos = new ZipOutputStream(os)) { - // It's enough to just create the thing - } - return; - } + DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); + try (StandardJavaFileManager fileManager = tool.getStandardFileManager(null, Locale.getDefault(), UTF_8)) { + fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, List.of(dir)); + fileManager.setLocation(StandardLocation.CLASS_PATH, classpath.stream().map(Path::toFile).collect(Collectors.toSet())); + + Set sources = new HashSet<>(); + Set topLevelPackages = new HashSet<>(); + + File unpackTo = tmpFS.createTempDir("unpacked-sources", ""); + tempDirs.add(unpackTo); + Set fileNames = new HashSet<>(); + readSourceFiles(unpackTo.toPath(), fileManager, sourceJars, sources, topLevelPackages, fileNames); - List options = new ArrayList<>(); - if (!classpath.isEmpty()) { - options.add("-cp"); - options.add(classpath.stream().map(String::valueOf).collect(Collectors.joining(File.pathSeparator))); + // True if we're just exporting a set of modules + if (sources.isEmpty()) { + try (OutputStream os = Files.newOutputStream(out); + ZipOutputStream zos = new ZipOutputStream(os)) { + // It's enough to just create the thing } - options.addAll(List.of("-html5", "--frames", "-notimestamp", "-use", "-quiet", "-Xdoclint:-missing", "-encoding", "UTF8")); + return; + } + List options = new ArrayList<>(); + if (!classpath.isEmpty()) { + options.add("-cp"); + options.add(classpath.stream().map(String::valueOf).collect(Collectors.joining(File.pathSeparator))); + } + options.addAll(List.of("-html5", "--frames", "-notimestamp", "-use", "-quiet", "-Xdoclint:-missing", "-encoding", "UTF8")); - Path outputTo = Files.createTempDirectory("output-dir"); - tempDirs.add(outputTo); - options.addAll(List.of("-d", outputTo.toAbsolutePath().toString())); + File outputTo = tmpFS.createTempDir("output-dir", ""); + tempDirs.add(outputTo); + Path outputToPath = outputTo.toPath(); - sources.forEach(obj -> options.add(obj.getName())); + options.addAll(List.of("-d", outputTo.getAbsolutePath())); - Writer writer = new StringWriter(); - DocumentationTool.DocumentationTask task = tool.getTask(writer, fileManager, null, null, options, sources); - Boolean result = task.call(); - if (result == null || !result) { - System.err.println("javadoc " + String.join(" ", options)); - System.err.println(writer); - return; - } + sources.forEach(obj -> options.add(obj.getName())); - try (OutputStream os = Files.newOutputStream(out); - ZipOutputStream zos = new ZipOutputStream(os); - Stream walk = Files.walk(outputTo)) { - walk.sorted(Comparator.naturalOrder()) - .forEachOrdered(path -> { - if (path.equals(outputTo)) { - return; - } + Writer writer = new StringWriter(); + DocumentationTool.DocumentationTask task = tool.getTask(writer, fileManager, null, null, options, sources); + Boolean result = task.call(); + if (result == null || !result) { + System.err.println("javadoc " + String.join(" ", options)); + System.err.println(writer); + return; + } - try { - if (Files.isDirectory(path)) { - String name = outputTo.relativize(path) + "/"; - ZipEntry entry = new StableZipEntry(name); - zos.putNextEntry(entry); - zos.closeEntry(); - } else { - String name = outputTo.relativize(path).toString(); - ZipEntry entry = new StableZipEntry(name); - zos.putNextEntry(entry); - try (InputStream is = Files.newInputStream(path)) { - is.transferTo(zos); - } - zos.closeEntry(); + try (OutputStream os = Files.newOutputStream(out); + ZipOutputStream zos = new ZipOutputStream(os); + Stream walk = Files.walk(outputToPath)) { + walk.sorted(Comparator.naturalOrder()) + .forEachOrdered(path -> { + if (path.equals(outputToPath)) { + return; + } + + try { + if (Files.isDirectory(path)) { + String name = outputToPath.relativize(path) + "/"; + ZipEntry entry = new StableZipEntry(name); + zos.putNextEntry(entry); + zos.closeEntry(); + } else { + String name = outputToPath.relativize(path).toString(); + ZipEntry entry = new StableZipEntry(name); + zos.putNextEntry(entry); + try (InputStream is = Files.newInputStream(path)) { + is.transferTo(zos); } - } catch (IOException e) { - throw new UncheckedIOException(e); + zos.closeEntry(); } - }); - } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); } - } finally { - tempDirs.forEach(d -> { - try (Stream walk = Files.walk(d)) { - walk.sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - } catch (IOException e) { - e.printStackTrace(); - } - }); } + + tempDirs.forEach(tmpFS::deleteTempDir); } private static void readSourceFiles( diff --git a/java/client/src/org/openqa/selenium/tools/modules/BUILD.bazel b/java/client/src/org/openqa/selenium/tools/modules/BUILD.bazel index ef4ea1aff8347..784b50e748331 100644 --- a/java/client/src/org/openqa/selenium/tools/modules/BUILD.bazel +++ b/java/client/src/org/openqa/selenium/tools/modules/BUILD.bazel @@ -12,6 +12,7 @@ java_binary( main_class = "org.openqa.selenium.tools.modules.ModuleGenerator", visibility = ["//visibility:public"], deps = [ + "//java/client/src/org/openqa/selenium/io", "//java/client/src/org/openqa/selenium/tools/zip", artifact("net.bytebuddy:byte-buddy"), artifact("com.github.javaparser:javaparser-core"), diff --git a/java/client/src/org/openqa/selenium/tools/modules/ModuleGenerator.java b/java/client/src/org/openqa/selenium/tools/modules/ModuleGenerator.java index be0e7229cf0ec..442d802472c10 100644 --- a/java/client/src/org/openqa/selenium/tools/modules/ModuleGenerator.java +++ b/java/client/src/org/openqa/selenium/tools/modules/ModuleGenerator.java @@ -35,6 +35,8 @@ import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import net.bytebuddy.jar.asm.ClassWriter; import net.bytebuddy.jar.asm.ModuleVisitor; + +import org.openqa.selenium.io.TemporaryFilesystem; import org.openqa.selenium.tools.zip.StableZipEntry; import java.io.ByteArrayOutputStream; @@ -149,7 +151,8 @@ public static void main(String[] args) throws IOException { Objects.requireNonNull(inJar, "Input jar must be set."); ToolProvider jdeps = ToolProvider.findFirst("jdeps").orElseThrow(); - Path tempDir = Files.createTempDirectory("module-dir"); + File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("module-dir", ""); + Path temp = tempDir.toPath(); // It doesn't matter what we use for writing to the stream: jdeps doesn't use it. *facepalm* List jdepsArgs = new LinkedList<>( @@ -159,7 +162,7 @@ public static void main(String[] args) throws IOException { if (!modulePath.isEmpty()) { jdepsArgs.addAll(List.of("--module-path", modulePath.stream().map(Object::toString).collect(Collectors.joining(File.pathSeparator)))); } - jdepsArgs.addAll(List.of("--generate-module-info", tempDir.toAbsolutePath().toString())); + jdepsArgs.addAll(List.of("--generate-module-info", temp.toAbsolutePath().toString())); jdepsArgs.add(inJar.toAbsolutePath().toString()); PrintStream origOut = System.out; @@ -186,7 +189,7 @@ public static void main(String[] args) throws IOException { AtomicReference moduleInfo = new AtomicReference<>(); // Fortunately, we know the directory where the output is written - Files.walkFileTree(tempDir, new SimpleFileVisitor<>() { + Files.walkFileTree(temp, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if ("module-info.java".equals(file.getFileName().toString())) { @@ -306,6 +309,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { jos.write(bytes); jos.closeEntry(); } + + TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(tempDir); } private static Set inferPackages(Path inJar) {