jdk/src/share/classes/java/util/PropertyPermission.java
author rriggs
Fri, 30 May 2014 15:46:12 -0400
changeset 24685 215fa91e1b4c
parent 24196 61c9885d76e2
permissions -rw-r--r--
8044461: Cleanup new Boolean and single character strings Reviewed-by: rriggs, alanb, lancea Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21334
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class is for property permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The name is the name of the property ("java.home",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * "os.name", etc). The naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * convention follows the  hierarchical property naming convention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Also, an asterisk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * may appear at the end of the name, following a ".", or by itself, to
14667
6862285acf46 7167056: Clarify that BasicPermission names that contain non-wildcard asterisks are not invalid
mullan
parents: 14014
diff changeset
    51
 * signify a wildcard match. For example: "java.*" and "*" signify a wildcard
6862285acf46 7167056: Clarify that BasicPermission names that contain non-wildcard asterisks are not invalid
mullan
parents: 14014
diff changeset
    52
 * match, while "*java" and "a*b" do not.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The actions to be granted are passed to the constructor in a string containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * a list of one or more comma-separated keywords. The possible keywords are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * "read" and "write". Their meaning is defined as follows:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 14668
diff changeset
    57
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    <DT> read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    <DD> read permission. Allows <code>System.getProperty</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *         be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *    <DT> write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    <DD> write permission. Allows <code>System.setProperty</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * The actions string is converted to lowercase before processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Care should be taken before granting code permission to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * certain system properties.  For example, granting permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * access the "java.home" system property gives potentially malevolent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * code sensitive information about the system environment (the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * installation directory).  Also, granting permission to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * the "user.name" and "user.home" system properties gives potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * malevolent code sensitive information about the user environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * (the user's account name and home directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see java.security.BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see java.lang.SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public final class PropertyPermission extends BasicPermission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Read action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static int READ    = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Write action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final static int WRITE   = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * All actions (read,write);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private final static int ALL     = READ|WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * No actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private final static int NONE    = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The actions string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private String actions; // Left null as long as possible, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            // created and re-used in the getAction function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * initialize a PropertyPermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Also called during de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param mask the actions mask to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   132
    private void init(int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if ((mask & ALL) != mask)
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   134
            throw new IllegalArgumentException("invalid actions mask");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (mask == NONE)
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   137
            throw new IllegalArgumentException("invalid actions mask");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (getName() == null)
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   140
            throw new NullPointerException("name can't be null");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a new PropertyPermission object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The name is the name of the system property, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <i>actions</i> contains a comma-separated list of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * desired actions granted on the property. Possible actions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * "read" and "write".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param name the name of the PropertyPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param actions the actions string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws IllegalArgumentException if <code>name</code> is empty or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>actions</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   159
    public PropertyPermission(String name, String actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        super(name,actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        init(getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Checks if this PropertyPermission object "implies" the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <P>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 14668
diff changeset
   168
     * More specifically, this method returns true if:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <ul>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 14668
diff changeset
   170
     * <li> <i>p</i> is an instanceof PropertyPermission,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <li> <i>p</i>'s actions are a subset of this
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 14668
diff changeset
   172
     * object's actions, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <li> <i>p</i>'s name is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *      name. For example, "java.*" implies "java.home".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (!(p instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        PropertyPermission that = (PropertyPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // we get the effective mask. i.e., the "and" of this and that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // They must be equal to that.mask for implies to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return ((this.mask & that.mask) == that.mask) && super.implies(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Checks two PropertyPermission objects for equality. Checks that <i>obj</i> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * a PropertyPermission, and has the same name and actions as this object.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23010
diff changeset
   196
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return true if obj is a PropertyPermission, and has the same name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * actions as this PropertyPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (! (obj instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        PropertyPermission that = (PropertyPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return (this.mask == that.mask) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            (this.getName().equals(that.getName()));
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 the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * The hash code used is the hash code of this permissions name, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <code>getName().hashCode()</code>, where <code>getName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Converts an actions String to an actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   229
     * @param actions the action string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static int getMask(String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int mask = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   240
        // Use object identity comparison against known-interned strings for
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   241
        // performance benefit (these values are used heavily within the JDK).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (actions == SecurityConstants.PROPERTY_READ_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else if (actions == SecurityConstants.PROPERTY_RW_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return READ|WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        char[] a = actions.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                          (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                          (a[i-1] == 'a' || a[i-1] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                          (a[i] == 'd' || a[i] == 'D'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                matchlen = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                mask |= READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                 (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                 (a[i-2] == 'i' || a[i-2] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                 (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                matchlen = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                mask |= WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    seencomma = true;
11139
db0c2ff5e1ea 7116997: fix warnings in java.util.PropertyPermission
smarks
parents: 5506
diff changeset
   300
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                            "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * read, write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   326
    static String getActions(int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        boolean comma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if ((mask & READ) == READ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            sb.append("read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if ((mask & WRITE) == WRITE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (comma) sb.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            else comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            sb.append("write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns the "canonical string representation" of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * That is, this method always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * read, write. For example, if this PropertyPermission object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * allows both write and read actions, a call to <code>getActions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * will return the string "read,write".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   352
    public String getActions() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Return the current action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Used by the PropertyPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns a new PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * PropertyPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * PropertyPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return new PropertyPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private static final long serialVersionUID = 885438825399942851L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * WriteObject is called to save the state of the PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * readObject is called to restore the state of the PropertyPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        init(getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 * A PropertyPermissionCollection stores a set of PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 * permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
final class PropertyPermissionCollection extends PermissionCollection
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   425
    implements Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Key is property name; value is PropertyPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   432
    private transient Map<String, PropertyPermission> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Boolean saying if "*" is in the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    // No sync access; OK for this to be stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private boolean all_allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   443
     * Create an empty PropertyPermissionCollection object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public PropertyPermissionCollection() {
11139
db0c2ff5e1ea 7116997: fix warnings in java.util.PropertyPermission
smarks
parents: 5506
diff changeset
   446
        perms = new HashMap<>(32);     // Capacity for default policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Adds a permission to the PropertyPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *                                       PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @exception SecurityException - if this PropertyPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *                                object has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   462
    public void add(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (! (permission instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                "attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        PropertyPermission pp = (PropertyPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        String propName = pp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   474
            PropertyPermission existing = perms.get(propName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (existing != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                int oldMask = existing.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                int newMask = pp.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                if (oldMask != newMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    int effective = oldMask | newMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    String actions = PropertyPermission.getActions(effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    perms.put(propName, new PropertyPermission(propName, actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   485
                perms.put(propName, pp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (propName.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   499
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   504
    public boolean implies(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (! (permission instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        PropertyPermission pp = (PropertyPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        PropertyPermission x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int desired = pp.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   517
                x = perms.get("*");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                effective |= x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // name looking for matches on a.b.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        String name = pp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        //System.out.println("check "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   534
            x = perms.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            effective |= x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        offset = name.length()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24196
diff changeset
   549
        while ((last = name.lastIndexOf('.', offset)) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            name = name.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            //System.out.println("check "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   554
                x = perms.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                effective |= x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            offset = last -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Returns an enumeration of all the PropertyPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @return an enumeration of all the PropertyPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   576
    @SuppressWarnings("unchecked")
11139
db0c2ff5e1ea 7116997: fix warnings in java.util.PropertyPermission
smarks
parents: 5506
diff changeset
   577
    public Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Convert Iterator of Map values into an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   580
            /**
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   581
             * Casting to rawtype since Enumeration<PropertyPermission>
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   582
             * cannot be directly cast to Enumeration<Permission>
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   583
             */
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   584
            return (Enumeration)Collections.enumeration(perms.values());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static final long serialVersionUID = 7015263904581634791L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    // Table of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    // @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    // private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *     A table of the PropertyPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @serialField all_allowed boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *     boolean saying if "*" is in the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        new ObjectStreamField("all_allowed", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Writes the contents of the perms field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * serialization compatibility with earlier releases. all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // Copy perms into a Hashtable
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   621
        Hashtable<String, Permission> permissions =
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   622
            new Hashtable<>(perms.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            permissions.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        pfields.put("all_allowed", all_allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Reads in a Hashtable of PropertyPermissions and saves them in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * perms field. Reads in all_allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   638
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   639
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   640
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // Get all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        all_allowed = gfields.get("all_allowed", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        // Get permissions
11139
db0c2ff5e1ea 7116997: fix warnings in java.util.PropertyPermission
smarks
parents: 5506
diff changeset
   650
        @SuppressWarnings("unchecked")
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   651
        Hashtable<String, PropertyPermission> permissions =
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11139
diff changeset
   652
            (Hashtable<String, PropertyPermission>)gfields.get("permissions", null);
11139
db0c2ff5e1ea 7116997: fix warnings in java.util.PropertyPermission
smarks
parents: 5506
diff changeset
   653
        perms = new HashMap<>(permissions.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        perms.putAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
}