Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/AllPermission.java
38829 views
/*1* Copyright (c) 1998, 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.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*54* @serial exclude55*/5657public final class AllPermission extends Permission {5859private static final long serialVersionUID = -2916474571451318075L;6061/**62* Creates a new AllPermission object.63*/64public AllPermission() {65super("<all permissions>");66}676869/**70* Creates a new AllPermission object. This71* constructor exists for use by the {@code Policy} object72* to instantiate new Permission objects.73*74* @param name ignored75* @param actions ignored.76*/77public AllPermission(String name, String actions) {78this();79}8081/**82* Checks if the specified permission is "implied" by83* this object. This method always returns true.84*85* @param p the permission to check against.86*87* @return return88*/89public boolean implies(Permission p) {90return true;91}9293/**94* Checks two AllPermission objects for equality. Two AllPermission95* objects are always equal.96*97* @param obj the object we are testing for equality with this object.98* @return true if <i>obj</i> is an AllPermission, false otherwise.99*/100public boolean equals(Object obj) {101return (obj instanceof AllPermission);102}103104/**105* Returns the hash code value for this object.106*107* @return a hash code value for this object.108*/109110public int hashCode() {111return 1;112}113114/**115* Returns the canonical string representation of the actions.116*117* @return the actions.118*/119public String getActions() {120return "<all actions>";121}122123/**124* Returns a new PermissionCollection object for storing AllPermission125* objects.126* <p>127*128* @return a new PermissionCollection object suitable for129* storing AllPermissions.130*/131public PermissionCollection newPermissionCollection() {132return new AllPermissionCollection();133}134135}136137/**138* A AllPermissionCollection stores a collection139* of AllPermission permissions. AllPermission objects140* must be stored in a manner that allows them to be inserted in any141* order, but enable the implies function to evaluate the implies142* method in an efficient (and consistent) manner.143*144* @see java.security.Permission145* @see java.security.Permissions146*147*148* @author Roland Schemers149*150* @serial include151*/152153final class AllPermissionCollection154extends PermissionCollection155implements java.io.Serializable156{157158// use serialVersionUID from JDK 1.2.2 for interoperability159private static final long serialVersionUID = -4023755556366636806L;160161private boolean all_allowed; // true if any all permissions have been added162163/**164* Create an empty AllPermissions object.165*166*/167168public AllPermissionCollection() {169all_allowed = false;170}171172/**173* Adds a permission to the AllPermissions. The key for the hash is174* permission.path.175*176* @param permission the Permission object to add.177*178* @exception IllegalArgumentException - if the permission is not a179* AllPermission180*181* @exception SecurityException - if this AllPermissionCollection object182* has been marked readonly183*/184185public void add(Permission permission) {186if (! (permission instanceof AllPermission))187throw new IllegalArgumentException("invalid permission: "+188permission);189if (isReadOnly())190throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");191192all_allowed = true; // No sync; staleness OK193}194195/**196* Check and see if this set of permissions implies the permissions197* expressed in "permission".198*199* @param permission the Permission object to compare200*201* @return always returns true.202*/203204public boolean implies(Permission permission) {205return all_allowed; // No sync; staleness OK206}207208/**209* Returns an enumeration of all the AllPermission objects in the210* container.211*212* @return an enumeration of all the AllPermission objects.213*/214public Enumeration<Permission> elements() {215return new Enumeration<Permission>() {216private boolean hasMore = all_allowed;217218public boolean hasMoreElements() {219return hasMore;220}221222public Permission nextElement() {223hasMore = false;224return SecurityConstants.ALL_PERMISSION;225}226};227}228}229230231