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