/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package com.sun.jini.thread;

import java.security.Permission;
import java.security.PrivilegedAction;

/**
 * Provides security-checked access to internal thread pools as a
 * java.security.PrivilegedAction, to be used conveniently with an
 * AccessController.doPrivileged or Security.doPrivileged.
 *
 * There are two internal thread pools: one of threads in the system
 * thread group, for executing tasks to be guarded by the security
 * policy for the system thread group, and one of threads in a
 * non-system thread group, for executing tasks with user code that
 * should not be restricted by that policy.
 *
 * If there is a security manager, the run method will check the
 * ThreadPoolPermission for the requested thread pool.  If used with a
 * doPrivileged (the typical case), then only the protection domain of
 * the immediate caller of the doPrivileged needs the permission.
 *
 * The thread pools execute an action in a thread without the security
 * context in which the execute method was invoked, without any
 * subject, and with the system class loader as the context class
 * loader.  Actions are expected to complete with the same context
 * class loader and other thread-specific state (such as priority)
 * that they were started with.
 *
 * @author Sun Microsystems, Inc.
 **/
public final class GetThreadPoolAction implements PrivilegedAction {

    /** pool of threads for executing tasks in system thread group */
    private static final ThreadPool systemThreadPool =
	new ThreadPool(NewThreadAction.systemThreadGroup);

    /** pool of threads for executing tasks with user code */
    private static final ThreadPool userThreadPool =
	new ThreadPool(NewThreadAction.userThreadGroup);

    private static final Permission getSystemThreadPoolPermission =
	new ThreadPoolPermission("getSystemThreadPool");
    private static final Permission getUserThreadPoolPermission =
	new ThreadPoolPermission("getUserThreadPool");

    private final boolean user;

    /**
     * Creates an action that will obtain an internal thread pool.
     * When run, this action verifies that the current access control
     * context has permission to access the thread group used by the
     * indicated pool.
     *
     * @param	user if true, will obtain the non-system thread group
     * pool for executing user code; if false, will obtain the system
     * thread group pool
     */
    public GetThreadPoolAction(boolean user) {
	this.user = user;
    }

    public Object run() {
	SecurityManager sm = System.getSecurityManager();
	if (sm != null) {
	    sm.checkPermission(user ? getUserThreadPoolPermission :
			       getSystemThreadPoolPermission);
	}
	return user ? userThreadPool : systemThreadPool;
    }
}
