Skip to content

Commit dc7326f

Browse files
committed
feat(utils): Split installJdk in two functions
Move the download logic to a separate function, allowing direct download of a specific JDK and not depending solely on Foojay Disco results. Directory creation is simplified to use a hash from the url provided. Signed-off-by: Helio Chissini de Castro <[email protected]>
1 parent 7f4f6c5 commit dc7326f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

utils/ort/src/main/kotlin/JavaBootstrapper.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
package org.ossreviewtoolkit.utils.ort
2121

2222
import java.io.File
23+
import java.security.MessageDigest
2324

25+
import kotlin.text.toByteArray
2426
import kotlin.time.measureTime
2527
import kotlin.time.measureTimedValue
2628

@@ -131,7 +133,20 @@ object JavaBootstrapper {
131133
return Result.failure(it)
132134
}
133135

134-
val installDir = (ortToolsDirectory / "jdks" / pkg.distribution / pkg.distributionVersion)
136+
return downloadJdk(pkg.links.pkgDownloadRedirect)
137+
}
138+
139+
/**
140+
* Download the resolved JDK Url and return its directory on success, or an exception on failure.
141+
*/
142+
fun downloadJdk(sdkUrl: String): Result<File> {
143+
val safeHash =
144+
MessageDigest.getInstance("SHA-256")
145+
.digest(sdkUrl.toByteArray())
146+
.joinToString("") { "%02x".format(it) }
147+
.take(16)
148+
149+
val installDir = (ortToolsDirectory / "jdks" / safeHash)
135150
.apply {
136151
if (isDirectory) {
137152
logger.info { "Not downloading the JDK again as the directory '$this' already exists." }
@@ -141,11 +156,10 @@ object JavaBootstrapper {
141156
safeMkdirs()
142157
}
143158

144-
val url = pkg.links.pkgDownloadRedirect
145-
logger.info { "Downloading the JDK package from $url..." }
159+
logger.info { "Downloading the JDK package from $sdkUrl..." }
146160

147161
val (archive, downloadDuration) = measureTimedValue {
148-
okHttpClient.downloadFile(url, installDir).getOrElse {
162+
okHttpClient.downloadFile(sdkUrl, installDir).getOrElse {
149163
return Result.failure(it)
150164
}
151165
}

0 commit comments

Comments
 (0)