< prev index next >

src/java.base/share/classes/javax/security/auth/Subject.java

Print this page

 419      *
 420      * @param subject the {@code Subject} that the specified {@code action}
 421      *               will run as.  This parameter may be {@code null}.
 422      * @param action the code to be run with {@code subject} as its current
 423      *               subject. Must not be {@code null}.
 424      * @param <T> the type of value returned by the {@code call} method
 425      *            of {@code action}
 426      * @return the value returned by the {@code call} method of {@code action}
 427      * @throws NullPointerException if {@code action} is {@code null}
 428      * @throws CompletionException if {@code action.call()} throws an exception.
 429      *      The cause of the {@code CompletionException} is set to the exception
 430      *      thrown by {@code action.call()}.
 431      * @see #current()
 432      * @since 18
 433      */
 434     public static <T> T callAs(final Subject subject,
 435             final Callable<T> action) throws CompletionException {
 436         Objects.requireNonNull(action);
 437         if (!SharedSecrets.getJavaLangAccess().allowSecurityManager()) {
 438             try {
 439                 return ScopedValue.callWhere(SCOPED_SUBJECT, subject, action::call);
 440             } catch (Exception e) {
 441                 throw new CompletionException(e);
 442             }
 443         } else {
 444             try {
 445                 PrivilegedExceptionAction<T> pa = () -> action.call();
 446                 @SuppressWarnings("removal")
 447                 var result = doAs(subject, pa);
 448                 return result;
 449             } catch (PrivilegedActionException e) {
 450                 throw new CompletionException(e.getCause());
 451             } catch (Exception e) {
 452                 throw new CompletionException(e);
 453             }
 454         }
 455     }
 456 
 457     /**
 458      * Perform work as a particular {@code Subject}.
 459      *

 419      *
 420      * @param subject the {@code Subject} that the specified {@code action}
 421      *               will run as.  This parameter may be {@code null}.
 422      * @param action the code to be run with {@code subject} as its current
 423      *               subject. Must not be {@code null}.
 424      * @param <T> the type of value returned by the {@code call} method
 425      *            of {@code action}
 426      * @return the value returned by the {@code call} method of {@code action}
 427      * @throws NullPointerException if {@code action} is {@code null}
 428      * @throws CompletionException if {@code action.call()} throws an exception.
 429      *      The cause of the {@code CompletionException} is set to the exception
 430      *      thrown by {@code action.call()}.
 431      * @see #current()
 432      * @since 18
 433      */
 434     public static <T> T callAs(final Subject subject,
 435             final Callable<T> action) throws CompletionException {
 436         Objects.requireNonNull(action);
 437         if (!SharedSecrets.getJavaLangAccess().allowSecurityManager()) {
 438             try {
 439                 return ScopedValue.where(SCOPED_SUBJECT, subject).call(action::call);
 440             } catch (Exception e) {
 441                 throw new CompletionException(e);
 442             }
 443         } else {
 444             try {
 445                 PrivilegedExceptionAction<T> pa = () -> action.call();
 446                 @SuppressWarnings("removal")
 447                 var result = doAs(subject, pa);
 448                 return result;
 449             } catch (PrivilegedActionException e) {
 450                 throw new CompletionException(e.getCause());
 451             } catch (Exception e) {
 452                 throw new CompletionException(e);
 453             }
 454         }
 455     }
 456 
 457     /**
 458      * Perform work as a particular {@code Subject}.
 459      *
< prev index next >