Skip to content

Commit

Permalink
[MNG-7785] Clean usage of SessionData (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 16, 2023
1 parent 4259464 commit f0c2c65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -275,22 +274,9 @@ public void close() {
@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());
}
}

Expand Down
Expand Up @@ -91,8 +91,7 @@ public PluginVersionResult resolve(PluginVersionRequest request) throws PluginVe
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);

Expand Down Expand Up @@ -347,16 +346,10 @@ private PluginVersionResult resolveFromProject(PluginVersionRequest request, Lis
}

@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) {
Expand Down

0 comments on commit f0c2c65

Please sign in to comment.