Java Doc for AccessController.java in  » 6.0-JDK-Modules » j2me » java » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.security 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.security.AccessController

AccessController
final public class AccessController (Code)

The AccessController class is used for access control operations and decisions.

More specifically, the AccessController class is used for three purposes:

  • to decide whether an access to a critical system resource is to be allowed or denied, based on the security policy currently in effect,

  • to mark code as being "privileged", thus affecting subsequent access determinations, and

  • to obtain a "snapshot" of the current calling context so access-control decisions from a different context can be made with respect to the saved context.

The AccessController.checkPermission(Permission) checkPermission method determines whether the access request indicated by a specified permission should be granted or denied. A sample call appears below. In this example, checkPermission will determine whether or not to grant "read" access to the file named "testFile" in the "/temp" directory.

 FilePermission perm = new FilePermission("/temp/testFile", "read");
 AccessController.checkPermission(perm);
 

If a requested access is allowed, checkPermission returns quietly. If denied, an AccessControlException is thrown. AccessControlException can also be thrown if the requested permission is of an incorrect type or contains an invalid value. Such information is given whenever possible. Suppose the current thread traversed m callers, in the order of caller 1 to caller 2 to caller m. Then caller m invoked the checkPermission method. The checkPermission method determines whether access is granted or denied based on the following algorithm:

 i = m;
 while (i > 0) {
 if (caller i's domain does not have the permission)
 throw AccessControlException
 else if (caller i is marked as privileged) {
 if (a context was specified in the call to doPrivileged) 
 context.checkPermission(permission)
 return;
 }
 i = i - 1;
 };
 // Next, check the context inherited when
 // the thread was created. Whenever a new thread is created, the
 // AccessControlContext at that time is
 // stored and associated with the new thread, as the "inherited"
 // context.
 inheritedContext.checkPermission(permission);
 

A caller can be marked as being "privileged" (see AccessController.doPrivileged(PrivilegedAction) doPrivileged and below). When making access control decisions, the checkPermission method stops checking if it reaches a caller that was marked as "privileged" via a doPrivileged call without a context argument (see below for information about a context argument). If that caller's domain has the specified permission, no further checking is done and checkPermission returns quietly, indicating that the requested access is allowed. If that domain does not have the specified permission, an exception is thrown, as usual.

The normal use of the "privileged" feature is as follows. If you don't need to return a value from within the "privileged" block, do the following:

 somemethod() {
 ...normal code here...
 AccessController.doPrivileged(new PrivilegedAction() {
 public Object run() {
 // privileged code goes here, for example:
 System.loadLibrary("awt");
 return null; // nothing to return
 }
 });
 ...normal code here...
 }
 

PrivilegedAction is an interface with a single method, named run, that returns an Object. The above example shows creation of an implementation of that interface; a concrete implementation of the run method is supplied. When the call to doPrivileged is made, an instance of the PrivilegedAction implementation is passed to it. The doPrivileged method calls the run method from the PrivilegedAction implementation after enabling privileges, and returns the run method's return value as the doPrivileged return value (which is ignored in this example).

If you need to return a value, you can do something like the following:

 somemethod() {
 ...normal code here...
 String user = (String) AccessController.doPrivileged(
 new PrivilegedAction() {
 public Object run() {
 return System.getProperty("user.name");
 }
 }
 );
 ...normal code here...
 }
 

If the action performed in your run method could throw a "checked" exception (those listed in the throws clause of a method), then you need to use the PrivilegedExceptionAction interface instead of the PrivilegedAction interface:

 somemethod() throws FileNotFoundException {
 ...normal code here...
 try {
 FileInputStream fis = (FileInputStream) AccessController.doPrivileged(
 new PrivilegedExceptionAction() {
 public Object run() throws FileNotFoundException {
 return new FileInputStream("someFile");
 }
 }
 );
 } catch (PrivilegedActionException e) {
 // e.getException() should be an instance of FileNotFoundException,
 // as only "checked" exceptions will be "wrapped" in a
 // PrivilegedActionException.
 throw (FileNotFoundException) e.getException();
 }
 ...normal code here...
 }
 

Be *very* careful in your use of the "privileged" construct, and always remember to make the privileged code section as small as possible.

Note that checkPermission always performs security checks within the context of the currently executing thread. Sometimes a security check that should be made within a given context will actually need to be done from within a different context (for example, from within a worker thread). The AccessController.getContext() getContext method and AccessControlContext class are provided for this situation. The getContext method takes a "snapshot" of the current calling context, and places it in an AccessControlContext object, which it returns. A sample call is the following:

 AccessControlContext acc = AccessController.getContext()
 

AccessControlContext itself has a checkPermission method that makes access decisions based on the context it encapsulates, rather than that of the current execution thread. Code within a different context can thus call that method on the previously-saved AccessControlContext object. A sample call is the following:

 acc.checkPermission(permission)
 

There are also times where you don't know a priori which permissions to check the context against. In these cases you can use the doPrivileged method that takes a context:

 somemethod() {
 AccessController.doPrivileged(new PrivilegedAction() {
 public Object run() {
 // Code goes here. Any permission checks within this
 // run method will require that the intersection of the
 // callers protection domain and the snapshot's
 // context have the desired permission.
 }
 }, acc);
 ...normal code here...
 }
 

See Also:   AccessControlContext
version:
   1.48 00/05/03
author:
   Li Gong
author:
   Roland Schemers




Method Summary
public static  voidcheckPermission(Permission perm)
     Determines whether the access request indicated by the specified permission should be allowed or denied, based on the security policy currently in effect.
public static  ObjectdoPrivileged(PrivilegedAction action)
     Performs the specified PrivilegedAction with privileges enabled.
public static  ObjectdoPrivileged(PrivilegedAction action, AccessControlContext context)
     Performs the specified PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext. The action is performed with the intersection of the permissions possessed by the caller's protection domain, and those possessed by the domains represented by the specified AccessControlContext.

If the action's run method throws an (unchecked) exception, it will propagate through this method.
Parameters:
  action - the action to be performed.
Parameters:
  context - an access control context representing therestriction to be applied to the caller's domain'sprivileges before performing the specified action.

public static  ObjectdoPrivileged(PrivilegedExceptionAction action)
     Performs the specified PrivilegedExceptionAction with privileges enabled.
public static  ObjectdoPrivileged(PrivilegedExceptionAction action, AccessControlContext context)
     Performs the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext.
public static  AccessControlContextgetContext()
     This method takes a "snapshot" of the current calling context, which includes the current Thread's inherited AccessControlContext, and places it in an AccessControlContext object.
native static  AccessControlContextgetInheritedAccessControlContext()
     Returns the "inherited" AccessControl context.



Method Detail
checkPermission
public static void checkPermission(Permission perm) throws AccessControlException(Code)
Determines whether the access request indicated by the specified permission should be allowed or denied, based on the security policy currently in effect. This method quietly returns if the access request is permitted, or throws a suitable AccessControlException otherwise.
Parameters:
  perm - the requested permission.
exception:
  AccessControlException - if the specified permissionis not permitted, based on the current security policy.



doPrivileged
public static Object doPrivileged(PrivilegedAction action)(Code)
Performs the specified PrivilegedAction with privileges enabled. The action is performed with all of the permissions possessed by the caller's protection domain.

If the action's run method throws an (unchecked) exception, it will propagate through this method.
Parameters:
  action - the action to be performed. the value returned by the action's run method.
See Also:   AccessController.doPrivileged(PrivilegedAction,AccessControlContext)
See Also:   AccessController.doPrivileged(PrivilegedExceptionAction)




doPrivileged
public static Object doPrivileged(PrivilegedAction action, AccessControlContext context)(Code)
Performs the specified PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext. The action is performed with the intersection of the permissions possessed by the caller's protection domain, and those possessed by the domains represented by the specified AccessControlContext.

If the action's run method throws an (unchecked) exception, it will propagate through this method.
Parameters:
  action - the action to be performed.
Parameters:
  context - an access control context representing therestriction to be applied to the caller's domain'sprivileges before performing the specified action. the value returned by the action's run method.
See Also:   AccessController.doPrivileged(PrivilegedAction)
See Also:   AccessController.doPrivileged(PrivilegedExceptionAction,AccessControlContext)




doPrivileged
public static Object doPrivileged(PrivilegedExceptionAction action) throws PrivilegedActionException(Code)
Performs the specified PrivilegedExceptionAction with privileges enabled. The action is performed with all of the permissions possessed by the caller's protection domain.

If the action's run method throws an unchecked exception, it will propagate through this method.
Parameters:
  action - the action to be performed the value returned by the action's run method
throws:
  PrivilegedActionException - if the specified action'srun method threw a checked exception
See Also:   AccessController.doPrivileged(PrivilegedAction)
See Also:   AccessController.doPrivileged(PrivilegedExceptionAction,AccessControlContext)




doPrivileged
public static Object doPrivileged(PrivilegedExceptionAction action, AccessControlContext context) throws PrivilegedActionException(Code)
Performs the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext. The action is performed with the intersection of the the permissions possessed by the caller's protection domain, and those possessed by the domains represented by the specified AccessControlContext.

If the action's run method throws an unchecked exception, it will propagate through this method.
Parameters:
  action - the action to be performed
Parameters:
  context - an access control context representing therestriction to be applied to the caller's domain'sprivileges before performing the specified action the value returned by the action's run method
throws:
  PrivilegedActionException - if the specified action'srun methodthrew a checked exception
See Also:   AccessController.doPrivileged(PrivilegedAction)
See Also:   AccessController.doPrivileged(PrivilegedExceptionAction,AccessControlContext)




getContext
public static AccessControlContext getContext()(Code)
This method takes a "snapshot" of the current calling context, which includes the current Thread's inherited AccessControlContext, and places it in an AccessControlContext object. This context may then be checked at a later point, possibly in another thread.
See Also:   AccessControlContext the AccessControlContext based on the current context.



getInheritedAccessControlContext
native static AccessControlContext getInheritedAccessControlContext()(Code)
Returns the "inherited" AccessControl context. This is the context that existed when the thread was created. Package private so AccessControlContext can use it.



Methods inherited from java.lang.Object
public boolean equals(Object obj)(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.