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

This commit is contained in:
Guillaume Nodet 2023-05-16 10:56:44 +02:00 committed by GitHub
parent 97a565e313
commit b76e78072a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 22 deletions

View File

@ -273,16 +273,8 @@ public class MojoExecutor {
@SuppressWarnings({"unchecked", "rawtypes"})
private OwnerReentrantLock getProjectLock(MavenSession session) {
SessionData data = session.getRepositorySession().getData();
// TODO: when resolver 1.7.3 is released, the code below should be changed to
// TODO: Map<MavenProject, Lock> locks = ( Map ) ((Map) data).computeIfAbsent(
// TODO: ProjectLock.class, l -> new ConcurrentHashMap<>() );
Map<MavenProject, OwnerReentrantLock> locks = (Map) 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 = (Map) data.get(ProjectLock.class);
}
Map<MavenProject, OwnerReentrantLock> locks =
(Map) data.computeIfAbsent(ProjectLock.class, ConcurrentHashMap::new);
return locks.computeIfAbsent(session.getCurrentProject(), p -> new OwnerReentrantLock());
}
}

View File

@ -100,8 +100,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);
@ -354,16 +353,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) {