jdk/src/share/classes/java/util/PropertyPermission.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 11139 db0c2ff5e1ea
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Check against use of constants (used heavily within the JDK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (actions == SecurityConstants.PROPERTY_READ_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else if (actions == SecurityConstants.PROPERTY_RW_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return READ|WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        char[] a = actions.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                          (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                          (a[i-1] == 'a' || a[i-1] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                          (a[i] == 'd' || a[i] == 'D'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                matchlen = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                mask |= READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                 (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                 (a[i-2] == 'i' || a[i-2] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                 (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                matchlen = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                mask |= WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    seencomma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    /*FALLTHROUGH*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
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
     * Return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * read, write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static String getActions(int mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        boolean comma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if ((mask & READ) == READ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            sb.append("read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if ((mask & WRITE) == WRITE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (comma) sb.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            else comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            sb.append("write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Returns the "canonical string representation" of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * That is, this method always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * read, write. For example, if this PropertyPermission object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * allows both write and read actions, a call to <code>getActions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * will return the string "read,write".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public String getActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return actions;
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
     * Return the current action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Used by the PropertyPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return mask;
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
     * Returns a new PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * PropertyPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * PropertyPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return new PropertyPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private static final long serialVersionUID = 885438825399942851L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * WriteObject is called to save the state of the PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        s.defaultWriteObject();
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
     * readObject is called to restore the state of the PropertyPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        init(getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
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
 * A PropertyPermissionCollection stores a set of PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
final class PropertyPermissionCollection extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Key is property name; value is PropertyPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private transient Map perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Boolean saying if "*" is in the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // No sync access; OK for this to be stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private boolean all_allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Create an empty PropertyPermissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public PropertyPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        perms = new HashMap(32);     // Capacity for default policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Adds a permission to the PropertyPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *                                       PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @exception SecurityException - if this PropertyPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *                                object has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (! (permission instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                "attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        PropertyPermission pp = (PropertyPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        String propName = pp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            PropertyPermission existing = (PropertyPermission) perms.get(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (existing != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                int oldMask = existing.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                int newMask = pp.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                if (oldMask != newMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    int effective = oldMask | newMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    String actions = PropertyPermission.getActions(effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    perms.put(propName, new PropertyPermission(propName, actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                perms.put(propName, permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (propName.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
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
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (! (permission instanceof PropertyPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        PropertyPermission pp = (PropertyPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        PropertyPermission x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        int desired = pp.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                x = (PropertyPermission) perms.get("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (x != null) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // name looking for matches on a.b.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        String name = pp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        //System.out.println("check "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            x = (PropertyPermission) perms.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // we have a direct hit!
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        offset = name.length()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        while ((last = name.lastIndexOf(".", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            name = name.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            //System.out.println("check "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                x = (PropertyPermission) perms.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                effective |= x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            offset = last -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Returns an enumeration of all the PropertyPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return an enumeration of all the PropertyPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public Enumeration elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // Convert Iterator of Map values into an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return Collections.enumeration(perms.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private static final long serialVersionUID = 7015263904581634791L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    // Table of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    // private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *     A table of the PropertyPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @serialField all_allowed boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *     boolean saying if "*" is in the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        new ObjectStreamField("all_allowed", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Writes the contents of the perms field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * serialization compatibility with earlier releases. all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        Hashtable permissions = new Hashtable(perms.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            permissions.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        pfields.put("all_allowed", all_allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Reads in a Hashtable of PropertyPermissions and saves them in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * perms field. Reads in all_allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // Get all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        all_allowed = gfields.get("all_allowed", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // Get permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        Hashtable permissions = (Hashtable)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        perms = new HashMap(permissions.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        perms.putAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
}