jdk/src/share/classes/java/security/BasicPermission.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 9035 1255eb81cc2f
child 14014 da3648e13e67
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The BasicPermission class extends the Permission class, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * can be used as the base class for permissions that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * follow the same naming convention as BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The name for a BasicPermission is the name of the given permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (for example, "exit",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * "setFactory", "print.queueJob", etc). The naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * convention follows the  hierarchical property naming convention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An asterisk may appear by itself, or if immediately preceded by a "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * may appear at the end of the name, to signify a wildcard match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * For example, "*" and "java.*" are valid, while "*java", "a*b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and "java*" are not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The action string (inherited from Permission) is unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Thus, BasicPermission is commonly used as the base class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * "named" permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * (ones that contain a name but no actions list; you either have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * named permission or you don't.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Subclasses may implement actions on top of BasicPermission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see java.lang.SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public abstract class BasicPermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final long serialVersionUID = 6279438298436773498L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // does this permission have a wildcard at the end?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private transient boolean wildcard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // the name without the wildcard on the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private transient String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // is this permission the old-style exitVM permission (pre JDK 1.6)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private transient boolean exitVM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * initialize a BasicPermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private void init(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int len = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException("name can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        char last = name.charAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Is wildcard or ends with ".*"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (last == '*' && (len == 1 || name.charAt(len - 2) == '.')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (len == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                path = name.substring(0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (name.equals("exitVM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                path = "exitVM.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                exitVM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                path = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates a new BasicPermission with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Name is the symbolic name of the permission, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * "setFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * "print.queueJob", or "topLevelWindow", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IllegalArgumentException if <code>name</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public BasicPermission(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Creates a new BasicPermission object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The name is the symbolic name of the BasicPermission, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * actions String is currently unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param actions ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws IllegalArgumentException if <code>name</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public BasicPermission(String name, String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Checks if the specified permission is "implied" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * More specifically, this method returns true if:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <li> <i>p</i>'s class is the same as this object's class, and<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <li> <i>p</i>'s name equals or (in the case of wildcards)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *      is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *      name. For example, "a.b.*" implies "a.b.c".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return true if the passed permission is equal to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * implied by this permission, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if ((p == null) || (p.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        BasicPermission that = (BasicPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (this.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // one wildcard can imply another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return that.path.startsWith(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // make sure ap.path is longer so a.b.* doesn't imply a.b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return (that.path.length() > this.path.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    that.path.startsWith(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // a non-wildcard can't imply a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return this.path.equals(that.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
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 BasicPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Checks that <i>obj</i>'s class is the same as this object's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * and has the same name as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param obj the object we are testing for equality with this object.
485
a66666a5898c 6690169: Specification for BasicPermission.equals() is not consistent
mullan
parents: 2
diff changeset
   206
     * @return true if <i>obj</i>'s class is the same as this object's class
a66666a5898c 6690169: Specification for BasicPermission.equals() is not consistent
mullan
parents: 2
diff changeset
   207
     *  and has the same name as this BasicPermission object, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if ((obj == null) || (obj.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        BasicPermission bp = (BasicPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return getName().equals(bp.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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * The hash code used is the hash code of the name, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>getName().hashCode()</code>, where <code>getName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return a hash code value for this object.
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
     * Returns the canonical string representation of the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * which currently is the empty string "", since there are no actions for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * a BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public String getActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Returns a new PermissionCollection object for storing BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>BasicPermission objects must be stored in a manner that allows them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * to be inserted in any order, but that also enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * PermissionCollection <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * storing BasicPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return new BasicPermissionCollection(this.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * readObject is called to restore the state of the BasicPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // init is called to initialize the rest of the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * All internal invocations of getName should invoke this method, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * that the pre-JDK 1.6 "exitVM" and current "exitVM.*" permission are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * equivalent in equals/hashCode methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    final String getCanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return exitVM ? "exitVM.*" : getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * A BasicPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * of BasicPermission permissions. BasicPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * method in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * A BasicPermissionCollection handles comparing a permission like "a.b.c.d.e"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * with a Permission such as "a.b.*", or "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * @see java.security.PermissionsImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
final class BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static final long serialVersionUID = 739301742472979399L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      * Key is name, value is permission. All permission objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      * collection must be of the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private transient Map<String, Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * This is set to <code>true</code> if this BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private boolean all_allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   335
    private Class<?> permClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Create an empty BasicPermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   342
    public BasicPermissionCollection(Class<?> clazz) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        perms = new HashMap<String, Permission>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        permClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Adds a permission to the BasicPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * permission.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *                                       BasicPermission, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *                                       the permission is not of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *                                       same Class as the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *                                       permissions in this collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @exception SecurityException - if this BasicPermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (! (permission instanceof BasicPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // make sure we only add new BasicPermissions of the same class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // Also check null for compatibility with deserialized form from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // previous versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // adding first permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            permClass = bp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                throw new IllegalArgumentException("invalid permission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                                permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            perms.put(bp.getCanonicalName(), permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // No sync on all_allowed; staleness OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (bp.getCanonicalName().equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (! (permission instanceof BasicPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // random subclasses of BasicPermission do not imply each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (all_allowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // path looking for matches on a.b..*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        String path = bp.getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        Permission x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        offset = path.length()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        while ((last = path.lastIndexOf(".", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            path = path.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            offset = last -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return 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
     * Returns an enumeration of all the BasicPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return an enumeration of all the BasicPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // Convert Iterator of Map values into an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return Collections.enumeration(perms.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    // @serial the Hashtable is indexed by the BasicPermission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *    The BasicPermissions in this BasicPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *    All BasicPermissions in the collection must belong to the same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *    The Hashtable is indexed by the BasicPermission name; the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *    of the Hashtable entry is the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @serialField all_allowed boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *   This is set to <code>true</code> if this BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *   contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @serialField permClass java.lang.Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *   The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *   BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        new ObjectStreamField("all_allowed", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        new ObjectStreamField("permClass", Class.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Writes the contents of the perms field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * serialization compatibility with earlier releases. all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * and permClass unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        Hashtable<String, Permission> permissions =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   517
                new Hashtable<>(perms.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            permissions.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        pfields.put("all_allowed", all_allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        pfields.put("permClass", permClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * BasicPermissionCollection from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // Get permissions
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   544
        // writeObject writes a Hashtable<String, Permission> for the
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   545
        // permissions key, so this cast is safe, unless the data is corrupt.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   546
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        Hashtable<String, Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                (Hashtable<String, Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        perms = new HashMap<String, Permission>(permissions.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        perms.putAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // Get all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        all_allowed = gfields.get("all_allowed", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // Get permClass
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   556
        permClass = (Class<?>) gfields.get("permClass", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // set permClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            Enumeration<Permission> e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                Permission p = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                permClass = p.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
}