Skip to content

Commit 25be1ca

Browse files
✨ Allow removing exploded Java bins (#765) (#766)
Fixes https://issues.redhat.com/browse/MTA-4484 --------- Signed-off-by: Emily McMullan <[email protected]> Signed-off-by: Cherry Picker <[email protected]> Signed-off-by: Emily McMullan <[email protected]> Signed-off-by: Cherry Picker <[email protected]> Co-authored-by: Emily McMullan <[email protected]>
1 parent db383c4 commit 25be1ca

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

external-providers/java-external-provider/pkg/java_external_provider/provider.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
MVN_SETTINGS_FILE_INIT_OPTION = "mavenSettingsFile"
4545
GLOBAL_SETTINGS_INIT_OPTION = "mavenCacheDir"
4646
MVN_INSECURE_SETTING = "mavenInsecure"
47+
CLEAN_EXPLODED_BIN_OPTION = "cleanExplodedBin"
4748
JVM_MAX_MEM_INIT_OPTION = "jvmMaxMem"
4849
FERN_FLOWER_INIT_OPTION = "fernFlowerPath"
4950
)
@@ -335,10 +336,13 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
335336
}
336337

337338
extension := strings.ToLower(path.Ext(config.Location))
339+
explodedBins := []string{}
338340
switch extension {
339341
case JavaArchive, WebArchive, EnterpriseArchive:
342+
cleanBin, ok := config.ProviderSpecificConfig[CLEAN_EXPLODED_BIN_OPTION].(bool)
343+
340344
depLocation, sourceLocation, err := decompileJava(ctx, log, fernflower,
341-
config.Location, getMavenLocalRepoPath(mavenSettingsFile))
345+
config.Location, getMavenLocalRepoPath(mavenSettingsFile), ok)
342346
if err != nil {
343347
cancelFunc()
344348
return nil, additionalBuiltinConfig, err
@@ -347,7 +351,13 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
347351
// for binaries, we fallback to looking at .jar files only for deps
348352
config.DependencyPath = depLocation
349353
isBinary = true
354+
355+
if ok && cleanBin {
356+
log.Info("removing exploded binaries after analysis")
357+
explodedBins = append(explodedBins, depLocation, sourceLocation)
358+
}
350359
}
360+
351361
additionalBuiltinConfig.Location = config.Location
352362
additionalBuiltinConfig.DependencyPath = config.DependencyPath
353363

@@ -482,6 +492,7 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
482492
globalSettings: globalSettingsFile,
483493
depsLocationCache: make(map[string]int),
484494
includedPaths: provider.GetIncludedPathsFromConfig(config, false),
495+
cleanExplodedBins: explodedBins,
485496
}
486497

487498
if mode == provider.FullAnalysisMode {

external-providers/java-external-provider/pkg/java_external_provider/service_client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type javaServiceClient struct {
3939
depsCache map[uri.URI][]*provider.Dep
4040
depsLocationCache map[string]int
4141
includedPaths []string
42+
cleanExplodedBins []string
4243
}
4344

4445
type depLabelItem struct {
@@ -230,6 +231,11 @@ func (p *javaServiceClient) Stop() {
230231
if err != nil {
231232
p.log.Info("stopping java provider", "error", err)
232233
}
234+
if len(p.cleanExplodedBins) > 0 {
235+
for _, explodedPath := range p.cleanExplodedBins {
236+
os.RemoveAll(explodedPath)
237+
}
238+
}
233239
}
234240

235241
func (p *javaServiceClient) initialization(ctx context.Context) {

external-providers/java-external-provider/pkg/java_external_provider/util.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"strings"
1919
"sync"
2020
"text/template"
21+
"time"
22+
23+
"math/rand"
2124

2225
"github.com/go-logr/logr"
2326
"github.com/konveyor/analyzer-lsp/tracing"
@@ -159,11 +162,16 @@ func decompile(ctx context.Context, log logr.Logger, filter decompileFilter, wor
159162
// decompileJava unpacks archive at archivePath, decompiles all .class files in it
160163
// creates new java project and puts the java files in the tree of the project
161164
// returns path to exploded archive, path to java project, and an error when encountered
162-
func decompileJava(ctx context.Context, log logr.Logger, fernflower, archivePath string, m2RepoPath string) (explodedPath, projectPath string, err error) {
165+
func decompileJava(ctx context.Context, log logr.Logger, fernflower, archivePath string, m2RepoPath string, cleanBin bool) (explodedPath, projectPath string, err error) {
163166
ctx, span := tracing.StartNewSpan(ctx, "decompile")
164167
defer span.End()
165168

166-
projectPath = filepath.Join(filepath.Dir(archivePath), "java-project")
169+
// only need random project name if there is not dir cleanup after
170+
if cleanBin {
171+
projectPath = filepath.Join(filepath.Dir(archivePath), fmt.Sprintf("java-project-%v", RandomName()))
172+
} else {
173+
projectPath = filepath.Join(filepath.Dir(archivePath), "java-project")
174+
}
167175

168176
decompFilter := alwaysDecompileFilter(true)
169177

@@ -626,4 +634,14 @@ func toFilePathDependency(_ context.Context, filePath string) (javaArtifact, err
626634
dep.Version = "0.0.0"
627635
return dep, nil
628636

629-
}
637+
}
638+
639+
func RandomName() string {
640+
rand.Seed(int64(time.Now().Nanosecond()))
641+
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
642+
b := make([]byte, 16)
643+
for i := range b {
644+
b[i] = charset[rand.Intn(len(charset))]
645+
}
646+
return string(b)
647+
}

0 commit comments

Comments
 (0)