AccessControlContext.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* AccessControlContext.java --- Access Control Context Class
  2. Copyright (C) 1999 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. AccessControlContext makes system resource access decsion
  34. based on permission rights.
  35. It is used for a specific context and has only one method
  36. checkPermission. It is similar to AccessController except
  37. that it makes decsions based on the current context instead
  38. of the the current thread.
  39. It is created by call AccessController.getContext method.
  40. @author Mark Benvenuto
  41. @since JDK 1.2
  42. */
  43. public final class AccessControlContext
  44. {
  45. private ProtectionDomain protectionDomain[];
  46. private DomainCombiner combiner;
  47. /**
  48. Construct a new AccessControlContext with the specified
  49. ProtectionDomains. <code>context</code> must not be
  50. null and duplicates will be removed.
  51. @param context The ProtectionDomains to use
  52. */
  53. public AccessControlContext(ProtectionDomain[]context)
  54. {
  55. int i, j, k, count = context.length, count2 = 0;
  56. for (i = 0, j = 0; i < count; i++)
  57. {
  58. for (k = 0; k < i; k++)
  59. if (context[k] == protectionDomain[i])
  60. break;
  61. if (k != i) //it means previous loop did not complete
  62. continue;
  63. count2++;
  64. }
  65. protectionDomain = new ProtectionDomain[count2];
  66. for (i = 0, j = 0; i < count2; i++)
  67. {
  68. for (k = 0; k < i; k++)
  69. if (context[k] == protectionDomain[i])
  70. break;
  71. if (k != i) //it means previous loop did not complete
  72. continue;
  73. protectionDomain[j++] = context[i];
  74. }
  75. }
  76. /**
  77. Construct a new AccessControlContext with the specified
  78. ProtectionDomains and DomainCombiner
  79. @param context The ProtectionDomains to use
  80. @since JDK 1.3
  81. */
  82. public AccessControlContext(AccessControlContext acc,
  83. DomainCombiner combiner)
  84. {
  85. this(acc.protectionDomain);
  86. this.combiner = combiner;
  87. }
  88. /**
  89. Returns the Domain Combiner associated with the AccessControlContext
  90. @returns the DomainCombiner
  91. */
  92. public DomainCombiner getDomainCombiner()
  93. {
  94. return combiner;
  95. }
  96. /**
  97. Determines whether or not the specific permission is granted
  98. depending on the context it is within.
  99. @param perm a permission to check
  100. @throws AccessControlException if the permssion is not permitted
  101. */
  102. public void checkPermission(Permission perm) throws AccessControlException
  103. {
  104. for (int i = 0; i < protectionDomain.length; i++)
  105. if (protectionDomain[i].implies(perm) == true)
  106. return;
  107. throw new AccessControlException("Permission not granted");
  108. }
  109. /**
  110. Checks if two AccessControlContexts are equal.
  111. It first checks if obj is an AccessControlContext class, and
  112. then checks if each ProtectionDomain matches.
  113. @param obj The object to compare this class to
  114. @return true if equal, false otherwise
  115. */
  116. public boolean equals(Object obj)
  117. {
  118. if (obj instanceof AccessControlContext)
  119. {
  120. AccessControlContext acc = (AccessControlContext) obj;
  121. if (acc.protectionDomain.length != protectionDomain.length)
  122. return false;
  123. for (int i = 0; i < protectionDomain.length; i++)
  124. if (acc.protectionDomain[i] != protectionDomain[i])
  125. return false;
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. Computes a hash code of this class
  132. @return a hash code representing this class
  133. */
  134. public int hashCode()
  135. {
  136. int h = 0;
  137. for (int i = 0; i < protectionDomain.length; i++)
  138. h ^= protectionDomain[i].hashCode();
  139. return h;
  140. }
  141. }