Skip to content

Commit

Permalink
[MENFORCER-420] Cache the results of getDependenciesToCheck across ru…
Browse files Browse the repository at this point in the history
…les.

Use an execution-scoped cache to store the results of the expensive call
to getDependenciesToCheck so that it's not recomputed for each rule.

Signed-off-by: Joseph Walton <jwalton@atlassian.com>
  • Loading branch information
josephw committed May 30, 2022
1 parent 70f8b22 commit 2772fc2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Expand Up @@ -21,6 +21,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -131,4 +132,13 @@ List<?> getComponentList ( String role )
* @return the container
*/
PlexusContainer getContainer();

/**
* Gets a cached value, or uses the provided producer to compute it.
*
* @param key a key to identify the value stored
* @param producer a supplier for the value if it's not already present
* @return a previously-cached or freshly-computed value
*/
Object getCache( String key, Supplier<?> producer );
}
Expand Up @@ -77,7 +77,7 @@ public void execute( EnforcerRuleHelper helper )

try
{
graphBuilder = (DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class );
graphBuilder = helper.getComponent( DependencyGraphBuilder.class );
}
catch ( ComponentLookupException e )
{
Expand All @@ -89,7 +89,7 @@ public void execute( EnforcerRuleHelper helper )
buildingRequest.setProject( project );

// get the correct list of dependencies
Set<Artifact> dependencies = getDependenciesToCheck( buildingRequest );
Set<Artifact> dependencies = getDependenciesToCheck( helper, buildingRequest );

// look for banned dependencies
Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() );
Expand Down Expand Up @@ -120,6 +120,18 @@ protected CharSequence getErrorMessage( Artifact artifact )
return "Found Banned Dependency: " + artifact.getId() + System.lineSeparator();
}

private Set<Artifact> getDependenciesToCheck( EnforcerRuleHelper helper,
ProjectBuildingRequest buildingRequest )
{
String cacheKey = buildingRequest.getProject().getId() + "_" + searchTransitive;

// check in the cache
Set<Artifact> dependencies =
(Set<Artifact>) helper.getCache( cacheKey, () -> getDependenciesToCheck( buildingRequest ) );

return dependencies;
}

protected Set<Artifact> getDependenciesToCheck( ProjectBuildingRequest buildingRequest )
{
Set<Artifact> dependencies = null;
Expand Down
Expand Up @@ -20,8 +20,10 @@
*/

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -50,6 +52,9 @@ public class DefaultEnforcementRuleHelper
/** The container. */
private PlexusContainer container;

/** A cache. */
private Map<String, Object> cache;

/**
* Instantiates a new default enforcement rule helper.
*
Expand All @@ -71,6 +76,8 @@ public DefaultEnforcementRuleHelper( MavenSession session, ExpressionEvaluator e
{
this.container = session.getContainer();
}

this.cache = new HashMap<>();
}

@Override
Expand Down Expand Up @@ -139,4 +146,10 @@ public PlexusContainer getContainer()
{
return container;
}

@Override
public Object getCache( String key, Supplier<?> producer )
{
return cache.computeIfAbsent( key, ( x ) -> producer.get() );
}
}

0 comments on commit 2772fc2

Please sign in to comment.