Refactor MavenSession#getPluginContext to be thread safe (#575)

This PR introduce no API change, merely refactors MavenSession
getPluginContext method to truly thread-safe.

Also added Javadoc to affected field and method.
This commit is contained in:
Tamas Cservenak 2021-10-09 12:11:36 +02:00 committed by GitHub
parent d75bea4154
commit 3017b85141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 19 deletions

View File

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.RepositoryCache; import org.apache.maven.artifact.repository.RepositoryCache;
@ -75,7 +76,13 @@ public class MavenSession
private boolean parallel; private boolean parallel;
private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey = /**
* Plugin context keyed by project ({@link MavenProject#getId()}) and by plugin lookup key
* ({@link PluginDescriptor#getPluginLookupKey()}). Plugin contexts itself are mappings of {@link String} keys to
* {@link Object} values.
*/
@SuppressWarnings( "checkstyle:linelength" )
private final ConcurrentMap<String, ConcurrentMap<String, ConcurrentMap<String, Object>>> pluginContextsByProjectAndPluginKey =
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
@ -192,31 +199,25 @@ public class MavenSession
// Backward compat // Backward compat
/**
* Returns the plugin context for given key ({@link PluginDescriptor#getPluginLookupKey()} and
* {@link MavenProject}, never returns {@code null} as if context not present, creates it.
*
* <strong>Implementation note:</strong> while this method return type is {@link Map}, the returned map instance
* implements {@link ConcurrentMap} as well.
*
*/
public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project ) public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
{ {
String projectKey = project.getId(); String projectKey = project.getId();
Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey ); ConcurrentMap<String, ConcurrentMap<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey
.computeIfAbsent( projectKey, k -> new ConcurrentHashMap<>() );
if ( pluginContextsByKey == null )
{
pluginContextsByKey = new ConcurrentHashMap<>();
pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
}
String pluginKey = plugin.getPluginLookupKey(); String pluginKey = plugin.getPluginLookupKey();
Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey ); return pluginContextsByKey.computeIfAbsent( pluginKey, k -> new ConcurrentHashMap<>() );
if ( pluginContext == null )
{
pluginContext = new ConcurrentHashMap<>();
pluginContextsByKey.put( pluginKey, pluginContext );
}
return pluginContext;
} }
public ProjectDependencyGraph getProjectDependencyGraph() public ProjectDependencyGraph getProjectDependencyGraph()