001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security;
027
028 import java.util.*;
029
030 /**
031 * Abstract class representing a collection of Permission objects.
032 *
033 * <p>With a PermissionCollection, you can:
034 * <UL>
035 * <LI> add a permission to the collection using the <code>add</code> method.
036 * <LI> check to see if a particular permission is implied in the
037 * collection, using the <code>implies</code> method.
038 * <LI> enumerate all the permissions, using the <code>elements</code> method.
039 * </UL>
040 * <P>
041 *
042 * <p>When it is desirable to group together a number of Permission objects
043 * of the same type, the <code>newPermissionCollection</code> method on that
044 * particular type of Permission object should first be called. The default
045 * behavior (from the Permission class) is to simply return null.
046 * Subclasses of class Permission override the method if they need to store
047 * their permissions in a particular PermissionCollection object in order
048 * to provide the correct semantics when the
049 * <code>PermissionCollection.implies</code> method is called.
050 * If a non-null value is returned, that PermissionCollection must be used.
051 * If null is returned, then the caller of <code>newPermissionCollection</code>
052 * is free to store permissions of the
053 * given type in any PermissionCollection they choose
054 * (one that uses a Hashtable, one that uses a Vector, etc).
055 *
056 * <p>The PermissionCollection returned by the
057 * <code>Permission.newPermissionCollection</code>
058 * method is a homogeneous collection, which stores only Permission objects
059 * for a given Permission type. A PermissionCollection may also be
060 * heterogeneous. For example, Permissions is a PermissionCollection
061 * subclass that represents a collection of PermissionCollections.
062 * That is, its members are each a homogeneous PermissionCollection.
063 * For example, a Permissions object might have a FilePermissionCollection
064 * for all the FilePermission objects, a SocketPermissionCollection for all the
065 * SocketPermission objects, and so on. Its <code>add</code> method adds a
066 * permission to the appropriate collection.
067 *
068 * <p>Whenever a permission is added to a heterogeneous PermissionCollection
069 * such as Permissions, and the PermissionCollection doesn't yet contain a
070 * PermissionCollection of the specified permission's type, the
071 * PermissionCollection should call
072 * the <code>newPermissionCollection</code> method on the permission's class
073 * to see if it requires a special PermissionCollection. If
074 * <code>newPermissionCollection</code>
075 * returns null, the PermissionCollection
076 * is free to store the permission in any type of PermissionCollection it
077 * desires (one using a Hashtable, one using a Vector, etc.). For example,
078 * the Permissions object uses a default PermissionCollection implementation
079 * that stores the permission objects in a Hashtable.
080 *
081 * <p> Subclass implementations of PermissionCollection should assume
082 * that they may be called simultaneously from multiple threads,
083 * and therefore should be synchronized properly. Furthermore,
084 * Enumerations returned via the <code>elements</code> method are
085 * not <em>fail-fast</em>. Modifications to a collection should not be
086 * performed while enumerating over that collection.
087 *
088 * @see Permission
089 * @see Permissions
090 *
091 * @version 1.44 07/07/30
092 *
093 * @author Roland Schemers
094 */
095
096 public abstract class PermissionCollection implements
097 java.io.Serializable {
098
099 private static final long serialVersionUID = -6727011328946861783L;
100
101 // when set, add will throw an exception.
102 private volatile boolean readOnly;
103
104 /**
105 * Adds a permission object to the current collection of permission objects.
106 *
107 * @param permission the Permission object to add.
108 *
109 * @exception SecurityException - if this PermissionCollection object
110 * has been marked readonly
111 * @exception IllegalArgumentException - if this PermissionCollection
112 * object is a homogeneous collection and the permission
113 * is not of the correct type.
114 */
115 public abstract void add(Permission permission);
116
117 /**
118 * Checks to see if the specified permission is implied by
119 * the collection of Permission objects held in this PermissionCollection.
120 *
121 * @param permission the Permission object to compare.
122 *
123 * @return true if "permission" is implied by the permissions in
124 * the collection, false if not.
125 */
126 public abstract boolean implies(Permission permission);
127
128 /**
129 * Returns an enumeration of all the Permission objects in the collection.
130 *
131 * @return an enumeration of all the Permissions.
132 */
133 public abstract Enumeration<Permission> elements();
134
135 /**
136 * Marks this PermissionCollection object as "readonly". After
137 * a PermissionCollection object
138 * is marked as readonly, no new Permission objects can be added to it
139 * using <code>add</code>.
140 */
141 public void setReadOnly() {
142 readOnly = true;
143 }
144
145 /**
146 * Returns true if this PermissionCollection object is marked as readonly.
147 * If it is readonly, no new Permission objects can be added to it
148 * using <code>add</code>.
149 *
150 * <p>By default, the object is <i>not</i> readonly. It can be set to
151 * readonly by a call to <code>setReadOnly</code>.
152 *
153 * @return true if this PermissionCollection object is marked as readonly,
154 * false otherwise.
155 */
156 public boolean isReadOnly() {
157 return readOnly;
158 }
159
160 /**
161 * Returns a string describing this PermissionCollection object,
162 * providing information about all the permissions it contains.
163 * The format is:
164 * <pre>
165 * super.toString() (
166 * // enumerate all the Permission
167 * // objects and call toString() on them,
168 * // one per line..
169 * )</pre>
170 *
171 * <code>super.toString</code> is a call to the <code>toString</code>
172 * method of this
173 * object's superclass, which is Object. The result is
174 * this PermissionCollection's type name followed by this object's
175 * hashcode, thus enabling clients to differentiate different
176 * PermissionCollections object, even if they contain the same permissions.
177 *
178 * @return information about this PermissionCollection object,
179 * as described above.
180 *
181 */
182 public String toString() {
183 Enumeration<Permission> enum_ = elements();
184 StringBuilder sb = new StringBuilder();
185 sb.append(super .toString() + " (\n");
186 while (enum_.hasMoreElements()) {
187 try {
188 sb.append(" ");
189 sb.append(enum_.nextElement().toString());
190 sb.append("\n");
191 } catch (NoSuchElementException e) {
192 // ignore
193 }
194 }
195 sb.append(")\n");
196 return sb.toString();
197 }
198 }
|