1 package org
.argeo
.cms
.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 evolution 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
{
18 private final static boolean useThreadLocal
= Boolean
19 .parseBoolean(System
.getProperty("jdk.security.auth.subject.useTL"));
21 private final static InheritableThreadLocal
<Subject
> current
= new InheritableThreadLocal
<>();
23 public static Subject
current() {
27 Subject subject
= Subject
.getSubject(AccessController
.getContext());
32 public static <T
> T
callAs(Subject subject
, Callable
<T
> action
) {
34 Subject previous
= current();
38 } catch (Exception e
) {
39 throw new CompletionException("Failed to execute action for " + subject
, e
);
41 current
.set(previous
);
45 return Subject
.doAs(subject
, new PrivilegedExceptionAction
<T
>() {
48 public T
run() throws Exception
{
53 } catch (PrivilegedActionException e
) {
54 throw new CompletionException("Failed to execute action for " + subject
, e
.getCause());
55 } catch (Exception e
) {
56 throw new CompletionException("Failed to execute action for " + subject
, e
);
62 private CurrentSubject() {