diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/KotlinTestsDependenciesUtil.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/KotlinTestsDependenciesUtil.kt index 2c195244fede..d87758f36dda 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/KotlinTestsDependenciesUtil.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/KotlinTestsDependenciesUtil.kt @@ -10,6 +10,7 @@ import java.nio.file.Files import java.nio.file.InvalidPathException import java.nio.file.Path import java.security.MessageDigest +import java.util.concurrent.ConcurrentHashMap import kotlin.io.path.* import kotlin.jvm.optionals.getOrNull @@ -179,7 +180,7 @@ object KotlinTestsDependenciesUtil { if (sha256 == null) { error("cannot find sha256 in '$block'") } - val checksum = calculateSha256SumForUrl(url) + val checksum = sha256SumForUrl(url) if (isUpToDateCheck) { if (checksum != sha256) { errors.add("sha256 mismatch for '$url' expected '$sha256' but got '$checksum'") @@ -201,6 +202,10 @@ object KotlinTestsDependenciesUtil { } } + private val sha256Cache = ConcurrentHashMap() + + internal fun sha256SumForUrl(url: String): String = sha256Cache.getOrPut(url) { calculateSha256SumForUrl(url) } + private fun calculateSha256SumForUrl(url: String): String { println("Calculating SHA256 checksum for '$url'") val digest = MessageDigest.getInstance("SHA-256") diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/JpsLibrary.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/JpsLibrary.kt index c313dd2db1e8..68bd6c809930 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/JpsLibrary.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/JpsLibrary.kt @@ -1,5 +1,7 @@ package org.jetbrains.tools.model.updater.impl +import org.jetbrains.tools.model.updater.KotlinTestsDependenciesUtil + data class JpsLibrary( val name: String, val type: LibraryType, @@ -25,10 +27,14 @@ data class JpsLibrary( xml("properties", *properties) { if (jpsResolverSettings.sha256ChecksumsEnabled) { xml("verification") { - computeSha256Checksums(classes, type.remoteRepository).forEach { - xml("artifact", "url" to it.url) { + for (clazz in classes) { + val jpsPath = clazz.path + val fileUrl = JpsUrl.File(jpsPath) + xml("artifact", "url" to fileUrl.url) { xml("sha256sum") { - raw(it.sha256Checksum) + val url = "${type.remoteRepository.url}/${jpsPath.relativePath}" + val sha256checksum = KotlinTestsDependenciesUtil.sha256SumForUrl(url) + raw(sha256checksum) } } } @@ -65,10 +71,6 @@ data class JpsLibrary( val excludes: List = emptyList(), val includeTransitive: Boolean = true, val remoteRepository: JpsRemoteRepository - ) : LibraryType() { - companion object { - data class ArtifactVerificationEntry(val url: String, val sha256Checksum: String) - } - } + ) : LibraryType() } } diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsChecksumUtil.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsChecksumUtil.kt deleted file mode 100644 index 6c30df97a338..000000000000 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsChecksumUtil.kt +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package org.jetbrains.tools.model.updater.impl - -import org.jetbrains.tools.model.updater.impl.JpsLibrary.LibraryType.Repository.Companion.ArtifactVerificationEntry -import java.net.URL -import java.security.MessageDigest - -fun computeSha256Checksums(classesRoots: List, remoteRepository: JpsRemoteRepository): List { - return classesRoots.map { - require(it.path is JpsPath.MavenRepository) - val sha256checksum = downloadFileAndComputeSha256("${remoteRepository.url}/${it.path.relativePath}") - val fileUrl = JpsUrl.File(it.path) - ArtifactVerificationEntry(fileUrl.url, sha256checksum) - } -} - -private fun downloadFileAndComputeSha256(url: String): String { - println("Computing SHA256 checksum for '$url'") - val digest = MessageDigest.getInstance("SHA-256") - - URL(url).openStream().use { inputStream -> - val buf = ByteArray(65536) - - while (true) { - val count = inputStream.read(buf) - if (count <= 0) break - digest.update(buf, 0, count) - } - } - - return byteArrayToHexString(digest.digest()) -} - -private fun byteArrayToHexString(bytes: ByteArray): String { - val builder = StringBuilder(bytes.size * 2) - for (b in bytes) { - builder.append(String.format("%02x", b)) - } - return builder.toString() -} \ No newline at end of file