Skip to content

Commit 1931f26

Browse files
authored
Make jar extraction multi-process safe (#2552)
1 parent de1712a commit 1931f26

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

utbot-core/src/main/kotlin/org/utbot/common/JarUtils.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@ import java.io.File
44
import java.net.URL
55
import java.nio.file.Files
66
import java.nio.file.StandardCopyOption
7+
import java.util.*
78

89
object JarUtils {
910
private const val UNKNOWN_MODIFICATION_TIME = 0L
1011

1112
fun extractJarFileFromResources(jarFileName: String, jarResourcePath: String, targetDirectoryName: String): File {
12-
val targetDirectory =
13-
Files.createDirectories(utBotTempDirectory.toFile().resolve(targetDirectoryName).toPath()).toFile()
14-
return targetDirectory.resolve(jarFileName).also { jarFile ->
15-
val resource = this::class.java.classLoader.getResource(jarResourcePath)
16-
?: error("Unable to find \"$jarResourcePath\" in resources, make sure it's on the classpath")
17-
updateJarIfRequired(jarFile, resource)
13+
val resource = this::class.java.classLoader.getResource(jarResourcePath)
14+
?: error("Unable to find \"$jarResourcePath\" in resources, make sure it's on the classpath")
15+
16+
val targetDirectory = utBotTempDirectory.toFile().resolve(targetDirectoryName).toPath()
17+
fun extractToSubDir(subDir: String) =
18+
Files.createDirectories(targetDirectory.resolve(subDir)).toFile().resolve(jarFileName).also { jarFile ->
19+
updateJarIfRequired(jarFile, resource)
20+
}
21+
22+
// We attempt to always extract jars to same locations, to avoid eating up drive space with
23+
// every UtBot launch, but we may fail to do so if multiple processes are running in parallel.
24+
repeat(10) { i ->
25+
runCatching {
26+
return extractToSubDir(i.toString())
27+
}
1828
}
29+
return extractToSubDir(UUID.randomUUID().toString())
1930
}
2031

2132
private fun updateJarIfRequired(jarFile: File, resource: URL) {

0 commit comments

Comments
 (0)