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
23 changes: 17 additions & 6 deletions utbot-core/src/main/kotlin/org/utbot/common/JarUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ import java.io.File
import java.net.URL
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.*

object JarUtils {
private const val UNKNOWN_MODIFICATION_TIME = 0L

fun extractJarFileFromResources(jarFileName: String, jarResourcePath: String, targetDirectoryName: String): File {
val targetDirectory =
Files.createDirectories(utBotTempDirectory.toFile().resolve(targetDirectoryName).toPath()).toFile()
return targetDirectory.resolve(jarFileName).also { jarFile ->
val resource = this::class.java.classLoader.getResource(jarResourcePath)
?: error("Unable to find \"$jarResourcePath\" in resources, make sure it's on the classpath")
updateJarIfRequired(jarFile, resource)
val resource = this::class.java.classLoader.getResource(jarResourcePath)
?: error("Unable to find \"$jarResourcePath\" in resources, make sure it's on the classpath")

val targetDirectory = utBotTempDirectory.toFile().resolve(targetDirectoryName).toPath()
fun extractToSubDir(subDir: String) =
Files.createDirectories(targetDirectory.resolve(subDir)).toFile().resolve(jarFileName).also { jarFile ->
updateJarIfRequired(jarFile, resource)
}

// We attempt to always extract jars to same locations, to avoid eating up drive space with
// every UtBot launch, but we may fail to do so if multiple processes are running in parallel.
repeat(10) { i ->
runCatching {
return extractToSubDir(i.toString())
}
}
return extractToSubDir(UUID.randomUUID().toString())
}

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