Skip to content

Commit

Permalink
Calculate BOM representation for each IU only once
Browse files Browse the repository at this point in the history
The BOM representation is currently calculated twice for each artifact.
Within a reactor build, such IUs should always produce the same BOM
representation and should therefore be cached.

Resolves #3911
  • Loading branch information
ptziegler committed Jun 2, 2024
1 parent a20b8bb commit bccc817
Showing 1 changed file with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
package org.eclipse.tycho.sbom;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;

Expand Down Expand Up @@ -54,6 +56,8 @@ public class TychoProjectDependenciesConverter extends DefaultProjectDependencie
@Inject
private P2DependencyTreeGenerator dependencyGenerator;

private final Map<IInstallableUnit, List<String>> bomRepresentations = new ConcurrentHashMap<>();

@Override
public void cleanupBomDependencies(Metadata metadata, Map<String, Component> components,
Map<String, Dependency> dependencies) {
Expand Down Expand Up @@ -108,38 +112,39 @@ private void convertToDependency(DependencyTreeNode node, Set<Dependency> depend
* empty list is returned.
*
* @param iu The installable unit for which to generate
* @return A {@code mutable} list of all bom representation. matching the given
* IU.
* @return An {@code immutable} list of all bom representations matching the
* given IU.
*/
private List<String> getBomRepresentation(IInstallableUnit iu) {
final MavenSession mavenSession = legacySupport.getSession();
final List<MavenProject> reactorProjects = mavenSession.getAllProjects();
// mutable!
List<String> bomRefs = new ArrayList<>();
// (I) IU describes local reactor project
for (MavenProject project : reactorProjects) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
Set<IInstallableUnit> initalUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.INITIAL);
Set<IInstallableUnit> seedUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED);
if (initalUnits.contains(iu) || seedUnits.contains(iu)) {
String bomRef = modelConverter.generatePackageUrl(project.getArtifact());
return bomRepresentations.computeIfAbsent(iu, ignore -> {
final MavenSession mavenSession = legacySupport.getSession();
final List<MavenProject> reactorProjects = mavenSession.getAllProjects();
final List<String> bomRefs = new ArrayList<>();
// (I) IU describes local reactor project
for (MavenProject project : reactorProjects) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
Set<IInstallableUnit> initalUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.INITIAL);
Set<IInstallableUnit> seedUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED);
if (initalUnits.contains(iu) || seedUnits.contains(iu)) {
String bomRef = modelConverter.generatePackageUrl(project.getArtifact());
if (bomRef == null) {
LOG.error("Unable to calculate BOM for: " + project);
return Collections.emptyList();
}
bomRefs.add(bomRef);
return Collections.unmodifiableList(bomRefs);
}
}
// (II) IU describes external artifact
for (IArtifactKey p2artifactKey : iu.getArtifacts()) {
String bomRef = modelConverter.generateP2PackageUrl(p2artifactKey, true, true, false);
if (bomRef == null) {
LOG.error("Unable to calculate BOM for: " + project);
return bomRefs;
LOG.error("Unable to calculate BOM for: " + p2artifactKey);
continue;
}
bomRefs.add(bomRef);
return bomRefs;
}
}
// (II) IU describes external artifact
for (IArtifactKey p2artifactKey : iu.getArtifacts()) {
String bomRef = modelConverter.generateP2PackageUrl(p2artifactKey, true, true, false);
if (bomRef == null) {
LOG.error("Unable to calculate BOM for: " + p2artifactKey);
continue;
}
bomRefs.add(bomRef);
}
return bomRefs; // mutable!
return Collections.unmodifiableList(bomRefs);
});
}
}

0 comments on commit bccc817

Please sign in to comment.