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