Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): close java-db client #5273

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions pkg/fanal/analyzer/language/java/jar/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -35,7 +34,6 @@ var requiredExtensions = []string{

// javaLibraryAnalyzer analyzes jar/war/ear/par files
type javaLibraryAnalyzer struct {
once sync.Once
client *javadb.DB
slow bool
}
Expand All @@ -49,18 +47,13 @@ func newJavaLibraryAnalyzer(options analyzer.AnalyzerOptions) (analyzer.PostAnal
func (a *javaLibraryAnalyzer) PostAnalyze(ctx context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
// TODO: think about the sonatype API and "--offline"
var err error
a.once.Do(func() {
knqyf263 marked this conversation as resolved.
Show resolved Hide resolved
log.Logger.Info("JAR files found")
a.client, err = javadb.NewClient()
if err != nil {
log.Logger.Errorf("Unable to initialize the Java DB: %s", err)
return
}
log.Logger.Info("Analyzing JAR files takes a while...")
})
log.Logger.Info("JAR files found")
a.client, err = javadb.NewClient()
if err != nil {
return nil, err
return nil, xerrors.Errorf("Unable to initialize the Java DB: %s", err)
}
defer func() { _ = a.client.Close() }()
log.Logger.Info("Analyzing JAR files takes a while...")

// Skip analyzing JAR files as the nil client means the Java DB was not downloaded successfully.
if a.client == nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/javadb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,10 @@ func (d *DB) SearchByArtifactID(artifactID string) (string, error) {

return groupID, nil
}

func (d *DB) Close() error {
if d == nil {
return nil
}
return d.driver.Close()
}