jdk/src/share/classes/java/security/Permission.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 18579 b678846778ad
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
     2
 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2172
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Abstract class for representing access to a system resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * All permissions have a name (whose interpretation depends on the subclass),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * as well as abstract functions for defining the semantics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * particular Permission subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>Most Permission objects also include an "actions" list that tells the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that are permitted for the object.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for a <code>java.io.FilePermission</code> object, the permission name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the pathname of a file (or directory), and the actions list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (such as "read, write") specifies which actions are granted for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * specified file (or for files in the specified directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The actions list is optional for Permission objects, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>java.lang.RuntimePermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that don't need such a list; you either have the named permission (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * as "system.exit") or you don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>An important method that must be implemented by each subclass is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the <code>implies</code> method to compare Permissions. Basically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * "permission p1 implies permission p2" means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * if one is granted permission p1, one is naturally granted permission p2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Thus, this is not an equality test, but rather more of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * subset test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <P> Permission objects are similar to String objects in that they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * are immutable once they have been created. Subclasses should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * provide methods that can change the state of a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * once it has been created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public abstract class Permission implements Guard, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final long serialVersionUID = -5636570222231596674L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Constructs a permission with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param name name of the Permission object being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public Permission(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Implements the guard interface for a permission. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>SecurityManager.checkPermission</code> method is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * passing this permission object as the permission to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns silently if access is granted. Otherwise, throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param object the object being guarded (currently ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *        <code>checkPermission</code> method doesn't allow access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @see Guard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see GuardedObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void checkGuard(Object object) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (sm != null) sm.checkPermission(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Checks if the specified permission's actions are "implied by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * this object's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This must be implemented by subclasses of Permission, as they are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * only ones that can impose semantics on a Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>The <code>implies</code> method is used by the AccessController to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * whether or not a requested permission is implied by another permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * is known to be valid in the current execution context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param permission the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public abstract boolean implies(Permission permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Checks two Permission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Do not use the <code>equals</code> method for making access control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * decisions; use the <code>implies</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return true if both Permission objects are equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public abstract boolean equals(Object obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns the hash code value for this Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The required <code>hashCode</code> behavior for Permission Objects is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the following: <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <li>Whenever it is invoked on the same Permission object more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *     once during an execution of a Java application, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *     <code>hashCode</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *     must consistently return the same integer. This integer need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *     remain consistent from one execution of an application to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *     execution of the same application. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <li>If two Permission objects are equal according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *     <code>equals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *     method, then calling the <code>hashCode</code> method on each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *     two Permission objects must produce the same integer result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public abstract int hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns the name of this Permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * For example, in the case of a <code>java.io.FilePermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the name will be a pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return the name of this Permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public final String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns the actions as a String. This is abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * so subclasses can defer creating a String representation until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * one is needed. Subclasses should always return actions in what they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * consider to be their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * canonical form. For example, two FilePermission objects created via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *   perm1 = new FilePermission(p1,"read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *   perm2 = new FilePermission(p2,"write,read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * both return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * "read,write" when the <code>getActions</code> method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return the actions of this Permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public abstract String getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns an empty PermissionCollection for a given Permission object, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * one is not defined. Subclasses of class Permission should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * override this if they need to store their permissions in a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * PermissionCollection object in order to provide the correct semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * when the <code>PermissionCollection.implies</code> method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If null is returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * then the caller of this method is free to store permissions of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * type in any PermissionCollection they choose (one that uses a Hashtable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * one that uses a Vector, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return a new PermissionCollection object for this type of Permission, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * null if one is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns a string describing this Permission.  The convention is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * specify the class name, the permission name, and the actions in
2172
2e7377d894db 6549506: Specification of Permission.toString() method contradicts with JDK implementation
xuelei
parents: 2
diff changeset
   217
     * the following format: '("ClassName" "name" "actions")', or
2e7377d894db 6549506: Specification of Permission.toString() method contradicts with JDK implementation
xuelei
parents: 2
diff changeset
   218
     * '("ClassName" "name")' if actions list is null or empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return information about this Permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String actions = getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if ((actions == null) || (actions.length() == 0)) { // OPTIONAL
2172
2e7377d894db 6549506: Specification of Permission.toString() method contradicts with JDK implementation
xuelei
parents: 2
diff changeset
   225
            return "(\"" + getClass().getName() + "\" \"" + name + "\")";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
2172
2e7377d894db 6549506: Specification of Permission.toString() method contradicts with JDK implementation
xuelei
parents: 2
diff changeset
   227
            return "(\"" + getClass().getName() + "\" \"" + name +
2e7377d894db 6549506: Specification of Permission.toString() method contradicts with JDK implementation
xuelei
parents: 2
diff changeset
   228
                 "\" \"" + actions + "\")";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}