Name
PrivilegedAction<T>
Synopsis
This interface
defines a block of code (the
run( )
method) that is to be executed as
privileged code by the AccessController.doPrivileged(
)
method. In Java 5.0 this interface is generic and the
type variable T
represents the return type
of the run( )
method. When privileged code is run
with the doPrivileged( )
method, the
AccessController
looks only at the permissions of
the immediate caller, not the permissions of the entire call stack.
The immediate caller is typically fully trusted system code that has
a full set of permissions, and therefore the privileged code runs
with that full set of permissions, even if the system code is invoked
by untrusted code with no permissions whatsoever.
Privileged code is typically required only when you are writing a
trusted system library (such as a Java extension package) that must
read local files or perform other restricted actions, even when
called by untrusted code. For example, a class that must call
System.loadLibrary( )
to load native methods
should make the call to loadLibrary( )
within the
run( )
method of a
PrivilegedAction
. If your privileged code may
throw a checked exception, implement it in the run(
)
method of a PrivilegedExceptionAction
instead.
Be very careful when implementing this interface. To minimize the
possibility of security holes, keep the body of the run(
)
method as short as possible.
public interface PrivilegedAction<T> { // Public Instance Methods T run( ); }
Passed To
AccessController.doPrivileged( ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.