Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7785] Clean usage of SessionData #1111

Merged
merged 1 commit into from May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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