Skip to content

Commit

Permalink
Clean usage of SessionData
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 17, 2023
1 parent 432bd7a commit 68b2330
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,8 @@ public void close() {
@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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,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 @@ -354,16 +353,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 68b2330

Please sign in to comment.