Skip to content

Commit 715d384

Browse files
committed
fix nested testPermission calls from PredicatePermissions
1 parent 0a5a3f1 commit 715d384

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

cloud-core/src/main/java/org/incendo/cloud/CommandManager.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
package org.incendo.cloud;
2525

2626
import io.leangen.geantyref.TypeToken;
27+
import java.util.ArrayDeque;
2728
import java.util.Collection;
2829
import java.util.Collections;
30+
import java.util.Deque;
31+
import java.util.HashMap;
2932
import java.util.HashSet;
3033
import java.util.LinkedList;
3134
import java.util.List;
@@ -381,8 +384,8 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
381384
return Collections.unmodifiableSet(new HashSet<>(this.capabilities));
382385
}
383386

384-
private final ThreadLocal<Map<Pair<C, Permission>, PermissionResult>> threadLocalPermissionCache =
385-
ThreadLocal.withInitial(ConcurrentHashMap::new);
387+
private final ThreadLocal<Deque<Map<Pair<C, Permission>, PermissionResult>>> threadLocalPermissionCache =
388+
ThreadLocal.withInitial(ArrayDeque::new);
386389

387390
@SuppressWarnings("rawtypes")
388391
private @NonNull <T> PermissionResult testPermissionCaching(
@@ -393,7 +396,8 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
393396
if (!this.settings.get(ManagerSetting.REDUCE_REDUNDANT_PERMISSION_CHECKS)) {
394397
return tester.apply(Pair.of(sender, permission));
395398
}
396-
return this.threadLocalPermissionCache.get().computeIfAbsent((Pair) Pair.of(sender, permission), (Function) tester);
399+
return Objects.requireNonNull(this.threadLocalPermissionCache.get().peek())
400+
.computeIfAbsent((Pair) Pair.of(sender, permission), (Function) tester);
397401
}
398402

399403
/**
@@ -409,9 +413,10 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
409413
final @NonNull Permission permission
410414
) {
411415
try {
416+
this.threadLocalPermissionCache.get().push(new HashMap<>());
412417
return this.testPermission_(sender, permission);
413418
} finally {
414-
this.threadLocalPermissionCache.get().clear();
419+
this.threadLocalPermissionCache.get().pop();
415420
}
416421
}
417422

0 commit comments

Comments
 (0)