All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.security.AsSubjectInvoker Maven / Gradle / Ivy

The newest version!
package org.apache.hadoop.security;

import com.facebook.presto.hadoop.$internal.com.facebook.presto.hadoop.$internal.com.facebook.presto.hadoop.$internal.org.apache.avro.reflect.Nullable;

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<>();
        }
    };

    @Nullable
    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