Skip to content

Commit bfc2647

Browse files
committed
add dep fallback
Signed-off-by: Emily McMullan <[email protected]>
1 parent fbaa6a4 commit bfc2647

File tree

1 file changed

+53
-117
lines changed
  • external-providers/java-external-provider/pkg/java_external_provider

1 file changed

+53
-117
lines changed

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

Lines changed: 53 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const javaProjectPom = `<?xml version="1.0" encoding="UTF-8"?>
5555
</project>
5656
`
5757

58+
const EMBEDDED_KONVEYOR_GROUP = "io.konveyor.embeddedep"
59+
5860
type javaArtifact struct {
5961
foundOnline bool
6062
packaging string
@@ -253,28 +255,28 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
253255
default:
254256
}
255257

256-
filePath := filepath.Join(destDir, f.Name)
258+
explodedFilePath := filepath.Join(destDir, f.Name)
257259

258260
// fernflower already deemed this unparsable, skip...
259261
if strings.Contains(f.Name, "unparsable") || strings.Contains(f.Name, "NonParsable") {
260-
log.V(8).Info("unable to parse file", "file", filePath)
262+
log.V(8).Info("unable to parse file", "file", explodedFilePath)
261263
continue
262264
}
263265

264266
if f.FileInfo().IsDir() {
265267
// make sure execute bits are set so that fernflower can decompile
266-
err := os.MkdirAll(filePath, f.Mode()|0111)
268+
err := os.MkdirAll(explodedFilePath, f.Mode()|0111)
267269
if err != nil {
268-
log.V(5).Error(err, "failed to create directory when exploding the archive", "filePath", filePath)
270+
log.V(5).Error(err, "failed to create directory when exploding the archive", "filePath", explodedFilePath)
269271
}
270272
continue
271273
}
272274

273-
if err = os.MkdirAll(filepath.Dir(filePath), f.Mode()|0111); err != nil {
275+
if err = os.MkdirAll(filepath.Dir(explodedFilePath), f.Mode()|0111); err != nil {
274276
return "", decompileJobs, dependencies, err
275277
}
276278

277-
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()|0111)
279+
dstFile, err := os.OpenFile(explodedFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()|0111)
278280
if err != nil {
279281
return "", decompileJobs, dependencies, err
280282
}
@@ -299,12 +301,12 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
299301
// full path in the java project for the decompd file
300302
destPath := filepath.Join(
301303
projectPath, "src", "main", "java",
302-
strings.Replace(filePath, destDir, "", -1))
304+
strings.Replace(explodedFilePath, destDir, "", -1))
303305
destPath = strings.ReplaceAll(destPath, filepath.Join("WEB-INF", "classes"), "")
304306
destPath = strings.ReplaceAll(destPath, filepath.Join("META-INF", "classes"), "")
305307
destPath = strings.TrimSuffix(destPath, ClassFile) + ".java"
306308
decompileJobs = append(decompileJobs, decompileJob{
307-
inputPath: filePath,
309+
inputPath: explodedFilePath,
308310
outputPath: destPath,
309311
artifact: javaArtifact{
310312
packaging: ClassFile,
@@ -316,10 +318,10 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
316318
!(strings.Contains(f.Name, "WEB-INF") || strings.Contains(f.Name, "META-INF")):
317319
destPath := filepath.Join(
318320
projectPath, "src", "main", "java",
319-
strings.Replace(filePath, destDir, "", -1))
321+
strings.Replace(explodedFilePath, destDir, "", -1))
320322
destPath = strings.TrimSuffix(destPath, ClassFile) + ".java"
321323
decompileJobs = append(decompileJobs, decompileJob{
322-
inputPath: filePath,
324+
inputPath: explodedFilePath,
323325
outputPath: destPath,
324326
artifact: javaArtifact{
325327
packaging: ClassFile,
@@ -338,40 +340,40 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
338340
case strings.HasSuffix(f.Name, JavaFile):
339341
destPath := filepath.Join(
340342
projectPath, "src", "main", "java",
341-
strings.Replace(filePath, destDir, "", -1))
343+
strings.Replace(explodedFilePath, destDir, "", -1))
342344
destPath = strings.ReplaceAll(destPath, filepath.Join("WEB-INF", "classes"), "")
343345
destPath = strings.ReplaceAll(destPath, filepath.Join("META-INF", "classes"), "")
344346
if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
345347
log.V(8).Error(err, "error creating directory for java file", "path", destPath)
346348
continue
347349
}
348-
if err := moveFile(filePath, destPath); err != nil {
350+
if err := moveFile(explodedFilePath, destPath); err != nil {
349351
log.V(8).Error(err, "error moving decompiled file to project path",
350-
"src", filePath, "dest", destPath)
352+
"src", explodedFilePath, "dest", destPath)
351353
continue
352354
}
353355
// decompile web archives
354356
case strings.HasSuffix(f.Name, WebArchive):
355357
// TODO(djzager): Should we add these deps to the pom?
356-
_, nestedJobs, deps, err := explode(ctx, log, filePath, projectPath, m2Repo, depLabels)
358+
_, nestedJobs, deps, err := explode(ctx, log, explodedFilePath, projectPath, m2Repo, depLabels)
357359
if err != nil {
358-
log.Error(err, "failed to decompile file", "file", filePath)
360+
log.Error(err, "failed to decompile file", "file", explodedFilePath)
359361
}
360362
decompileJobs = append(decompileJobs, nestedJobs...)
361363
dependencies = append(dependencies, deps...)
362364
// attempt to add nested jars as dependency before decompiling
363365
case strings.HasSuffix(f.Name, JavaArchive):
364-
dep, err := toDependency(ctx, log, depLabels, filePath)
366+
dep, err := toDependency(ctx, log, depLabels, explodedFilePath)
365367
if err != nil {
366-
log.V(3).Error(err, "failed to add dep", "file", filePath)
368+
log.V(3).Error(err, "failed to add dep", "file", explodedFilePath)
367369
// when we fail to identify a dep we will fallback to
368370
// decompiling it ourselves and adding as source
369371
if (dep != javaArtifact{}) {
370372
outputPath := filepath.Join(
371-
filepath.Dir(filePath), fmt.Sprintf("%s-decompiled",
373+
filepath.Dir(explodedFilePath), fmt.Sprintf("%s-decompiled",
372374
strings.TrimSuffix(f.Name, JavaArchive)), filepath.Base(f.Name))
373375
decompileJobs = append(decompileJobs, decompileJob{
374-
inputPath: filePath,
376+
inputPath: explodedFilePath,
375377
outputPath: outputPath,
376378
artifact: javaArtifact{
377379
packaging: JavaArchive,
@@ -382,36 +384,41 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
382384
}
383385
}
384386
if (dep != javaArtifact{}) {
385-
if dep.foundOnline {
386-
dependencies = append(dependencies, dep)
387-
// copy this into m2 repo to avoid downloading again
388-
groupPath := filepath.Join(strings.Split(dep.GroupId, ".")...)
389-
artifactPath := filepath.Join(strings.Split(dep.ArtifactId, ".")...)
390-
destPath := filepath.Join(m2Repo, groupPath, artifactPath,
391-
dep.Version, filepath.Base(filePath))
392-
if err := CopyFile(filePath, destPath); err != nil {
393-
log.V(8).Error(err, "failed copying jar to m2 local repo")
394-
} else {
395-
log.V(8).Info("copied jar file", "src", filePath, "dest", destPath)
396-
}
387+
dependencies = append(dependencies, dep)
388+
// copy this into m2 repo to avoid downloading again
389+
groupPath := filepath.Join(strings.Split(dep.GroupId, ".")...)
390+
artifactPath := filepath.Join(strings.Split(dep.ArtifactId, ".")...)
391+
destPath := filepath.Join(m2Repo, groupPath, artifactPath,
392+
dep.Version, filepath.Base(explodedFilePath))
393+
if err := CopyFile(explodedFilePath, destPath); err != nil {
394+
log.V(8).Error(err, "failed copying jar to m2 local repo")
397395
} else {
398-
// when it isn't found online, decompile it
399-
outputPath := filepath.Join(
400-
filepath.Dir(filePath), fmt.Sprintf("%s-decompiled",
401-
strings.TrimSuffix(f.Name, JavaArchive)), filepath.Base(f.Name))
402-
decompileJobs = append(decompileJobs, decompileJob{
403-
inputPath: filePath,
404-
outputPath: outputPath,
405-
artifact: javaArtifact{
406-
packaging: JavaArchive,
407-
GroupId: dep.GroupId,
408-
ArtifactId: dep.ArtifactId,
409-
},
410-
})
396+
log.V(8).Info("copied jar file", "src", explodedFilePath, "dest", destPath)
397+
}
398+
399+
} else {
400+
log.Info("failed to add dependency to list of depdencies - using file to create dummy values", "file", explodedFilePath)
401+
name, _ := strings.CutSuffix(filepath.Base(explodedFilePath), ".jar")
402+
newDep := javaArtifact{
403+
foundOnline: false,
404+
packaging: "",
405+
GroupId: EMBEDDED_KONVEYOR_GROUP,
406+
ArtifactId: name,
407+
Version: "0.0.0-SNAPSHOT",
408+
sha1: "",
409+
}
410+
dependencies = append(dependencies, newDep)
411+
gropupPath := filepath.Join(strings.Split(EMBEDDED_KONVEYOR_GROUP, ".")...)
412+
destPath := filepath.Join(m2Repo, gropupPath, name, "0.0.0-SNAPSHOT", fmt.Sprintf("%s-%s.jar", newDep.ArtifactId, newDep.Version))
413+
if err := CopyFile(explodedFilePath, destPath); err != nil {
414+
log.V(8).Error(err, "failed copying jar to m2 local repo")
415+
} else {
416+
log.V(8).Info("copied jar file", "src", explodedFilePath, "dest", destPath)
411417
}
412418
}
413419
// any other files, move to java project as-is
414420
default:
421+
log.Info("default case", "file", explodedFilePath)
415422
baseName := strings.ToValidUTF8(f.Name, "_")
416423
re := regexp.MustCompile(`[^\w\-\.\\/]+`)
417424
baseName = re.ReplaceAllString(baseName, "_")
@@ -421,9 +428,9 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
421428
log.V(8).Error(err, "error creating directory for java file", "path", destPath)
422429
continue
423430
}
424-
if err := moveFile(filePath, destPath); err != nil {
431+
if err := moveFile(explodedFilePath, destPath); err != nil {
425432
log.V(8).Error(err, "error moving decompiled file to project path",
426-
"src", filePath, "dest", destPath)
433+
"src", explodedFilePath, "dest", destPath)
427434
continue
428435
}
429436
}
@@ -510,11 +517,6 @@ func AppendToFile(src string, dst string) error {
510517

511518
// toDependency returns javaArtifact constructed for a jar
512519
func toDependency(_ context.Context, log logr.Logger, depToLabels map[string]*depLabelItem, jarFile string) (javaArtifact, error) {
513-
//dep, err := constructArtifactFromSHA(jarFile)
514-
//if err == nil {
515-
// return dep, nil
516-
//}
517-
// if we fail to lookup on maven, construct it from pom
518520
dep, err := constructArtifactFromPom(log, jarFile)
519521
if err != nil {
520522
log.V(10).Info("could not construct artifact object from pom for artifact, trying to infer from structure", "jarFile", jarFile, "error", err.Error())
@@ -664,72 +666,6 @@ func inferGroupName(jarPath string) (string, error) {
664666
}
665667
}
666668

667-
//func constructArtifactFromSHA(jarFile string) (javaArtifact, error) {
668-
// dep := javaArtifact{}
669-
// // we look up the jar in maven
670-
// file, err := os.Open(jarFile)
671-
// if err != nil {
672-
// return dep, err
673-
// }
674-
// defer file.Close()
675-
//
676-
// hash := sha1.New()
677-
// _, err = io.Copy(hash, file)
678-
// if err != nil {
679-
// return dep, err
680-
// }
681-
//
682-
// sha1sum := hex.EncodeToString(hash.Sum(nil))
683-
//
684-
// // Make an HTTPS request to search.maven.org
685-
// searchURL := fmt.Sprintf("https://search.maven.org/solrsearch/select?q=1:%s&rows=20&wt=json", sha1sum)
686-
// resp, err := http.Get(searchURL)
687-
// if err != nil {
688-
// return dep, err
689-
// }
690-
// defer resp.Body.Close()
691-
//
692-
// // Read and parse the JSON response
693-
// body, err := io.ReadAll(resp.Body)
694-
// if err != nil {
695-
// return dep, err
696-
// }
697-
//
698-
// var data map[string]interface{}
699-
// err = json.Unmarshal(body, &data)
700-
// if err != nil {
701-
// return dep, err
702-
// }
703-
//
704-
// // Check if a single result is found
705-
// response, ok := data["response"].(map[string]interface{})
706-
// if !ok {
707-
// return dep, err
708-
// }
709-
//
710-
// numFound, ok := response["numFound"].(float64)
711-
// if !ok {
712-
// return dep, err
713-
// }
714-
//
715-
// if numFound == 1 {
716-
// jarInfo := response["docs"].([]interface{})[0].(map[string]interface{})
717-
// dep.GroupId = jarInfo["g"].(string)
718-
// dep.ArtifactId = jarInfo["a"].(string)
719-
// dep.Version = jarInfo["v"].(string)
720-
// dep.sha1 = sha1sum
721-
// dep.foundOnline = true
722-
// return dep, nil
723-
// } else if numFound > 1 {
724-
// dep, err = constructArtifactFromPom(jarFile)
725-
// if err == nil {
726-
// dep.foundOnline = true
727-
// return dep, nil
728-
// }
729-
// }
730-
// return dep, fmt.Errorf("failed to construct artifact from maven lookup")
731-
//}
732-
733669
func toFilePathDependency(_ context.Context, filePath string) (javaArtifact, error) {
734670
dep := javaArtifact{}
735671
// Move up one level to the artifact. we are assuming that we get the full class file here.

0 commit comments

Comments
 (0)