001/*
002 * (c) 2010 ThoughtWorks Ltd
003 * All rights reserved.
004 *
005 * The software in this package is published under the terms of the BSD
006 * style license a copy of which has been included with this distribution in
007 * the LICENSE.txt file.
008 * 
009 * Created on 19-03-2010.
010 */
011package com.thoughtworks.proxy.toys.privilege;
012
013import java.security.AccessControlContext;
014import java.security.AccessController;
015import java.security.PrivilegedActionException;
016import java.security.PrivilegedExceptionAction;
017
018
019/**
020 * Execution of a {@link PrivilegedExceptionAction} with the {@link AccessController}.
021 * 
022 * @author Jörg Schaible
023 * @since 1.0
024 */
025public class AccessControllerExecutor implements ActionExecutor {
026    private final AccessControlContext context;
027
028    /**
029     * Construct an AccessControllerExecutor that runs a {@link PrivilegedExceptionAction} with
030     * the privileges of the {@link AccessControlContext} of the caller.
031     * 
032     * @since 1.0
033     */
034    public AccessControllerExecutor() {
035        this(null);
036    }
037
038    /**
039     * Construct an AccessControllerExecutor that runs a {@link PrivilegedExceptionAction} with
040     * special privileges.
041     * 
042     * @param context the {@link AccessControlContext} defining the privileges used to run the
043     *            action
044     * @since 1.0
045     */
046    public AccessControllerExecutor(AccessControlContext context) {
047        this.context = context;
048    }
049
050    public Object execute(PrivilegedExceptionAction<Object> action)
051        throws PrivilegedActionException {
052        return AccessController.doPrivileged(action, context);
053    }
054}