Path: blob/master/src/java.base/share/classes/java/security/Permission.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;2627/**28* Abstract class for representing access to a system resource.29* All permissions have a name (whose interpretation depends on the subclass),30* as well as abstract functions for defining the semantics of the31* particular Permission subclass.32*33* <p>Most Permission objects also include an "actions" list that tells the actions34* that are permitted for the object. For example,35* for a {@code java.io.FilePermission} object, the permission name is36* the pathname of a file (or directory), and the actions list37* (such as "read, write") specifies which actions are granted for the38* specified file (or for files in the specified directory).39* The actions list is optional for Permission objects, such as40* {@code java.lang.RuntimePermission},41* that don't need such a list; you either have the named permission (such42* as "system.exit") or you don't.43*44* <p>An important method that must be implemented by each subclass is45* the {@code implies} method to compare Permissions. Basically,46* "permission p1 implies permission p2" means that47* if one is granted permission p1, one is naturally granted permission p2.48* Thus, this is not an equality test, but rather more of a49* subset test.50*51* <P> Permission objects are similar to String objects in that they52* are immutable once they have been created. Subclasses should not53* provide methods that can change the state of a permission54* once it has been created.55*56* @see Permissions57* @see PermissionCollection58*59*60* @author Marianne Mueller61* @author Roland Schemers62* @since 1.263*/6465public abstract class Permission implements Guard, java.io.Serializable {6667@java.io.Serial68private static final long serialVersionUID = -5636570222231596674L;6970/**71* The permission name.72*/73private String name;7475/**76* Constructs a permission with the specified name.77*78* @param name name of the Permission object being created.79*80*/8182public Permission(String name) {83this.name = name;84}8586/**87* Implements the guard interface for a permission. The88* {@code SecurityManager.checkPermission} method is called,89* passing this permission object as the permission to check.90* Returns silently if access is granted. Otherwise, throws91* a SecurityException.92*93* @param object the object being guarded (currently ignored).94*95* @throws SecurityException96* if a security manager exists and its97* {@code checkPermission} method doesn't allow access.98*99* @see Guard100* @see GuardedObject101* @see SecurityManager#checkPermission102*103*/104public void checkGuard(Object object) throws SecurityException {105@SuppressWarnings("removal")106SecurityManager sm = System.getSecurityManager();107if (sm != null) sm.checkPermission(this);108}109110/**111* Checks if the specified permission's actions are "implied by"112* this object's actions.113* <P>114* This must be implemented by subclasses of Permission, as they are the115* only ones that can impose semantics on a Permission object.116*117* <p>The {@code implies} method is used by the AccessController to determine118* whether or not a requested permission is implied by another permission that119* is known to be valid in the current execution context.120*121* @param permission the permission to check against.122*123* @return true if the specified permission is implied by this object,124* false if not.125*/126127public abstract boolean implies(Permission permission);128129/**130* Checks two Permission objects for equality.131* <P>132* Do not use the {@code equals} method for making access control133* decisions; use the {@code implies} method.134*135* @param obj the object we are testing for equality with this object.136*137* @return true if both Permission objects are equivalent.138*/139140public abstract boolean equals(Object obj);141142/**143* Returns the hash code value for this Permission object.144* <P>145* The required {@code hashCode} behavior for Permission Objects is146* the following:147* <ul>148* <li>Whenever it is invoked on the same Permission object more than149* once during an execution of a Java application, the150* {@code hashCode} method151* must consistently return the same integer. This integer need not152* remain consistent from one execution of an application to another153* execution of the same application.154* <li>If two Permission objects are equal according to the155* {@code equals}156* method, then calling the {@code hashCode} method on each of the157* two Permission objects must produce the same integer result.158* </ul>159*160* @return a hash code value for this object.161*/162163public abstract int hashCode();164165/**166* Returns the name of this Permission.167* For example, in the case of a {@code java.io.FilePermission},168* the name will be a pathname.169*170* @return the name of this Permission.171*172*/173174public final String getName() {175return name;176}177178/**179* Returns the actions as a String. This is abstract180* so subclasses can defer creating a String representation until181* one is needed. Subclasses should always return actions in what they182* consider to be their183* canonical form. For example, two FilePermission objects created via184* the following:185*186* <pre>187* perm1 = new FilePermission(p1,"read,write");188* perm2 = new FilePermission(p2,"write,read");189* </pre>190*191* both return192* "read,write" when the {@code getActions} method is invoked.193*194* @return the actions of this Permission.195*196*/197198public abstract String getActions();199200/**201* Returns an empty PermissionCollection for a given Permission object, or null if202* one is not defined. Subclasses of class Permission should203* override this if they need to store their permissions in a particular204* PermissionCollection object in order to provide the correct semantics205* when the {@code PermissionCollection.implies} method is called.206* If null is returned,207* then the caller of this method is free to store permissions of this208* type in any PermissionCollection they choose (one that uses a Hashtable,209* one that uses a Vector, etc).210*211* @return a new PermissionCollection object for this type of Permission, or212* null if one is not defined.213*/214215public PermissionCollection newPermissionCollection() {216return null;217}218219/**220* Returns a string describing this Permission. The convention is to221* specify the class name, the permission name, and the actions in222* the following format: '("ClassName" "name" "actions")', or223* '("ClassName" "name")' if actions list is null or empty.224*225* @return information about this Permission.226*/227public String toString() {228String actions = getActions();229if (actions == null || actions.isEmpty()) { // OPTIONAL230return "(\"" + getClass().getName() + "\" \"" + name + "\")";231} else {232return "(\"" + getClass().getName() + "\" \"" + name +233"\" \"" + actions + "\")";234}235}236}237238239