Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/Permission.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;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*/6364public abstract class Permission implements Guard, java.io.Serializable {6566private static final long serialVersionUID = -5636570222231596674L;6768private String name;6970/**71* Constructs a permission with the specified name.72*73* @param name name of the Permission object being created.74*75*/7677public Permission(String name) {78this.name = name;79}8081/**82* Implements the guard interface for a permission. The83* {@code SecurityManager.checkPermission} method is called,84* passing this permission object as the permission to check.85* Returns silently if access is granted. Otherwise, throws86* a SecurityException.87*88* @param object the object being guarded (currently ignored).89*90* @throws SecurityException91* if a security manager exists and its92* {@code checkPermission} method doesn't allow access.93*94* @see Guard95* @see GuardedObject96* @see SecurityManager#checkPermission97*98*/99public void checkGuard(Object object) throws SecurityException {100SecurityManager sm = System.getSecurityManager();101if (sm != null) sm.checkPermission(this);102}103104/**105* Checks if the specified permission's actions are "implied by"106* this object's actions.107* <P>108* This must be implemented by subclasses of Permission, as they are the109* only ones that can impose semantics on a Permission object.110*111* <p>The {@code implies} method is used by the AccessController to determine112* whether or not a requested permission is implied by another permission that113* is known to be valid in the current execution context.114*115* @param permission the permission to check against.116*117* @return true if the specified permission is implied by this object,118* false if not.119*/120121public abstract boolean implies(Permission permission);122123/**124* Checks two Permission objects for equality.125* <P>126* Do not use the {@code equals} method for making access control127* decisions; use the {@code implies} method.128*129* @param obj the object we are testing for equality with this object.130*131* @return true if both Permission objects are equivalent.132*/133134public abstract boolean equals(Object obj);135136/**137* Returns the hash code value for this Permission object.138* <P>139* The required {@code hashCode} behavior for Permission Objects is140* the following:141* <ul>142* <li>Whenever it is invoked on the same Permission object more than143* once during an execution of a Java application, the144* {@code hashCode} method145* must consistently return the same integer. This integer need not146* remain consistent from one execution of an application to another147* execution of the same application.148* <li>If two Permission objects are equal according to the149* {@code equals}150* method, then calling the {@code hashCode} method on each of the151* two Permission objects must produce the same integer result.152* </ul>153*154* @return a hash code value for this object.155*/156157public abstract int hashCode();158159/**160* Returns the name of this Permission.161* For example, in the case of a {@code java.io.FilePermission},162* the name will be a pathname.163*164* @return the name of this Permission.165*166*/167168public final String getName() {169return name;170}171172/**173* Returns the actions as a String. This is abstract174* so subclasses can defer creating a String representation until175* one is needed. Subclasses should always return actions in what they176* consider to be their177* canonical form. For example, two FilePermission objects created via178* the following:179*180* <pre>181* perm1 = new FilePermission(p1,"read,write");182* perm2 = new FilePermission(p2,"write,read");183* </pre>184*185* both return186* "read,write" when the {@code getActions} method is invoked.187*188* @return the actions of this Permission.189*190*/191192public abstract String getActions();193194/**195* Returns an empty PermissionCollection for a given Permission object, or null if196* one is not defined. Subclasses of class Permission should197* override this if they need to store their permissions in a particular198* PermissionCollection object in order to provide the correct semantics199* when the {@code PermissionCollection.implies} method is called.200* If null is returned,201* then the caller of this method is free to store permissions of this202* type in any PermissionCollection they choose (one that uses a Hashtable,203* one that uses a Vector, etc).204*205* @return a new PermissionCollection object for this type of Permission, or206* null if one is not defined.207*/208209public PermissionCollection newPermissionCollection() {210return null;211}212213/**214* Returns a string describing this Permission. The convention is to215* specify the class name, the permission name, and the actions in216* the following format: '("ClassName" "name" "actions")', or217* '("ClassName" "name")' if actions list is null or empty.218*219* @return information about this Permission.220*/221public String toString() {222String actions = getActions();223if ((actions == null) || (actions.length() == 0)) { // OPTIONAL224return "(\"" + getClass().getName() + "\" \"" + name + "\")";225} else {226return "(\"" + getClass().getName() + "\" \"" + name +227"\" \"" + actions + "\")";228}229}230}231232233