Skip to content

Commit 17e7327

Browse files
committed
refactor(spdx-utils): Avoid storing URLs as keys in maps
`URL`'s `equals()` and `hashCode()` methods can perform a DNS lookup to resolve the host name, which may cause significant delays. So avoid using `URL` instances as keys in maps by using `URI` instances instead. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 8ff942d commit 17e7327

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

utils/spdx/build.gradle.kts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import de.undercouch.gradle.tasks.download.Download
2222
import groovy.json.JsonSlurper
2323

2424
import java.net.URI
25-
import java.net.URL
2625

2726
val spdxLicenseListVersion: String by project
2827

@@ -90,7 +89,7 @@ data class LicenseInfo(
9089
)
9190

9291
fun interface SpdxLicenseTextProvider {
93-
fun getLicenseUrl(info: LicenseInfo): URL?
92+
fun getLicenseUrl(info: LicenseInfo): URI?
9493
}
9594

9695
class ScanCodeLicenseTextProvider : SpdxLicenseTextProvider {
@@ -112,18 +111,18 @@ class ScanCodeLicenseTextProvider : SpdxLicenseTextProvider {
112111
}.toMap()
113112
}
114113

115-
override fun getLicenseUrl(info: LicenseInfo): URL? {
114+
override fun getLicenseUrl(info: LicenseInfo): URI? {
116115
val key = spdxIdToScanCodeKeyMap[info.id] ?: return null
117-
return URI("$url/$key.LICENSE").toURL()
116+
return URI("$url/$key.LICENSE")
118117
}
119118
}
120119

121120
class SpdxLicenseListDataProvider : SpdxLicenseTextProvider {
122121
private val url = "https://raw.githubusercontent.com/spdx/license-list-data/v$spdxLicenseListVersion"
123122

124-
override fun getLicenseUrl(info: LicenseInfo): URL? {
123+
override fun getLicenseUrl(info: LicenseInfo): URI? {
125124
val prefix = "deprecated_".takeIf { info.isDeprecated && !info.isException }.orEmpty()
126-
return URI("$url/text/$prefix${info.id}.txt").toURL()
125+
return URI("$url/text/$prefix${info.id}.txt")
127126
}
128127
}
129128

@@ -359,7 +358,7 @@ val generateSpdxLicenseEnum by tasks.registering(Download::class) {
359358

360359
src(licenseUrlMap.keys.sortedBy { it.toString().lowercase() })
361360
dest("src/main/resources/$licensesResourcePath")
362-
eachFile { name = licenseUrlMap[sourceURL] }
361+
eachFile { name = licenseUrlMap[sourceURL.toURI()] }
363362

364363
doLast {
365364
generateEnumClass(
@@ -392,7 +391,7 @@ val generateSpdxLicenseExceptionEnum by tasks.registering(Download::class) {
392391

393392
src(licenseExceptionUrlMap.keys.sortedBy { it.toString().lowercase() })
394393
dest("src/main/resources/$exceptionsResourcePath")
395-
eachFile { name = licenseExceptionUrlMap[sourceURL] }
394+
eachFile { name = licenseExceptionUrlMap[sourceURL.toURI()] }
396395

397396
doLast {
398397
generateEnumClass(

0 commit comments

Comments
 (0)