Path: blob/master/src/java.base/share/classes/java/security/PermissionCollection.java
41152 views
/*1* Copyright (c) 1997, 2021, 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.*;28import java.util.stream.Stream;29import java.util.stream.StreamSupport;3031/**32* Abstract class representing a collection of Permission objects.33*34* <p>With a PermissionCollection, you can:35* <UL>36* <LI> add a permission to the collection using the {@code add} method.37* <LI> check to see if a particular permission is implied in the38* collection, using the {@code implies} method.39* <LI> enumerate all the permissions, using the {@code elements} method.40* </UL>41*42* <p>When it is desirable to group together a number of Permission objects43* of the same type, the {@code newPermissionCollection} method on that44* particular type of Permission object should first be called. The default45* behavior (from the Permission class) is to simply return null.46* Subclasses of class Permission override the method if they need to store47* their permissions in a particular PermissionCollection object in order48* to provide the correct semantics when the49* {@code PermissionCollection.implies} method is called.50* If a non-null value is returned, that PermissionCollection must be used.51* If null is returned, then the caller of {@code newPermissionCollection}52* is free to store permissions of the53* given type in any PermissionCollection they choose54* (one that uses a Hashtable, one that uses a Vector, etc).55*56* <p>The PermissionCollection returned by the57* {@code Permission.newPermissionCollection}58* method is a homogeneous collection, which stores only Permission objects59* for a given Permission type. A PermissionCollection may also be60* heterogeneous. For example, Permissions is a PermissionCollection61* subclass that represents a collection of PermissionCollections.62* That is, its members are each a homogeneous PermissionCollection.63* For example, a Permissions object might have a FilePermissionCollection64* for all the FilePermission objects, a SocketPermissionCollection for all the65* SocketPermission objects, and so on. Its {@code add} method adds a66* permission to the appropriate collection.67*68* <p>Whenever a permission is added to a heterogeneous PermissionCollection69* such as Permissions, and the PermissionCollection doesn't yet contain a70* PermissionCollection of the specified permission's type, the71* PermissionCollection should call72* the {@code newPermissionCollection} method on the permission's class73* to see if it requires a special PermissionCollection. If74* {@code newPermissionCollection}75* returns null, the PermissionCollection76* is free to store the permission in any type of PermissionCollection it77* desires (one using a Hashtable, one using a Vector, etc.). For example,78* the Permissions object uses a default PermissionCollection implementation79* that stores the permission objects in a Hashtable.80*81* <p> Subclass implementations of PermissionCollection should assume82* that they may be called simultaneously from multiple threads,83* and therefore should be synchronized properly. Furthermore,84* Enumerations returned via the {@code elements} method are85* not <em>fail-fast</em>. Modifications to a collection should not be86* performed while enumerating over that collection.87*88* @see Permission89* @see Permissions90*91*92* @author Roland Schemers93* @since 1.294*/9596public abstract class PermissionCollection implements java.io.Serializable {9798@java.io.Serial99private static final long serialVersionUID = -6727011328946861783L;100101/**102* Whether this permission collection is read-only.103* <p>104* If set, the {@code add} method will throw an exception.105*/106private volatile boolean readOnly;107108/**109* Constructor for subclasses to call.110*/111public PermissionCollection() {}112113/**114* Adds a permission object to the current collection of permission objects.115*116* @param permission the Permission object to add.117*118* @throws SecurityException if this PermissionCollection object119* has been marked readonly120* @throws IllegalArgumentException if this PermissionCollection121* object is a homogeneous collection and the permission122* is not of the correct type.123*/124public abstract void add(Permission permission);125126/**127* Checks to see if the specified permission is implied by128* the collection of Permission objects held in this PermissionCollection.129*130* @param permission the Permission object to compare.131*132* @return true if "permission" is implied by the permissions in133* the collection, false if not.134*/135public abstract boolean implies(Permission permission);136137/**138* Returns an enumeration of all the Permission objects in the collection.139*140* @return an enumeration of all the Permissions.141* @see #elementsAsStream()142*/143public abstract Enumeration<Permission> elements();144145/**146* Returns a stream of all the Permission objects in the collection.147*148* <p> The collection should not be modified (see {@link #add}) during the149* execution of the terminal stream operation. Otherwise, the result of the150* terminal stream operation is undefined.151*152* @implSpec153* The default implementation creates a stream whose source is derived from154* the enumeration returned from a call to {@link #elements()}.155*156* @return a stream of all the Permissions.157* @since 9158*/159public Stream<Permission> elementsAsStream() {160int characteristics = isReadOnly()161? Spliterator.NONNULL | Spliterator.IMMUTABLE162: Spliterator.NONNULL;163return StreamSupport.stream(164Spliterators.spliteratorUnknownSize(165elements().asIterator(), characteristics),166false);167}168169/**170* Marks this PermissionCollection object as "readonly". After171* a PermissionCollection object172* is marked as readonly, no new Permission objects can be added to it173* using {@code add}.174*/175public void setReadOnly() {176readOnly = true;177}178179/**180* Returns true if this PermissionCollection object is marked as readonly.181* If it is readonly, no new Permission objects can be added to it182* using {@code add}.183*184* <p>By default, the object is <i>not</i> readonly. It can be set to185* readonly by a call to {@code setReadOnly}.186*187* @return true if this PermissionCollection object is marked as readonly,188* false otherwise.189*/190public boolean isReadOnly() {191return readOnly;192}193194/**195* Returns a string describing this PermissionCollection object,196* providing information about all the permissions it contains.197* The format is:198* <pre>199* super.toString() (200* // enumerate all the Permission201* // objects and call toString() on them,202* // one per line..203* )</pre>204*205* {@code super.toString} is a call to the {@code toString}206* method of this207* object's superclass, which is Object. The result is208* this PermissionCollection's type name followed by this object's209* hashcode, thus enabling clients to differentiate different210* PermissionCollections object, even if they contain the same permissions.211*212* @return information about this PermissionCollection object,213* as described above.214*215*/216public String toString() {217Enumeration<Permission> enum_ = elements();218StringBuilder sb = new StringBuilder();219sb.append(super.toString()+" (\n");220while (enum_.hasMoreElements()) {221try {222sb.append(" ");223sb.append(enum_.nextElement().toString());224sb.append("\n");225} catch (NoSuchElementException e){226// ignore227}228}229sb.append(")\n");230return sb.toString();231}232}233234235