Path: blob/master/src/java.base/share/classes/java/security/AllPermission.java
41152 views
/*1* Copyright (c) 1998, 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.security.*;28import java.util.Enumeration;29import java.util.Hashtable;30import java.util.StringTokenizer;31import sun.security.util.SecurityConstants;3233/**34* The AllPermission is a permission that implies all other permissions.35* <p>36* <b>Note:</b> Granting AllPermission should be done with extreme care,37* as it implies all other permissions. Thus, it grants code the ability38* to run with security39* disabled. Extreme caution should be taken before granting such40* a permission to code. This permission should be used only during testing,41* or in extremely rare cases where an application or applet is42* completely trusted and adding the necessary permissions to the policy43* is prohibitively cumbersome.44*45* @see java.security.Permission46* @see java.security.AccessController47* @see java.security.Permissions48* @see java.security.PermissionCollection49* @see java.lang.SecurityManager50*51*52* @author Roland Schemers53* @since 1.254*55* @serial exclude56*/5758public final class AllPermission extends Permission {5960@java.io.Serial61private static final long serialVersionUID = -2916474571451318075L;6263/**64* Creates a new AllPermission object.65*/66public AllPermission() {67super("<all permissions>");68}697071/**72* Creates a new AllPermission object. This73* constructor exists for use by the {@code Policy} object74* to instantiate new Permission objects.75*76* @param name ignored77* @param actions ignored.78*/79public AllPermission(String name, String actions) {80this();81}8283/**84* Checks if the specified permission is "implied" by85* this object. This method always returns true.86*87* @param p the permission to check against.88*89* @return return90*/91public boolean implies(Permission p) {92return true;93}9495/**96* Checks two AllPermission objects for equality. Two AllPermission97* objects are always equal.98*99* @param obj the object we are testing for equality with this object.100* @return true if {@code obj} is an AllPermission, false otherwise.101*/102public boolean equals(Object obj) {103return (obj instanceof AllPermission);104}105106/**107* Returns the hash code value for this object.108*109* @return a hash code value for this object.110*/111112public int hashCode() {113return 1;114}115116/**117* Returns the canonical string representation of the actions.118*119* @return the actions.120*/121public String getActions() {122return "<all actions>";123}124125/**126* Returns a new PermissionCollection object for storing AllPermission127* objects.128*129* @return a new PermissionCollection object suitable for130* storing AllPermissions.131*/132public PermissionCollection newPermissionCollection() {133return new AllPermissionCollection();134}135136}137138/**139* A AllPermissionCollection stores a collection140* of AllPermission permissions. AllPermission objects141* must be stored in a manner that allows them to be inserted in any142* order, but enable the implies function to evaluate the implies143* method in an efficient (and consistent) manner.144*145* @see java.security.Permission146* @see java.security.Permissions147*148*149* @author Roland Schemers150*151* @serial include152*/153154final class AllPermissionCollection155extends PermissionCollection156implements java.io.Serializable157{158159// use serialVersionUID from JDK 1.2.2 for interoperability160@java.io.Serial161private static final long serialVersionUID = -4023755556366636806L;162163/**164* True if any AllPermissions have been added.165*/166private boolean all_allowed;167168/**169* Create an empty AllPermissions object.170*171*/172173public AllPermissionCollection() {174all_allowed = false;175}176177/**178* Adds a permission to the AllPermissions. The key for the hash is179* permission.path.180*181* @param permission the Permission object to add.182*183* @throws IllegalArgumentException if the permission is not an184* AllPermission185*186* @throws SecurityException if this AllPermissionCollection object187* has been marked readonly188*/189190public void add(Permission permission) {191if (! (permission instanceof AllPermission))192throw new IllegalArgumentException("invalid permission: "+193permission);194if (isReadOnly())195throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");196197all_allowed = true; // No sync; staleness OK198}199200/**201* Check and see if this set of permissions implies the permissions202* expressed in "permission".203*204* @param permission the Permission object to compare205*206* @return always returns true.207*/208209public boolean implies(Permission permission) {210return all_allowed; // No sync; staleness OK211}212213/**214* Returns an enumeration of all the AllPermission objects in the215* container.216*217* @return an enumeration of all the AllPermission objects.218*/219public Enumeration<Permission> elements() {220return new Enumeration<>() {221private boolean hasMore = all_allowed;222223public boolean hasMoreElements() {224return hasMore;225}226227public Permission nextElement() {228hasMore = false;229return SecurityConstants.ALL_PERMISSION;230}231};232}233}234235236