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