1 package org
.argeo
.util
;
3 import java
.security
.AccessController
;
4 import java
.security
.PrivilegedActionException
;
5 import java
.security
.PrivilegedExceptionAction
;
6 import java
.util
.concurrent
.Callable
;
7 import java
.util
.concurrent
.CompletionException
;
9 import javax
.security
.auth
.Subject
;
12 * Prepare evotion of Java APIs introduced in JDK 18, as these static methods
13 * will be added to {@link Subject}.
15 @SuppressWarnings("removal")
16 public class CurrentSubject
{
19 private CurrentSubject() {
23 public static Subject
current() {
24 Subject subject
= Subject
.getSubject(AccessController
.getContext());
26 throw new IllegalStateException("Cannot find related subject");
30 public static <T
> T
callAs(Subject subject
, Callable
<T
> action
) {
32 return Subject
.doAs(subject
, new PrivilegedExceptionAction
<T
>() {
35 public T
run() throws Exception
{
40 } catch (PrivilegedActionException e
) {
41 throw new CompletionException("Failed to execute action for " + subject
, e
.getCause());