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