Skip to content

Commit 80df2bf

Browse files
committed
[java] Include shutdown hook again to delete binary when stored in tmp folder
1 parent f867435 commit 80df2bf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: java/src/org/openqa/selenium/manager/SeleniumManager.java

+19
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,31 @@ public class SeleniumManager {
7171
private final String managerPath = System.getenv("SE_MANAGER_PATH");
7272
private Path binary = managerPath == null ? null : Paths.get(managerPath);
7373
private String seleniumManagerVersion;
74+
private boolean binaryInTemporalFolder = false;
7475

7576
/** Wrapper for the Selenium Manager binary. */
7677
private SeleniumManager() {
7778
BuildInfo info = new BuildInfo();
7879
String releaseLabel = info.getReleaseLabel();
7980
int lastDot = releaseLabel.lastIndexOf(".");
8081
seleniumManagerVersion = BETA_PREFIX + releaseLabel.substring(0, lastDot);
82+
if (managerPath == null) {
83+
Runtime.getRuntime()
84+
.addShutdownHook(
85+
new Thread(
86+
() -> {
87+
if (binaryInTemporalFolder && binary != null && Files.exists(binary)) {
88+
try {
89+
Files.delete(binary);
90+
} catch (IOException e) {
91+
LOG.warning(
92+
String.format(
93+
"%s deleting temporal file: %s",
94+
e.getClass().getSimpleName(), e.getMessage()));
95+
}
96+
}
97+
}));
98+
}
8199
}
82100

83101
public static SeleniumManager getInstance() {
@@ -298,6 +316,7 @@ private Path getBinaryInCache(String binaryName) throws IOException {
298316
Path cacheParent = Paths.get(cachePath);
299317
if (!Files.isWritable(cacheParent)) {
300318
cacheParent = Files.createTempDirectory(SELENIUM_MANAGER);
319+
binaryInTemporalFolder = true;
301320
}
302321

303322
return Paths.get(

0 commit comments

Comments
 (0)