Skip to content

Commit

Permalink
fix(analyze-command): properly close PackageManager instances
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Dec 4, 2023
1 parent 5af7043 commit ff1323e
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 3 deletions.
8 changes: 7 additions & 1 deletion analyzer/src/main/kotlin/PackageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.ossreviewtoolkit.analyzer

import java.io.Closeable
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Path
Expand Down Expand Up @@ -62,7 +63,7 @@ abstract class PackageManager(
val analysisRoot: File,
val analyzerConfig: AnalyzerConfiguration,
val repoConfig: RepositoryConfiguration
) {
) : Closeable {
companion object {
private val PACKAGE_MANAGER_DIRECTORIES = setOf(
// Ignore intermediate build system directories.
Expand Down Expand Up @@ -361,6 +362,11 @@ abstract class PackageManager(
}
}
}

/**
* [PackageManager] implementations should override this method if they need to close resources.
*/
override fun close() = Unit
}

/**
Expand Down
4 changes: 4 additions & 0 deletions plugins/commands/analyzer/src/main/kotlin/AnalyzerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,9 @@ class AnalyzerCommand : OrtCommand(
val issues = analyzerRun.result.getAllIssues().flatMap { it.value }
SeverityStatsPrinter(terminal, resolutionProvider).stats(issues)
.print().conclude(ortConfig.severeIssueThreshold, 2)

for (packageManager in info.managedFiles.keys) {
packageManager.close()
}
}
}
5 changes: 5 additions & 0 deletions plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,9 @@ class Gradle(
}
}
}

override fun close() {
// Silently close the [MvnSupport] instance.
maven.use {}
}
}
5 changes: 5 additions & 0 deletions plugins/package-managers/maven/src/main/kotlin/Maven.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ class Maven(

return listOf(ProjectAnalyzerResult(project, emptySet(), issues))
}

override fun close() {
// Silently close the [MvnSupport] instance.
mvn.use {}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.ossreviewtoolkit.plugins.packagemanagers.maven.utils

import java.io.Closeable
import java.io.File
import java.net.URI

Expand Down Expand Up @@ -108,7 +109,7 @@ fun Artifact.identifier() = "$groupId:$artifactId:$version"
private val File?.safePath: String
get() = this?.invariantSeparatorsPath ?: "<unknown file>"

class MavenSupport(private val workspaceReader: WorkspaceReader) {
class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
companion object {
private val PACKAGING_TYPES = setOf(
// Core packaging types, see https://maven.apache.org/pom.html#packaging.
Expand Down Expand Up @@ -814,6 +815,10 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) {
legacySupport.session = null
}
}

override fun close() {
remoteArtifactCache.close()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>default</groupId>
<artifactId>projects_2.12</artifactId>
<packaging>jar</packaging>
<description>projects</description>
<version>0.1.0-SNAPSHOT</version>
<name>projects</name>
<organization>
<name>default</name>
</organization>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.16</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[debug] Other repositories:
[debug] Default repositories:
[debug] Using inline dependencies specified in Scala.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[info] Wrote /home/tn/workspace/netomi/ort/plugins/package-managers/sbt/src/funTest/assets/projects/target/scala-2.12/projects_2.12-0.1.0-SNAPSHOT.pom
7 changes: 6 additions & 1 deletion utils/common/src/main/kotlin/DiskCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.ossreviewtoolkit.utils.common

import com.jakewharton.disklrucache.DiskLruCache

import java.io.Closeable
import java.io.File
import java.io.IOException

Expand All @@ -46,7 +47,7 @@ class DiskCache(
* Duration in seconds that cache entries are valid.
*/
private val maxCacheEntryAgeInSeconds: Long
) {
) : Closeable {
companion object {
const val INDEX_FULL_KEY = 0
const val INDEX_TIMESTAMP = 1
Expand Down Expand Up @@ -144,4 +145,8 @@ class DiskCache(
}

private fun currentTimeInSeconds() = System.currentTimeMillis() / 1000L

override fun close() {
diskLruCache.close()
}
}

0 comments on commit ff1323e

Please sign in to comment.