0N/A/*
2362N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/A/**
0N/A * Abstract class for representing access to a system resource.
0N/A * All permissions have a name (whose interpretation depends on the subclass),
0N/A * as well as abstract functions for defining the semantics of the
0N/A * particular Permission subclass.
0N/A *
0N/A * <p>Most Permission objects also include an "actions" list that tells the actions
0N/A * that are permitted for the object. For example,
0N/A * for a <code>java.io.FilePermission</code> object, the permission name is
0N/A * the pathname of a file (or directory), and the actions list
0N/A * (such as "read, write") specifies which actions are granted for the
0N/A * specified file (or for files in the specified directory).
0N/A * The actions list is optional for Permission objects, such as
0N/A * <code>java.lang.RuntimePermission</code>,
0N/A * that don't need such a list; you either have the named permission (such
0N/A * as "system.exit") or you don't.
0N/A *
0N/A * <p>An important method that must be implemented by each subclass is
0N/A * the <code>implies</code> method to compare Permissions. Basically,
0N/A * "permission p1 implies permission p2" means that
0N/A * if one is granted permission p1, one is naturally granted permission p2.
0N/A * Thus, this is not an equality test, but rather more of a
0N/A * subset test.
0N/A *
0N/A * <P> Permission objects are similar to String objects in that they
0N/A * are immutable once they have been created. Subclasses should not
0N/A * provide methods that can change the state of a permission
0N/A * once it has been created.
0N/A *
0N/A * @see Permissions
0N/A * @see PermissionCollection
0N/A *
0N/A *
0N/A * @author Marianne Mueller
0N/A * @author Roland Schemers
0N/A */
0N/A
0N/Apublic abstract class Permission implements Guard, java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = -5636570222231596674L;
0N/A
0N/A private String name;
0N/A
0N/A /**
0N/A * Constructs a permission with the specified name.
0N/A *
0N/A * @param name name of the Permission object being created.
0N/A *
0N/A */
0N/A
0N/A public Permission(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A /**
0N/A * Implements the guard interface for a permission. The
0N/A * <code>SecurityManager.checkPermission</code> method is called,
0N/A * passing this permission object as the permission to check.
0N/A * Returns silently if access is granted. Otherwise, throws
0N/A * a SecurityException.
0N/A *
0N/A * @param object the object being guarded (currently ignored).
0N/A *
0N/A * @throws SecurityException
0N/A * if a security manager exists and its
0N/A * <code>checkPermission</code> method doesn't allow access.
0N/A *
0N/A * @see Guard
0N/A * @see GuardedObject
0N/A * @see SecurityManager#checkPermission
0N/A *
0N/A */
0N/A public void checkGuard(Object object) throws SecurityException {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) sm.checkPermission(this);
0N/A }
0N/A
0N/A /**
0N/A * Checks if the specified permission's actions are "implied by"
0N/A * this object's actions.
0N/A * <P>
0N/A * This must be implemented by subclasses of Permission, as they are the
0N/A * only ones that can impose semantics on a Permission object.
0N/A *
0N/A * <p>The <code>implies</code> method is used by the AccessController to determine
0N/A * whether or not a requested permission is implied by another permission that
0N/A * is known to be valid in the current execution context.
0N/A *
0N/A * @param permission the permission to check against.
0N/A *
0N/A * @return true if the specified permission is implied by this object,
0N/A * false if not.
0N/A */
0N/A
0N/A public abstract boolean implies(Permission permission);
0N/A
0N/A /**
0N/A * Checks two Permission objects for equality.
0N/A * <P>
0N/A * Do not use the <code>equals</code> method for making access control
0N/A * decisions; use the <code>implies</code> method.
0N/A *
0N/A * @param obj the object we are testing for equality with this object.
0N/A *
0N/A * @return true if both Permission objects are equivalent.
0N/A */
0N/A
0N/A public abstract boolean equals(Object obj);
0N/A
0N/A /**
0N/A * Returns the hash code value for this Permission object.
0N/A * <P>
0N/A * The required <code>hashCode</code> behavior for Permission Objects is
0N/A * the following: <p>
0N/A * <ul>
0N/A * <li>Whenever it is invoked on the same Permission object more than
0N/A * once during an execution of a Java application, the
0N/A * <code>hashCode</code> method
0N/A * must consistently return the same integer. This integer need not
0N/A * remain consistent from one execution of an application to another
0N/A * execution of the same application. <p>
0N/A * <li>If two Permission objects are equal according to the
0N/A * <code>equals</code>
0N/A * method, then calling the <code>hashCode</code> method on each of the
0N/A * two Permission objects must produce the same integer result.
0N/A * </ul>
0N/A *
0N/A * @return a hash code value for this object.
0N/A */
0N/A
0N/A public abstract int hashCode();
0N/A
0N/A /**
0N/A * Returns the name of this Permission.
0N/A * For example, in the case of a <code>java.io.FilePermission</code>,
0N/A * the name will be a pathname.
0N/A *
0N/A * @return the name of this Permission.
0N/A *
0N/A */
0N/A
0N/A public final String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns the actions as a String. This is abstract
0N/A * so subclasses can defer creating a String representation until
0N/A * one is needed. Subclasses should always return actions in what they
0N/A * consider to be their
0N/A * canonical form. For example, two FilePermission objects created via
0N/A * the following:
0N/A *
0N/A * <pre>
0N/A * perm1 = new FilePermission(p1,"read,write");
0N/A * perm2 = new FilePermission(p2,"write,read");
0N/A * </pre>
0N/A *
0N/A * both return
0N/A * "read,write" when the <code>getActions</code> method is invoked.
0N/A *
0N/A * @return the actions of this Permission.
0N/A *
0N/A */
0N/A
0N/A public abstract String getActions();
0N/A
0N/A /**
0N/A * Returns an empty PermissionCollection for a given Permission object, or null if
0N/A * one is not defined. Subclasses of class Permission should
0N/A * override this if they need to store their permissions in a particular
0N/A * PermissionCollection object in order to provide the correct semantics
0N/A * when the <code>PermissionCollection.implies</code> method is called.
0N/A * If null is returned,
0N/A * then the caller of this method is free to store permissions of this
0N/A * type in any PermissionCollection they choose (one that uses a Hashtable,
0N/A * one that uses a Vector, etc).
0N/A *
0N/A * @return a new PermissionCollection object for this type of Permission, or
0N/A * null if one is not defined.
0N/A */
0N/A
0N/A public PermissionCollection newPermissionCollection() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing this Permission. The convention is to
0N/A * specify the class name, the permission name, and the actions in
928N/A * the following format: '("ClassName" "name" "actions")', or
928N/A * '("ClassName" "name")' if actions list is null or empty.
0N/A *
0N/A * @return information about this Permission.
0N/A */
0N/A public String toString() {
0N/A String actions = getActions();
0N/A if ((actions == null) || (actions.length() == 0)) { // OPTIONAL
928N/A return "(\"" + getClass().getName() + "\" \"" + name + "\")";
0N/A } else {
928N/A return "(\"" + getClass().getName() + "\" \"" + name +
928N/A "\" \"" + actions + "\")";
0N/A }
0N/A }
0N/A}