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