[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.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -275,22 +274,9 @@ public class MojoExecutor {
@SuppressWarnings({"unchecked", "rawtypes"})
private OwnerReentrantLock getProjectLock(MavenSession session) {
SessionData data = session.getRepositorySession().getData();
ConcurrentMap<MavenProject, OwnerReentrantLock> locks = (ConcurrentMap) data.get(ProjectLock.class);
// initialize the value if not already done (in case of a concurrent access) to the method
if (locks == null) {
// 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;
Map<MavenProject, OwnerReentrantLock> locks =
(Map) data.computeIfAbsent(ProjectLock.class, ConcurrentHashMap::new);
return locks.computeIfAbsent(session.getCurrentProject(), p -> new OwnerReentrantLock());
}
}

View File

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