1. /*
  2. * @(#)AllPermission.java 1.6 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.security.*;
  9. import java.util.Enumeration;
  10. import java.util.Hashtable;
  11. import java.util.StringTokenizer;
  12. /**
  13. * The AllPermission is a permission that implies all other permissions.
  14. * <p>
  15. * <b>Note:</b> Granting AllPermission should be done with extreme care,
  16. * as it implies all other permissions. Thus, it grants code the ability
  17. * to run with security
  18. * disabled. Extreme caution should be taken before granting such
  19. * a permission to code. This permission should be used only during testing,
  20. * or in extremely rare cases where an application or applet is
  21. * completely trusted and adding the necessary permissions to the policy
  22. * is prohibitively cumbersome.
  23. *
  24. * @see java.security.Permission
  25. * @see java.security.AccessController
  26. * @see java.security.Permissions
  27. * @see java.security.PermissionCollection
  28. * @see java.lang.SecurityManager
  29. *
  30. * @version 1.6 01/11/29
  31. *
  32. * @author Roland Schemers
  33. */
  34. public final class AllPermission extends Permission {
  35. /**
  36. * Creates a new AllPermission object.
  37. */
  38. public AllPermission()
  39. {
  40. super("<all permissions>");
  41. }
  42. /**
  43. * Creates a new AllPermission object. This
  44. * constructor exists for use by the <code>Policy</code> object
  45. * to instantiate new Permission objects.
  46. *
  47. * @param name ignored
  48. * @param actions ignored.
  49. */
  50. public AllPermission(String name, String actions)
  51. {
  52. this();
  53. }
  54. /**
  55. * Checks if the specified permission is "implied" by
  56. * this object. This method always returns true.
  57. *
  58. * @param p the permission to check against.
  59. *
  60. * @return return
  61. */
  62. public boolean implies(Permission p) {
  63. return true;
  64. }
  65. /**
  66. * Checks two AllPermission objects for equality. Two AllPermission
  67. * objects are always equal.
  68. *
  69. * @param obj the object we are testing for equality with this object.
  70. * @return true if <i>obj</i> is an AllPermission, false otherwise.
  71. */
  72. public boolean equals(Object obj) {
  73. return (obj instanceof AllPermission);
  74. }
  75. /**
  76. * Returns the hash code value for this object.
  77. *
  78. * @return a hash code value for this object.
  79. */
  80. public int hashCode() {
  81. return 1;
  82. }
  83. /**
  84. * Returns the canonical string representation of the actions.
  85. *
  86. * @return the actions.
  87. */
  88. public String getActions()
  89. {
  90. return "<all actions>";
  91. }
  92. /**
  93. * Returns a new PermissionCollection object for storing AllPermission
  94. * objects.
  95. * <p>
  96. *
  97. * @return a new PermissionCollection object suitable for
  98. * storing AllPermissions.
  99. */
  100. public PermissionCollection newPermissionCollection() {
  101. return new AllPermissionCollection();
  102. }
  103. }
  104. /**
  105. * A AllPermissionCollection stores a collection
  106. * of AllPermission permissions. AllPermission objects
  107. * must be stored in a manner that allows them to be inserted in any
  108. * order, but enable the implies function to evaluate the implies
  109. * method in an efficient (and consistent) manner.
  110. *
  111. * @see java.security.Permission
  112. * @see java.security.Permissions
  113. *
  114. * @version 1.6 02/10/07
  115. *
  116. * @author Roland Schemers
  117. */
  118. final class AllPermissionCollection
  119. extends PermissionCollection
  120. implements java.io.Serializable
  121. {
  122. private boolean all_allowed; // true if any all permissions have been added
  123. /**
  124. * Create an empty AllPermissions object.
  125. *
  126. */
  127. public AllPermissionCollection() {
  128. all_allowed = false;
  129. }
  130. /**
  131. * Adds a permission to the AllPermissions. The key for the hash is
  132. * permission.path.
  133. *
  134. * @param permission the Permission object to add.
  135. */
  136. public void add(Permission permission)
  137. {
  138. if (! (permission instanceof AllPermission))
  139. throw new IllegalArgumentException("invalid permission: "+
  140. permission);
  141. all_allowed = true;
  142. }
  143. /**
  144. * Check and see if this set of permissions implies the permissions
  145. * expressed in "permission".
  146. *
  147. * @param p the Permission object to compare
  148. *
  149. * @return always returns true.
  150. */
  151. public boolean implies(Permission permission)
  152. {
  153. return all_allowed;
  154. }
  155. /**
  156. * Returns an enumeration of all the AllPermission objects in the
  157. * container.
  158. *
  159. * @return an enumeration of all the AllPermission objects.
  160. */
  161. public Enumeration elements()
  162. {
  163. return new Enumeration() {
  164. private boolean done = false;
  165. public boolean hasMoreElements() {
  166. return !done;
  167. }
  168. public Object nextElement() {
  169. done = true;
  170. return new AllPermission();
  171. }
  172. };
  173. }
  174. }