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