![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.hadoop.security.AsSubjectInvoker Maven / Gradle / Ivy
package org.apache.hadoop.security;
import javax.security.auth.Subject;
import java.security.PrivilegedActionException;
import java.util.ArrayDeque;
import java.util.Deque;
class AsSubjectInvoker
{
private static ThreadLocal> SUBJECT_STACKS = new ThreadLocal>()
{
@Override
protected Deque initialValue()
{
return new ArrayDeque<>();
}
};
public static Subject getCurrentUser()
{
return subjectStack().peek();
}
private static Deque subjectStack()
{
return SUBJECT_STACKS.get();
}
public static T doAs(Subject subject, SubjectPrivilegedAction action)
{
subjectStack().push(subject);
try {
return action.run();
}
finally {
subjectStack().pop();
}
}
public static T doAs(Subject subject, SubjectPrivilegedExceptionAction action)
throws PrivilegedActionException
{
subjectStack().push(subject);
try {
return action.run();
}
catch (PrivilegedActionException e) {
throw e;
}
catch (Exception e) {
throw new PrivilegedActionException(e);
}
finally {
subjectStack().pop();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy