1. /*
  2. * @(#)PermissionCollection.java 1.26 00/02/02
  3. *
  4. * Copyright 1997-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.util.*;
  12. /**
  13. * Abstract class representing a collection of Permission objects.
  14. *
  15. * <p>With a PermissionCollection, you can:
  16. * <UL>
  17. * <LI> add a permission to the collection using the <code>add</code> method.
  18. * <LI> check to see if a particular permission is implied in the
  19. * collection, using the <code>implies</code> method.
  20. * <LI> enumerate all the permissions, using the <code>elements</code> method.
  21. * </UL>
  22. * <P>
  23. *
  24. * <p>When it is desirable to group together a number of Permission objects of the
  25. * same type, the <code>newPermissionCollection</code> method on that particular
  26. * type of Permission object should first be called. The default behavior (from the
  27. * Permission class) is to simply return null. Subclasses of class Permission
  28. * override the method if they need to store their permissions in a particular
  29. * PermissionCollection object in order to provide the correct semantics
  30. * when the <code>PermissionCollection.implies</code> method is called.
  31. * If a non-null value is returned, that PermissionCollection must be used.
  32. * If null is returned, then the caller of <code>newPermissionCollection</code>
  33. * is free to store permissions of the
  34. * given type in any PermissionCollection they choose (one that uses a Hashtable,
  35. * one that uses a Vector, etc).
  36. *
  37. * <p>The PermissionCollection returned by the
  38. * <code>Permission.newPermissionCollection</code>
  39. * method is a homogeneous collection, which stores only Permission objects
  40. * for a given Permission type. A PermissionCollection may also be heterogenous.
  41. * For example, Permissions is a PermissionCollection subclass that represents a
  42. * collection of PermissionCollections. That is, its members are each a homogeneous
  43. * PermissionCollection. For example, a Permissions object might have a
  44. * FilePermissionCollection
  45. * for all the FilePermission objects, a SocketPermissionCollection for all the
  46. * SocketPermission objects, and so on. Its <code>add</code> method adds a permission
  47. * to the appropriate collection.
  48. *
  49. * <p>Whenever a permission is added to a heterogeneous PermissionCollection such
  50. * as Permissions, and the PermissionCollection doesn't yet contain a
  51. * PermissionCollection of the specified permission's type, the
  52. * PermissionCollection should call
  53. * the <code>newPermissionCollection</code> method on the permission's class
  54. * to see if it requires a special PermissionCollection. If
  55. * <code>newPermissionCollection</code>
  56. * returns null, the PermissionCollection
  57. * is free to store the permission in any type of PermissionCollection it desires
  58. * (one using a Hastable, one using a Vector, etc.). For example,
  59. * the Permissions object uses a default PermissionCollection implementation
  60. * that stores the permission objects in a Hashtable.
  61. *
  62. * @see Permission
  63. * @see Permissions
  64. *
  65. * @version 1.26 00/02/02
  66. *
  67. * @author Roland Schemers
  68. */
  69. public abstract class PermissionCollection implements java.io.Serializable {
  70. // when set, add will throw an exception.
  71. private boolean readOnly;
  72. /**
  73. * Adds a permission object to the current collection of permission objects.
  74. *
  75. * @param permission the Permission object to add.
  76. *
  77. * @exception SecurityException - if this PermissionCollection object
  78. * has been marked readonly
  79. */
  80. public abstract void add(Permission permission);
  81. /**
  82. * Checks to see if the specified permission is implied by
  83. * the collection of Permission objects held in this PermissionCollection.
  84. *
  85. * @param permission the Permission object to compare.
  86. *
  87. * @return true if "permission" is implied by the permissions in
  88. * the collection, false if not.
  89. */
  90. public abstract boolean implies(Permission permission);
  91. /**
  92. * Returns an enumeration of all the Permission objects in the collection.
  93. *
  94. * @return an enumeration of all the Permissions.
  95. */
  96. public abstract Enumeration elements();
  97. /**
  98. * Marks this PermissionCollection object as "readonly". After
  99. * a PermissionCollection object
  100. * is marked as readonly, no new Permission objects can be added to it
  101. * using <code>add</code>.
  102. */
  103. public void setReadOnly() {
  104. readOnly = true;
  105. }
  106. /**
  107. * Returns true if this PermissionCollection object is marked as readonly. If it
  108. * is readonly, no new Permission objects can be added to it
  109. * using <code>add</code>.
  110. *
  111. * <p>By default, the object is <i>not</i> readonly. It can be set to readonly
  112. * by a call to <code>setReadOnly</code>.
  113. *
  114. * @return true if this PermissionCollection object is marked as readonly, false
  115. * otherwise.
  116. */
  117. public boolean isReadOnly() {
  118. return readOnly;
  119. }
  120. /**
  121. * Returns a string describing this PermissionCollection object,
  122. * providing information about all the permissions it contains.
  123. * The format is:
  124. * <pre>
  125. * super.toString() (
  126. * // enumerate all the Permission
  127. * // objects and call toString() on them,
  128. * // one per line..
  129. * )</pre>
  130. *
  131. * <code>super.toString</code> is a call to the <code>toString</code>
  132. * method of this
  133. * object's superclass, which is Object. The result is
  134. * this PermissionCollection's type name followed by this object's
  135. * hashcode, thus enabling clients to differentiate different
  136. * PermissionCollections object, even if they contain the same permissions.
  137. *
  138. * @return information about this PermissionCollection object,
  139. * as described above.
  140. *
  141. */
  142. public String toString() {
  143. Enumeration enum = elements();
  144. StringBuffer sb = new StringBuffer();
  145. sb.append(super.toString()+" (\n");
  146. while (enum.hasMoreElements()) {
  147. try {
  148. sb.append(" ");
  149. sb.append(enum.nextElement().toString());
  150. sb.append("\n");
  151. } catch (NoSuchElementException e){
  152. // ignore
  153. }
  154. }
  155. sb.append(")\n");
  156. return sb.toString();
  157. }
  158. }