AccessControlContext.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* AccessControlContext.java --- Access Control Context Class
  2. Copyright (C) 1999, 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. import java.util.HashSet;
  33. /**
  34. * AccessControlContext makes system resource access decsion
  35. * based on permission rights.
  36. *
  37. * It is used for a specific context and has only one method
  38. * checkPermission. It is similar to AccessController except
  39. * that it makes decsions based on the current context instead
  40. * of the the current thread.
  41. *
  42. * It is created by call AccessController.getContext method.
  43. *
  44. * @author Mark Benvenuto
  45. * @since 1.2
  46. */
  47. public final class AccessControlContext
  48. {
  49. private final ProtectionDomain[] protectionDomains;
  50. private final DomainCombiner combiner;
  51. /**
  52. * Construct a new AccessControlContext with the specified
  53. * ProtectionDomains. <code>context</code> must not be
  54. * null and duplicates will be removed.
  55. *
  56. * @param context The ProtectionDomains to use
  57. */
  58. public AccessControlContext(ProtectionDomain[] context)
  59. {
  60. HashSet domains = new HashSet (context.length);
  61. for (int i = 0; i < context.length; i++)
  62. domains.add (context[i]);
  63. protectionDomains = (ProtectionDomain[])
  64. domains.toArray (new ProtectionDomain[domains.size()]);
  65. combiner = null;
  66. }
  67. /**
  68. * Construct a new AccessControlContext with the specified
  69. * {@link ProtectionDomain}s and {@link DomainCombiner}.
  70. *
  71. * <p>Code calling this constructor must have a {@link
  72. * SecurityPermission} of <i>createAccessControlContext</i>.</p>
  73. *
  74. * @throws SecurityException If the caller does not have permission
  75. * to create an access control context.
  76. * @since 1.3
  77. */
  78. public AccessControlContext(AccessControlContext acc,
  79. DomainCombiner combiner)
  80. {
  81. AccessControlContext acc2 = null;
  82. SecurityManager sm = System.getSecurityManager ();
  83. if (sm != null)
  84. {
  85. Permission perm =
  86. new SecurityPermission ("createAccessControlContext");
  87. // The default SecurityManager.checkPermission(perm) just calls
  88. // AccessController.checkPermission(perm) which in turn just
  89. // calls AccessController.getContext().checkPermission(perm).
  90. // This means AccessController.getContext() is called twice,
  91. // once for the security check and once by us. It's a very
  92. // expensive call (on gcj at least) so if we're using the
  93. // default security manager we avoid this duplication.
  94. if (sm.getClass() == SecurityManager.class)
  95. {
  96. acc2 = AccessController.getContext ();
  97. acc2.checkPermission (perm);
  98. }
  99. else
  100. sm.checkPermission (perm);
  101. }
  102. if (acc2 == null)
  103. acc2 = AccessController.getContext ();
  104. protectionDomains = combiner.combine (acc2.protectionDomains,
  105. acc.protectionDomains);
  106. this.combiner = combiner;
  107. }
  108. AccessControlContext (ProtectionDomain[] domains, AccessControlContext acc,
  109. DomainCombiner combiner)
  110. {
  111. protectionDomains = combiner.combine (domains, acc.protectionDomains);
  112. this.combiner = combiner;
  113. }
  114. /**
  115. * Returns the Domain Combiner associated with the AccessControlContext
  116. *
  117. * @return the DomainCombiner
  118. */
  119. public DomainCombiner getDomainCombiner()
  120. {
  121. return combiner;
  122. }
  123. /**
  124. * Determines whether or not the specific permission is granted
  125. * depending on the context it is within.
  126. *
  127. * @param perm a permission to check
  128. *
  129. * @throws AccessControlException if the permssion is not permitted
  130. */
  131. public void checkPermission(Permission perm) throws AccessControlException
  132. {
  133. if (protectionDomains.length == 0)
  134. throw new AccessControlException ("permission "
  135. + perm
  136. + " not granted: no protection domains");
  137. for (int i = 0; i < protectionDomains.length; i++)
  138. {
  139. final ProtectionDomain domain = protectionDomains[i];
  140. if (!domain.implies(perm))
  141. throw new AccessControlException ("permission "
  142. + perm
  143. + " not granted: "
  144. + domain
  145. + " does not imply it.");
  146. }
  147. }
  148. /**
  149. * Checks if two AccessControlContexts are equal.
  150. *
  151. * It first checks if obj is an AccessControlContext class, and
  152. * then checks if each ProtectionDomain matches.
  153. *
  154. * @param obj The object to compare this class to
  155. *
  156. * @return true if equal, false otherwise
  157. */
  158. public boolean equals(Object obj)
  159. {
  160. if (obj instanceof AccessControlContext)
  161. {
  162. AccessControlContext acc = (AccessControlContext) obj;
  163. if (acc.protectionDomains.length != protectionDomains.length)
  164. return false;
  165. int i, j;
  166. for (i = 0; i < protectionDomains.length; i++)
  167. {
  168. for (j = 0; j < acc.protectionDomains.length; j++)
  169. {
  170. if (acc.protectionDomains[j].equals (protectionDomains[i]))
  171. break;
  172. }
  173. if (j == acc.protectionDomains.length)
  174. return false;
  175. }
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. * Computes a hash code of this class
  182. *
  183. * @return a hash code representing this class
  184. */
  185. public int hashCode()
  186. {
  187. int h = 0;
  188. for (int i = 0; i < protectionDomains.length; i++)
  189. h ^= protectionDomains[i].hashCode();
  190. return h;
  191. }
  192. ProtectionDomain[] getProtectionDomains ()
  193. {
  194. return protectionDomains;
  195. }
  196. }