1. /*
  2. * @(#)Permission.java 1.33 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. /**
  9. * Abstract class for representing access to a system resource.
  10. * All permissions have a name (whose interpretation depends on the subclass),
  11. * as well as abstract functions for defining the semantics of the
  12. * particular Permission subclass.
  13. *
  14. * <p>Most Permission objects also include an "actions" list that tells the actions
  15. * that are permitted for the object. For example,
  16. * for a <code>java.io.FilePermission</code> object, the permission name is
  17. * the pathname of a file (or directory), and the actions list
  18. * (such as "read, write") specifies which actions are granted for the
  19. * specified file (or for files in the specified directory).
  20. * The actions list is optional for Permission objects, such as
  21. * <code>java.lang.RuntimePermission</code>,
  22. * that don't need such a list; you either have the named permission (such
  23. * as "system.exit") or you don't.
  24. *
  25. * <p>An important method that must be implemented by each subclass is
  26. * the <code>implies</code> method to compare Permissions. Basically,
  27. * "permission p1 implies permission p2" means that
  28. * if one is granted permission p1, one is naturally granted permission p2.
  29. * Thus, this is not an equality test, but rather more of a
  30. * subset test.
  31. *
  32. * <P> Permission objects are similar to String objects in that they
  33. * are immutable once they have been created. Subclasses should not
  34. * provide methods that can change the state of a permission
  35. * once it has been created.
  36. *
  37. * @see Permissions
  38. * @see PermissionCollection
  39. *
  40. * @version 1.33 01/11/29
  41. *
  42. * @author Marianne Mueller
  43. * @author Roland Schemers
  44. */
  45. public abstract class Permission implements Guard, java.io.Serializable {
  46. private String name;
  47. /**
  48. * Constructs a permission with the specified name.
  49. *
  50. * @param name name of the Permission object being created.
  51. *
  52. */
  53. public Permission(String name) {
  54. this.name = name;
  55. }
  56. /**
  57. * Implements the guard interface for a permission. The
  58. * <code>SecurityManager.checkPermission</code> method is called,
  59. * passing this permission object as the permission to check.
  60. * Returns silently if access is granted. Otherwise, throws
  61. * a SecurityException.
  62. *
  63. * @param object the object being guarded (currently ignored).
  64. *
  65. * @throws SecurityException
  66. * if a security manager exists and its
  67. * <code>checkPermission</code> method doesn't allow access.
  68. *
  69. * @see Guard
  70. * @see GuardedObject
  71. * @see SecurityManager#checkPermission
  72. *
  73. */
  74. public void checkGuard(Object object) throws SecurityException {
  75. SecurityManager sm = System.getSecurityManager();
  76. if (sm != null) sm.checkPermission(this);
  77. }
  78. /**
  79. * Checks if the specified permission's actions are "implied by"
  80. * this object's actions.
  81. * <P>
  82. * This must be implemented by subclasses of Permission, as they are the
  83. * only ones that can impose semantics on a Permission object.
  84. *
  85. * <p>The <code>implies</code> method is used by the AccessController to determine
  86. * whether or not a requested permission is implied by another permission that
  87. * is known to be valid in the current execution context.
  88. *
  89. * @param permission the permission to check against.
  90. *
  91. * @return true if the specified permission is implied by this object,
  92. * false if not.
  93. */
  94. public abstract boolean implies(Permission permission);
  95. /**
  96. * Checks two Permission objects for equality.
  97. * <P>
  98. * Do not use the <code>equals</code> method for making access control
  99. * decisions; use the <code>implies</code> method.
  100. *
  101. * @param obj the object we are testing for equality with this object.
  102. *
  103. * @return true if both Permission objects are equivalent.
  104. */
  105. public abstract boolean equals(Object obj);
  106. /**
  107. * Returns the hash code value for this Permission object.
  108. * <P>
  109. * The required <code>hashCode</code> behavior for Permission Objects is
  110. * the following: <p>
  111. * <ul>
  112. * <li>Whenever it is invoked on the same Permission object more than
  113. * once during an execution of a Java application, the
  114. * <code>hashCode</code> method
  115. * must consistently return the same integer. This integer need not
  116. * remain consistent from one execution of an application to another
  117. * execution of the same application. <p>
  118. * <li>If two Permission objects are equal according to the
  119. * <code>equals</code>
  120. * method, then calling the <code>hashCode</code> method on each of the
  121. * two Permission objects must produce the same integer result.
  122. * </ul>
  123. *
  124. * @return a hash code value for this object.
  125. */
  126. public abstract int hashCode();
  127. /**
  128. * Returns the name of this Permission.
  129. * For example, in the case of a <code>java.io.FilePermission</code>,
  130. * the name will be a pathname.
  131. *
  132. * @return the name of this Permission.
  133. *
  134. */
  135. public final String getName() {
  136. return name;
  137. }
  138. /**
  139. * Returns the actions as a String. This is abstract
  140. * so subclasses can defer creating a String representation until
  141. * one is needed. Subclasses should always return actions in what they
  142. * consider to be their
  143. * canonical form. For example, two FilePermission objects created via
  144. * the following:
  145. *
  146. * <pre>
  147. * perm1 = new FilePermission(p1,"read,write");
  148. * perm2 = new FilePermission(p2,"write,read");
  149. * </pre>
  150. *
  151. * both return
  152. * "read,write" when the <code>getActions</code> method is invoked.
  153. *
  154. * @return the actions of this Permission.
  155. *
  156. */
  157. public abstract String getActions();
  158. /**
  159. * Returns an empty PermissionCollection for a given Permission object, or null if
  160. * one is not defined. Subclasses of class Permission should
  161. * override this if they need to store their permissions in a particular
  162. * PermissionCollection object in order to provide the correct semantics
  163. * when the <code>PermissionCollection.implies</code> method is called.
  164. * If null is returned,
  165. * then the caller of this method is free to store permissions of this
  166. * type in any PermissionCollection they choose (one that uses a Hashtable,
  167. * one that uses a Vector, etc).
  168. *
  169. * @return a new PermissionCollection object for this type of Permission, or
  170. * null if one is not defined.
  171. */
  172. public PermissionCollection newPermissionCollection() {
  173. return null;
  174. }
  175. /**
  176. * Returns a string describing this Permission. The convention is to
  177. * specify the class name, the permission name, and the actions in
  178. * the following format: '("ClassName" "name" "actions")'.
  179. *
  180. * @return information about this Permission.
  181. */
  182. public String toString() {
  183. return "(" + getClass().getName() + " " +
  184. name + " " + getActions() + ")";
  185. }
  186. }