Skip to content

Commit d7cec5f

Browse files
committed
refactor(license-fact-providers): Stop implementing LicenseFactProvider
The `interface LicenseFactProvider` is implemented by plugins and also used internally from various reporters. So, it should be suitable nicely for both, the plugins and the calling code of the interface. In an upcoming enhancement, it cannot work nicely for both anymore. Prepare for that by no more implementing `LicenseFactProvider` with `CompositeLicenseFactProvider`, to have clean separation of the APIs of the plugins and of their internal use. Note: `getNonBlankLicenseText()` has to be moved to `CompositeLicenseFactProvider`, because that class is now the internal API for code using the providers. That function is used by the Freemarker template reporter scripts. As there is no function implementation left in `LicenseFactProvider`, it is turned back into an interface again. Signed-off-by: Frank Viernau <frank.viernau@gmail.com>
1 parent dee3adb commit d7cec5f

8 files changed

Lines changed: 29 additions & 37 deletions

File tree

plugins/license-fact-providers/api/src/main/kotlin/CompositeLicenseFactProvider.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@
1919

2020
package org.ossreviewtoolkit.plugins.licensefactproviders.api
2121

22-
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
23-
2422
/**
25-
* A [LicenseFactProvider] that aggregates multiple [LicenseFactProvider]s.
23+
* A composition of multiple [LicenseFactProvider]s.
2624
*/
2725
class CompositeLicenseFactProvider(
2826
/**
2927
* A list of [LicenseFactProvider]s to aggregate. The order of the list determines the precedence of the
3028
* providers: the first provider in the list that has a fact for a given license ID will be used.
3129
*/
3230
private val providers: List<LicenseFactProvider>
33-
) : LicenseFactProvider() {
34-
override val descriptor = PluginDescriptor(
35-
id = "Composite",
36-
displayName = "Composite License Fact Provider",
37-
summary = "A license fact provider that aggregates multiple license fact providers."
38-
)
31+
) {
32+
/** Return the [LicenseText] for the given [licenseId], or `null` if no valid text is available. */
33+
fun getLicenseText(licenseId: String) = providers.firstNotNullOfOrNull { it.getLicenseText(licenseId) }
3934

40-
override fun getLicenseText(licenseId: String) = providers.firstNotNullOfOrNull { it.getLicenseText(licenseId) }
35+
/** Return a non-blank license text for the given [licenseId], or `null` if no valid text is available. */
36+
@Deprecated("Java-only API", level = DeprecationLevel.HIDDEN)
37+
@JvmName("getLicenseText")
38+
fun getNonBlankLicenseText(licenseId: String): String? = getLicenseText(licenseId)?.text
4139

42-
override fun hasLicenseText(licenseId: String) = providers.any { it.hasLicenseText(licenseId) }
40+
/** Return `true´ if this provider has a license text for the given [licenseId]. */
41+
fun hasLicenseText(licenseId: String) = providers.any { it.hasLicenseText(licenseId) }
4342
}

plugins/license-fact-providers/api/src/main/kotlin/LicenseFactProvider.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ package org.ossreviewtoolkit.plugins.licensefactproviders.api
2222
import org.ossreviewtoolkit.plugins.api.Plugin
2323

