[MNG-7785] Clean usage of SessionData (#1111)

This commit is contained in:
Guillaume Nodet 2023-05-16 11:17:16 +02:00 committed by GitHub
parent 42594643c3
commit f0c2c65526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 29 deletions

View File

@ -27,7 +27,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -275,22 +274,9 @@ public class MojoExecutor {
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
private OwnerReentrantLock getProjectLock(MavenSession session) { private OwnerReentrantLock getProjectLock(MavenSession session) {
SessionData data = session.getRepositorySession().getData(); SessionData data = session.getRepositorySession().getData();
ConcurrentMap<MavenProject, OwnerReentrantLock> locks = (ConcurrentMap) data.get(ProjectLock.class); Map<MavenProject, OwnerReentrantLock> locks =
// initialize the value if not already done (in case of a concurrent access) to the method (Map) data.computeIfAbsent(ProjectLock.class, ConcurrentHashMap::new);
if (locks == null) { return locks.computeIfAbsent(session.getCurrentProject(), p -> new OwnerReentrantLock());
// the call to data.set(k, null, v) is effectively a call to data.putIfAbsent(k, v)
data.set(ProjectLock.class, null, new ConcurrentHashMap<>());
locks = (ConcurrentMap) data.get(ProjectLock.class);
}
OwnerReentrantLock acquiredProjectLock = locks.get(session.getCurrentProject());
if (acquiredProjectLock == null) {
acquiredProjectLock = new OwnerReentrantLock();
OwnerReentrantLock prev = locks.putIfAbsent(session.getCurrentProject(), acquiredProjectLock);
if (prev != null) {
acquiredProjectLock = prev;
}
}
return acquiredProjectLock;
} }
} }

View File

@ -91,8 +91,7 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver {
PluginVersionResult result = resolveFromProject(request); PluginVersionResult result = resolveFromProject(request);
if (result == null) { if (result == null) {
ConcurrentMap<Key, PluginVersionResult> cache = ConcurrentMap<Key, PluginVersionResult> cache = getCache(request);
getCache(request.getRepositorySession().getData());
Key key = getKey(request); Key key = getKey(request);
result = cache.get(key); result = cache.get(key);
@ -347,16 +346,10 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private ConcurrentMap<Key, PluginVersionResult> getCache(SessionData data) { private ConcurrentMap<Key, PluginVersionResult> getCache(PluginVersionRequest request) {
ConcurrentMap<Key, PluginVersionResult> cache = (ConcurrentMap<Key, PluginVersionResult>) data.get(CACHE_KEY); SessionData data = request.getRepositorySession().getData();
while (cache == null) { return (ConcurrentMap<Key, PluginVersionResult>)
cache = new ConcurrentHashMap<>(256); data.computeIfAbsent(CACHE_KEY, () -> new ConcurrentHashMap<>(256));
if (data.set(CACHE_KEY, null, cache)) {
break;
}
cache = (ConcurrentMap<Key, PluginVersionResult>) data.get(CACHE_KEY);
}
return cache;
} }
private static Key getKey(PluginVersionRequest request) { private static Key getKey(PluginVersionRequest request) {