You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2021/10/09 09:02:28 UTC

[maven] branch maven-session-plugincontext created (now fc5d8a0)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a change to branch maven-session-plugincontext
in repository https://gitbox.apache.org/repos/asf/maven.git.


      at fc5d8a0  Refactor MavenSession#getPluginContext to be thread safe

This branch includes the following new commits:

     new fc5d8a0  Refactor MavenSession#getPluginContext to be thread safe

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[maven] 01/01: Refactor MavenSession#getPluginContext to be thread safe

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch maven-session-plugincontext
in repository https://gitbox.apache.org/repos/asf/maven.git

commit fc5d8a081f1b48d6e71cad496839814fc07058c9
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Sat Oct 9 11:00:51 2021 +0200

    Refactor MavenSession#getPluginContext to be thread safe
    
    This PR introduce no API change, merely refactors MavenSession
    internals re getPluginContext methud and makes it truly thread-safe.
    
    Also added Javadoc with explanations.
---
 .../org/apache/maven/execution/MavenSession.java   | 39 +++++++++++-----------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
index f8dbb03..c2f0c89 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.RepositoryCache;
@@ -75,7 +76,13 @@ public class MavenSession
 
     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<>();
 
 
@@ -192,31 +199,25 @@ public class MavenSession
 
     // 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 )
     {
         String projectKey = project.getId();
 
-        Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
-
-        if ( pluginContextsByKey == null )
-        {
-            pluginContextsByKey = new ConcurrentHashMap<>();
-
-            pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
-        }
+        ConcurrentMap<String, ConcurrentMap<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey
+                .computeIfAbsent( projectKey, k -> new ConcurrentHashMap<>() );
 
         String pluginKey = plugin.getPluginLookupKey();
 
-        Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
-
-        if ( pluginContext == null )
-        {
-            pluginContext = new ConcurrentHashMap<>();
-
-            pluginContextsByKey.put( pluginKey, pluginContext );
-        }
-
-        return pluginContext;
+        return pluginContextsByKey.computeIfAbsent( pluginKey, k -> new ConcurrentHashMap<>() );
     }
 
     public ProjectDependencyGraph getProjectDependencyGraph()