AccessController.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* AccessController.java --- Access control context and permission checker
  2. Copyright (C) 2001, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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. *
  43. * @author Mark Wielaard (mark@klomp.org)
  44. * @since 1.2
  45. */
  46. public final class AccessController
  47. {
  48. /**
  49. * This class only has static methods so there is no public contructor.
  50. */
  51. private AccessController()
  52. {
  53. }
  54. /**
  55. * Checks wether the access control context of the current thread allows
  56. * the given Permission. Throws an <code>AccessControlException</code>
  57. * when the permission is not allowed in the current context. Otherwise
  58. * returns silently without throwing an exception.
  59. *
  60. * @param perm the permission to be checked.
  61. * @exception AccessControlException thrown if the current context does not
  62. * allow the given permission.
  63. */
  64. public static void checkPermission(Permission perm)
  65. throws AccessControlException
  66. {
  67. getContext().checkPermission(perm);
  68. }
  69. /**
  70. * Calls the <code>run()</code> method of the given action with as
  71. * (initial) access control context only the protection domain of the
  72. * calling class. Calls to <code>checkPermission()</code> in the
  73. * <code>run()</code> method ignore all earlier protection domains of
  74. * classes in the call chain. Note that the protection domains of classes
  75. * called by the code in the <code>run()</code> method are not ignored.
  76. *
  77. * @param action the <code>PrivilegedAction</code> whose <code>run()</code>
  78. * should be be called.
  79. * @return the result of the <code>action.run()</code> method.
  80. */
  81. public static <T> T doPrivileged(PrivilegedAction<T> action)
  82. {
  83. VMAccessController.pushContext(null);
  84. try
  85. {
  86. return action.run();
  87. }
  88. finally
  89. {
  90. VMAccessController.popContext();
  91. }
  92. }
  93. /**
  94. * Calls the <code>run()</code> method of the given action with as
  95. * (initial) access control context the given context combined with the
  96. * protection domain of the calling class. Calls to
  97. * <code>checkPermission()</code> in the <code>run()</code> method ignore
  98. * all earlier protection domains of classes in the call chain, but add
  99. * checks for the protection domains given in the supplied context.
  100. *
  101. * @param action the <code>PrivilegedAction</code> whose <code>run()</code>
  102. * should be be called.
  103. * @param context the <code>AccessControlContext</code> whose protection
  104. * domains should be added to the protection domain of the calling class.
  105. * @return the result of the <code>action.run()</code> method.
  106. */
  107. public static <T> T doPrivileged(PrivilegedAction<T> action,
  108. AccessControlContext context)
  109. {
  110. VMAccessController.pushContext(context);
  111. try
  112. {
  113. return action.run();
  114. }
  115. finally
  116. {
  117. VMAccessController.popContext();
  118. }
  119. }
  120. /**
  121. * Calls the <code>run()</code> method of the given action with as
  122. * (initial) access control context only the protection domain of the
  123. * calling class. Calls to <code>checkPermission()</code> in the
  124. * <code>run()</code> method ignore all earlier protection domains of
  125. * classes in the call chain. Note that the protection domains of classes
  126. * called by the code in the <code>run()</code> method are not ignored.
  127. * If the <code>run()</code> method throws an exception then this method
  128. * will wrap that exception in an <code>PrivilegedActionException</code>.
  129. *
  130. * @param action the <code>PrivilegedExceptionAction</code> whose
  131. * <code>run()</code> should be be called.
  132. * @return the result of the <code>action.run()</code> method.
  133. * @exception PrivilegedActionException wrapped around any checked exception
  134. * that is thrown in the <code>run()</code> method.
  135. */
  136. public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
  137. throws PrivilegedActionException
  138. {
  139. VMAccessController.pushContext(null);
  140. try
  141. {
  142. return action.run();
  143. }
  144. catch (RuntimeException e)
  145. {
  146. throw e;
  147. }
  148. catch (Exception e)
  149. {
  150. throw new PrivilegedActionException(e);
  151. }
  152. finally
  153. {
  154. VMAccessController.popContext();
  155. }
  156. }
  157. /**
  158. * Calls the <code>run()</code> method of the given action with as
  159. * (initial) access control context the given context combined with the
  160. * protection domain of the calling class. Calls to
  161. * <code>checkPermission()</code> in the <code>run()</code> method ignore
  162. * all earlier protection domains of classes in the call chain, but add
  163. * checks for the protection domains given in the supplied context.
  164. * If the <code>run()</code> method throws an exception then this method
  165. * will wrap that exception in an <code>PrivilegedActionException</code>.
  166. *
  167. * @param action the <code>PrivilegedExceptionAction</code> whose
  168. * <code>run()</code> should be be called.
  169. * @param context the <code>AccessControlContext</code> whose protection
  170. * domains should be added to the protection domain of the calling class.
  171. * @return the result of the <code>action.run()</code> method.
  172. * @exception PrivilegedActionException wrapped around any checked exception
  173. * that is thrown in the <code>run()</code> method.
  174. */
  175. public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
  176. AccessControlContext context)
  177. throws PrivilegedActionException
  178. {
  179. VMAccessController.pushContext(context);
  180. try
  181. {
  182. return action.run();
  183. }
  184. catch (RuntimeException e)
  185. {
  186. throw e;
  187. }
  188. catch (Exception e)
  189. {
  190. throw new PrivilegedActionException(e);
  191. }
  192. finally
  193. {
  194. VMAccessController.popContext();
  195. }
  196. }
  197. /**
  198. * Returns the complete access control context of the current thread.
  199. * The returned object encompasses all {@link ProtectionDomain} objects
  200. * for all classes in the current call stack, or the set of protection
  201. * domains until the last call to {@link
  202. * #doPrivileged(java.security.PrivilegedAction)}.
  203. *
  204. * <p>Additionally, if a call was made to {@link
  205. * #doPrivileged(java.security.PrivilegedAction,java.security.AccessControlContext)}
  206. * that supplied an {@link AccessControlContext}, then that context
  207. * will be intersected with the calculated one.
  208. *
  209. * @return The context.
  210. */
  211. public static AccessControlContext getContext()
  212. {
  213. return VMAccessController.getContext();
  214. }
  215. }