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