AccessController.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* AccessController.java --- Access control context and permission checker
  2. Copyright (C) 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. /**
  33. * Access control context and permission checker.
  34. * Can check permissions in the access control context of the current thread
  35. * through the <code>checkPermission()</code> method.
  36. * Manipulates the access control context for code that needs to be executed
  37. * the protection domain of the calling class (by explicitly ignoring the
  38. * context of the calling code) in the <code>doPrivileged()</code> methods.
  39. * And provides a <code>getContext()</code> method which gives the access
  40. * control context of the current thread that can be used for checking
  41. * permissions at a later time and/or in another thread.
  42. * <p>
  43. * XXX - Mostly a stub implementation at the moment. Needs native support
  44. * from the VM to function correctly. XXX - Do not forget to think about
  45. * how to handle <code>java.lang.reflect.Method.invoke()</code> on the
  46. * <code>doPrivileged()</code> methods.
  47. *
  48. * @author Mark Wielaard (mark@klomp.org)
  49. * @since 1.2
  50. */
  51. public final class AccessController
  52. {
  53. /**
  54. * This class only has static methods so there is no public contructor.
  55. */
  56. private AccessController()
  57. {
  58. }
  59. /**
  60. * Checks wether the access control context of the current thread allows
  61. * the given Permission. Throws an <code>AccessControlException</code>
  62. * when the permission is not allowed in the current context. Otherwise
  63. * returns silently without throwing an exception.
  64. *
  65. * @param perm the permission to be checked.
  66. * @exception AccessControlException thrown if the current context does not
  67. * allow the given permission.
  68. */
  69. public static void checkPermission(Permission perm)
  70. throws AccessControlException
  71. {
  72. getContext().checkPermission(perm);
  73. }
  74. /**
  75. * Calls the <code>run()</code> method of the given action with as
  76. * (initial) access control context only the protection domain of the
  77. * calling class. Calls to <code>checkPermission()</code> in the
  78. * <code>run()</code> method ignore all earlier protection domains of
  79. * classes in the call chain. Note that the protection domains of classes
  80. * called by the code in the <code>run()</code> method are not ignored.
  81. *
  82. * @param action the <code>PrivilegedAction</code> whose <code>run()</code>
  83. * should be be called.
  84. * @returns the result of the <code>action.run()</code> method.
  85. */
  86. public static Object doPrivileged(PrivilegedAction action)
  87. {
  88. return action.run();
  89. }
  90. /**
  91. * Calls the <code>run()</code> method of the given action with as
  92. * (initial) access control context the given context combined with the
  93. * protection domain of the calling class. Calls to
  94. * <code>checkPermission()</code> in the <code>run()</code> method ignore
  95. * all earlier protection domains of classes in the call chain, but add
  96. * checks for the protection domains given in the supplied context.
  97. *
  98. * @param action the <code>PrivilegedAction</code> whose <code>run()</code>
  99. * should be be called.
  100. * @param context the <code>AccessControlContext</code> whose protection
  101. * domains should be added to the protection domain of the calling class.
  102. * @returns the result of the <code>action.run()</code> method.
  103. */
  104. public static Object doPrivileged(PrivilegedAction action,
  105. AccessControlContext context)
  106. {
  107. return action.run();
  108. }
  109. /**
  110. * Calls the <code>run()</code> method of the given action with as
  111. * (initial) access control context only the protection domain of the
  112. * calling class. Calls to <code>checkPermission()</code> in the
  113. * <code>run()</code> method ignore all earlier protection domains of
  114. * classes in the call chain. Note that the protection domains of classes
  115. * called by the code in the <code>run()</code> method are not ignored.
  116. * If the <code>run()</code> method throws an exception then this method
  117. * will wrap that exception in an <code>PrivilegedActionException</code>.
  118. *
  119. * @param action the <code>PrivilegedExceptionAction</code> whose
  120. * <code>run()</code> should be be called.
  121. * @returns the result of the <code>action.run()</code> method.
  122. * @exception PrivilegedActionException wrapped around any exception that
  123. * is thrown in the <code>run()</code> method.
  124. */
  125. public static Object doPrivileged(PrivilegedExceptionAction action)
  126. throws PrivilegedActionException
  127. {
  128. try
  129. {
  130. return action.run();
  131. }
  132. catch (Exception e)
  133. {
  134. throw new PrivilegedActionException(e);
  135. }
  136. }
  137. /**
  138. * Calls the <code>run()</code> method of the given action with as
  139. * (initial) access control context the given context combined with the
  140. * protection domain of the calling class. Calls to
  141. * <code>checkPermission()</code> in the <code>run()</code> method ignore
  142. * all earlier protection domains of classes in the call chain, but add
  143. * checks for the protection domains given in the supplied context.
  144. * If the <code>run()</code> method throws an exception then this method
  145. * will wrap that exception in an <code>PrivilegedActionException</code>.
  146. *
  147. * @param action the <code>PrivilegedExceptionAction</code> whose
  148. * <code>run()</code> should be be called.
  149. * @param context the <code>AccessControlContext</code> whose protection
  150. * domains should be added to the protection domain of the calling class.
  151. * @returns the result of the <code>action.run()</code> method.
  152. * @exception PrivilegedActionException wrapped around any exception that
  153. * is thrown in the <code>run()</code> method.
  154. */
  155. public static Object doPrivileged(PrivilegedExceptionAction action,
  156. AccessControlContext context)
  157. throws PrivilegedActionException
  158. {
  159. try
  160. {
  161. return action.run();
  162. }
  163. catch (Exception e)
  164. {
  165. throw new PrivilegedActionException(e);
  166. }
  167. }
  168. /**
  169. * Returns the complete access control context of the current thread.
  170. * <p>
  171. * XXX - Should this include all the protection domains in the call chain
  172. * or only the domains till the last <code>doPrivileged()</code> call?
  173. * <p>
  174. * XXX - needs native support. Currently returns an empty context.
  175. */
  176. public static AccessControlContext getContext()
  177. {
  178. // For now just return an new empty context
  179. return new AccessControlContext(new ProtectionDomain[0]);
  180. }
  181. }