Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// under the License.
package org.openqa.selenium.manager;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static org.openqa.selenium.Platform.LINUX;
import static org.openqa.selenium.Platform.MAC;
import static org.openqa.selenium.Platform.UNIX;
Expand Down Expand Up @@ -213,11 +216,10 @@ private synchronized Path getBinary() {
}

binary = getBinaryInCache(SELENIUM_MANAGER + extension);
if (!binary.toFile().exists()) {
String binaryPathInJar = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
try (InputStream inputStream = this.getClass().getResourceAsStream(binaryPathInJar)) {
if (!Files.exists(binary)) {
try (InputStream inputStream = findBinaryInClasspath(folder, extension)) {
Files.createDirectories(binary.getParent());
Files.copy(inputStream, binary);
saveToFileSafely(inputStream, binary);
}
}
} catch (Exception e) {
Expand All @@ -233,6 +235,29 @@ private synchronized Path getBinary() {
return binary;
}

/**
* Protect from concurrency issue when executed by 2+ processes simultaneously. Every process sees
* the file created by another process only when the file is fully completed.
*/
private void saveToFileSafely(InputStream inputStream, Path target) throws IOException {
Path temporaryFile = target.resolveSibling(target.getFileName() + "." + randomUUID() + ".tmp");
Files.copy(inputStream, temporaryFile);
try {
if (!Files.exists(target)) {
Files.move(temporaryFile, target, REPLACE_EXISTING);
}
} finally {
Files.deleteIfExists(temporaryFile);
}
}

private InputStream findBinaryInClasspath(String folder, String extension) {
String binaryPathInJar = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
return requireNonNull(
getClass().getResourceAsStream(binaryPathInJar),
() -> "Resource not found in classpath: " + binaryPathInJar);
}

/**
* Executes Selenium Manager to get the locations of the requested assets
*
Expand Down
Loading