2424
/** A provider for license facts. */
25-
abstract class LicenseFactProvider : Plugin {
25+
interface LicenseFactProvider : Plugin {
2626
/** Return the [LicenseText] for the given [licenseId], or `null` if no valid text is available. */
27-
abstract fun getLicenseText(licenseId: String): LicenseText?
28-
29-
/** Return a non-blank license text for the given [licenseId], or `null` if no valid text is available. */
30-
@Deprecated("Java-only API", level = DeprecationLevel.HIDDEN)
31-
@JvmName("getLicenseText")
32-
fun getNonBlankLicenseText(licenseId: String): String? = getLicenseText(licenseId)?.text
27+
fun getLicenseText(licenseId: String): LicenseText?
3328

3429
/** Return `true´ if this provider has a license text for the given [licenseId]. */
35-
abstract fun hasLicenseText(licenseId: String): Boolean
30+
fun hasLicenseText(licenseId: String): Boolean
3631
}

plugins/reporters/cyclonedx/src/funTest/kotlin/CycloneDxReporterFunTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import org.ossreviewtoolkit.model.OrtResult
4141
import org.ossreviewtoolkit.model.Project
4242
import org.ossreviewtoolkit.model.VcsInfo
4343
import org.ossreviewtoolkit.model.VcsType
44-
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
44+
import org.ossreviewtoolkit.plugins.licensefactproviders.api.CompositeLicenseFactProvider
4545
import org.ossreviewtoolkit.plugins.reporters.cyclonedx.CycloneDxReporter.Companion.REPORT_BASE_FILENAME
4646
import org.ossreviewtoolkit.reporter.LICENSE_FACT_PROVIDER_EMPTY
4747
import org.ossreviewtoolkit.reporter.LICENSE_FACT_PROVIDER_SPDX
@@ -259,7 +259,7 @@ private fun TestConfiguration.generateSingleBomReport(
259259
format: Format,
260260
singleBomComponentName: String = "",
261261
singleBomComponentType: Component.Type = Component.Type.APPLICATION,
262-
licenseFactProvider: LicenseFactProvider = LICENSE_FACT_PROVIDER_EMPTY
262+
licenseFactProvider: CompositeLicenseFactProvider = LICENSE_FACT_PROVIDER_EMPTY
263263
): String {
264264
val reporter = CycloneDxReporterFactory.create(
265265
schemaVersion = SchemaVersion.entries.single { it.version.versionString == DEFAULT_SCHEMA_VERSION_NAME },
@@ -279,7 +279,7 @@ private fun TestConfiguration.generateSingleBomReport(
279279
private fun TestConfiguration.generateMultiBomReport(
280280
ortResult: OrtResult,
281281
format: Format,
282-
licenseFactProvider: LicenseFactProvider = LICENSE_FACT_PROVIDER_EMPTY
282+
licenseFactProvider: CompositeLicenseFactProvider = LICENSE_FACT_PROVIDER_EMPTY
283283
): List<String> {
284284
val reporter = CycloneDxReporterFactory.create(
285285
schemaVersion = SchemaVersion.entries.single { it.version.versionString == DEFAULT_SCHEMA_VERSION_NAME },

plugins/reporters/spdx/src/funTest/kotlin/SpdxDocumentReporterFunTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import io.kotest.matchers.should
2828
import io.kotest.matchers.shouldBe
2929

3030
import org.ossreviewtoolkit.model.OrtResult
31-
import org.ossreviewtoolkit.reporter.LICENSE_FACT_PROVIDER_SPDX
31+
import org.ossreviewtoolkit.reporter.LICENSE_FACT_PROVIDER_SCAN_CODE
3232
import org.ossreviewtoolkit.reporter.ReporterInput
3333
import org.ossreviewtoolkit.utils.common.normalizeLineBreaks
3434
import org.ossreviewtoolkit.utils.ort.ORT_VERSION
@@ -159,7 +159,7 @@ private fun TestConfiguration.generateReport(
159159

160160
return SpdxDocumentReporter(config = config)
161161
.generateReport(
162-
input = ReporterInput(ortResult, licenseFactProvider = LICENSE_FACT_PROVIDER_SPDX),
162+
input = ReporterInput(ortResult, licenseFactProvider = LICENSE_FACT_PROVIDER_SCAN_CODE),
163163
outputDir = tempdir()
164164
)
165165
.single()

plugins/reporters/spdx/src/main/kotlin/Extensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import org.ossreviewtoolkit.model.licenses.LicenseView
4444
import org.ossreviewtoolkit.model.licenses.ResolvedLicenseInfo
4545
import org.ossreviewtoolkit.model.utils.FindingCurationMatcher
4646
import org.ossreviewtoolkit.model.utils.prependedPath
47-
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
47+
import org.ossreviewtoolkit.plugins.licensefactproviders.api.CompositeLicenseFactProvider
4848
import org.ossreviewtoolkit.utils.common.replaceCredentialsInUri
4949
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
5050
import org.ossreviewtoolkit.utils.spdx.SpdxLicense
@@ -242,7 +242,7 @@ private fun OrtResult.getPackageVerificationCode(id: Identifier, type: SpdxPacka
242242
/**
243243
* Use [licenseFactProvider] to add the license texts for all packages to the [SpdxDocument].
244244
*/
245-
internal fun SpdxDocument.addExtractedLicenseInfo(licenseFactProvider: LicenseFactProvider): SpdxDocument {
245+
internal fun SpdxDocument.addExtractedLicenseInfo(licenseFactProvider: CompositeLicenseFactProvider): SpdxDocument {
246246
val allLicenses = buildSet {
247247
packages.forEach {
248248
add(it.licenseConcluded)

plugins/reporters/spdx/src/main/kotlin/SpdxDocumentModelMapper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import org.ossreviewtoolkit.model.PackageLinkage
3232
import org.ossreviewtoolkit.model.SourceCodeOrigin.ARTIFACT
3333
import org.ossreviewtoolkit.model.SourceCodeOrigin.VCS
3434
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
35-
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
35+
import org.ossreviewtoolkit.plugins.licensefactproviders.api.CompositeLicenseFactProvider
3636
import org.ossreviewtoolkit.utils.ort.ORT_NAME
3737
import org.ossreviewtoolkit.utils.ort.ORT_VERSION
3838
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
@@ -50,7 +50,7 @@ internal object SpdxDocumentModelMapper {
5050
fun map(
5151
ortResult: OrtResult,
5252
licenseInfoResolver: LicenseInfoResolver,
53-
licenseFactProvider: LicenseFactProvider,
53+
licenseFactProvider: CompositeLicenseFactProvider,
5454
config: SpdxDocumentReporterConfig
5555
): SpdxDocument {
5656
val nextFileIndex = AtomicInteger(1)

reporter/src/main/kotlin/ReporterInput.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import org.ossreviewtoolkit.model.licenses.DefaultLicenseInfoProvider
2828
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
2929
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
3030
import org.ossreviewtoolkit.plugins.licensefactproviders.api.CompositeLicenseFactProvider
31-
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
3231
import org.ossreviewtoolkit.reporter.StatisticsCalculator.getStatistics
3332

3433
/**
@@ -46,9 +45,9 @@ data class ReporterInput(
4645
val ortConfig: OrtConfiguration = OrtConfiguration(),
4746

4847
/**
49-
* A [LicenseFactProvider], can be used to integrate licenses facts like license texts into reports.
48+
* A [CompositeLicenseFactProvider], can be used to integrate licenses facts like license texts into reports.
5049
*/
51-
val licenseFactProvider: LicenseFactProvider = CompositeLicenseFactProvider(emptyList()),
50+
val licenseFactProvider: CompositeLicenseFactProvider = CompositeLicenseFactProvider(emptyList()),
5251

5352
/**
5453
* A [CopyrightGarbage] container, can be used to clean up copyrights used in reports.

reporter/src/testFixtures/kotlin/Utils.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
package org.ossreviewtoolkit.reporter
2121

2222
import org.ossreviewtoolkit.plugins.licensefactproviders.api.CompositeLicenseFactProvider
23-
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
2423
import org.ossreviewtoolkit.plugins.licensefactproviders.scancode.ScanCodeLicenseFactProviderFactory
2524
import org.ossreviewtoolkit.plugins.licensefactproviders.spdx.SpdxLicenseFactProviderFactory
2625

27-
val LICENSE_FACT_PROVIDER_EMPTY: LicenseFactProvider by lazy {
26+
val LICENSE_FACT_PROVIDER_EMPTY: CompositeLicenseFactProvider by lazy {
2827
CompositeLicenseFactProvider(emptyList())
2928
}
3029

31-
val LICENSE_FACT_PROVIDER_SCAN_CODE: LicenseFactProvider by lazy {
32-
ScanCodeLicenseFactProviderFactory.create()
30+
val LICENSE_FACT_PROVIDER_SCAN_CODE: CompositeLicenseFactProvider by lazy {
31+
CompositeLicenseFactProvider(listOf(ScanCodeLicenseFactProviderFactory.create()))
3332
}
3433

35-
val LICENSE_FACT_PROVIDER_SPDX: LicenseFactProvider by lazy {
36-
SpdxLicenseFactProviderFactory.create()
34+
val LICENSE_FACT_PROVIDER_SPDX: CompositeLicenseFactProvider by lazy {
35+
CompositeLicenseFactProvider(listOf(SpdxLicenseFactProviderFactory.create()))
3736
}

0 commit comments

Comments
 (0)