Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/PermissionCollection.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.util.*;2829/**30* Abstract class representing a collection of Permission objects.31*32* <p>With a PermissionCollection, you can:33* <UL>34* <LI> add a permission to the collection using the {@code add} method.35* <LI> check to see if a particular permission is implied in the36* collection, using the {@code implies} method.37* <LI> enumerate all the permissions, using the {@code elements} method.38* </UL>39*40* <p>When it is desirable to group together a number of Permission objects41* of the same type, the {@code newPermissionCollection} method on that42* particular type of Permission object should first be called. The default43* behavior (from the Permission class) is to simply return null.44* Subclasses of class Permission override the method if they need to store45* their permissions in a particular PermissionCollection object in order46* to provide the correct semantics when the47* {@code PermissionCollection.implies} method is called.48* If a non-null value is returned, that PermissionCollection must be used.49* If null is returned, then the caller of {@code newPermissionCollection}50* is free to store permissions of the51* given type in any PermissionCollection they choose52* (one that uses a Hashtable, one that uses a Vector, etc).53*54* <p>The PermissionCollection returned by the55* {@code Permission.newPermissionCollection}56* method is a homogeneous collection, which stores only Permission objects57* for a given Permission type. A PermissionCollection may also be58* heterogeneous. For example, Permissions is a PermissionCollection59* subclass that represents a collection of PermissionCollections.60* That is, its members are each a homogeneous PermissionCollection.61* For example, a Permissions object might have a FilePermissionCollection62* for all the FilePermission objects, a SocketPermissionCollection for all the63* SocketPermission objects, and so on. Its {@code add} method adds a64* permission to the appropriate collection.65*66* <p>Whenever a permission is added to a heterogeneous PermissionCollection67* such as Permissions, and the PermissionCollection doesn't yet contain a68* PermissionCollection of the specified permission's type, the69* PermissionCollection should call70* the {@code newPermissionCollection} method on the permission's class71* to see if it requires a special PermissionCollection. If72* {@code newPermissionCollection}73* returns null, the PermissionCollection74* is free to store the permission in any type of PermissionCollection it75* desires (one using a Hashtable, one using a Vector, etc.). For example,76* the Permissions object uses a default PermissionCollection implementation77* that stores the permission objects in a Hashtable.78*79* <p> Subclass implementations of PermissionCollection should assume80* that they may be called simultaneously from multiple threads,81* and therefore should be synchronized properly. Furthermore,82* Enumerations returned via the {@code elements} method are83* not <em>fail-fast</em>. Modifications to a collection should not be84* performed while enumerating over that collection.85*86* @see Permission87* @see Permissions88*89*90* @author Roland Schemers91*/9293public abstract class PermissionCollection implements java.io.Serializable {9495private static final long serialVersionUID = -6727011328946861783L;9697// when set, add will throw an exception.98private volatile boolean readOnly;99100/**101* Adds a permission object to the current collection of permission objects.102*103* @param permission the Permission object to add.104*105* @exception SecurityException - if this PermissionCollection object106* has been marked readonly107* @exception IllegalArgumentException - if this PermissionCollection108* object is a homogeneous collection and the permission109* is not of the correct type.110*/111public abstract void add(Permission permission);112113/**114* Checks to see if the specified permission is implied by115* the collection of Permission objects held in this PermissionCollection.116*117* @param permission the Permission object to compare.118*119* @return true if "permission" is implied by the permissions in120* the collection, false if not.121*/122public abstract boolean implies(Permission permission);123124/**125* Returns an enumeration of all the Permission objects in the collection.126*127* @return an enumeration of all the Permissions.128*/129public abstract Enumeration<Permission> elements();130131/**132* Marks this PermissionCollection object as "readonly". After133* a PermissionCollection object134* is marked as readonly, no new Permission objects can be added to it135* using {@code add}.136*/137public void setReadOnly() {138readOnly = true;139}140141/**142* Returns true if this PermissionCollection object is marked as readonly.143* If it is readonly, no new Permission objects can be added to it144* using {@code add}.145*146* <p>By default, the object is <i>not</i> readonly. It can be set to147* readonly by a call to {@code setReadOnly}.148*149* @return true if this PermissionCollection object is marked as readonly,150* false otherwise.151*/152public boolean isReadOnly() {153return readOnly;154}155156/**157* Returns a string describing this PermissionCollection object,158* providing information about all the permissions it contains.159* The format is:160* <pre>161* super.toString() (162* // enumerate all the Permission163* // objects and call toString() on them,164* // one per line..165* )</pre>166*167* {@code super.toString} is a call to the {@code toString}168* method of this169* object's superclass, which is Object. The result is170* this PermissionCollection's type name followed by this object's171* hashcode, thus enabling clients to differentiate different172* PermissionCollections object, even if they contain the same permissions.173*174* @return information about this PermissionCollection object,175* as described above.176*177*/178public String toString() {179Enumeration<Permission> enum_ = elements();180StringBuilder sb = new StringBuilder();181sb.append(super.toString()+" (\n");182while (enum_.hasMoreElements()) {183try {184sb.append(" ");185sb.append(enum_.nextElement().toString());186sb.append("\n");187} catch (NoSuchElementException e){188// ignore189}190}191sb.append(")\n");192return sb.toString();193}194}195196197