jdk/src/share/classes/java/security/BasicPermission.java
author juh
Fri, 28 Jun 2013 10:48:02 -0700
changeset 18579 b678846778ad
parent 14668 a2b9c2c9a823
child 21334 c60dfce46a77
permissions -rw-r--r--
8019360: Cleanup of the javadoc <code> tag in java.security.* Summary: Convert to {@code ...} tags. convert package.html to package-info.java. Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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.
14667
6862285acf46 7167056: Clarify that BasicPermission names that contain non-wildcard asterisks are not invalid
mullan
parents: 14014
diff changeset
    49
 * 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
    50
 * and "java*" do not.
2
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
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
    71
    implements java.io.Serializable
2
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
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
    88
    private void init(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int len = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IllegalArgumentException("name can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        char last = name.charAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Is wildcard or ends with ".*"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (last == '*' && (len == 1 || name.charAt(len - 2) == '.')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (len == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                path = name.substring(0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (name.equals("exitVM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                path = "exitVM.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                exitVM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                path = name;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Creates a new BasicPermission with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Name is the symbolic name of the permission, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * "setFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * "print.queueJob", or "topLevelWindow", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   127
     * @throws NullPointerException if {@code name} is {@code null}.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   128
     * @throws IllegalArgumentException if {@code name} is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   130
    public BasicPermission(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        init(name);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Creates a new BasicPermission object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * The name is the symbolic name of the BasicPermission, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * actions String is currently unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param actions ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   144
     * @throws NullPointerException if {@code name} is {@code null}.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   145
     * @throws IllegalArgumentException if {@code name} is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   147
    public BasicPermission(String name, String actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Checks if the specified permission is "implied" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * More specifically, this method returns true if:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <li> <i>p</i>'s class is the same as this object's class, and<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <li> <i>p</i>'s name equals or (in the case of wildcards)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *      is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *      name. For example, "a.b.*" implies "a.b.c".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return true if the passed permission is equal to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * implied by this permission, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Checks that <i>obj</i>'s class is the same as this object's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * and has the same name as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @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
   201
     * @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
   202
     *  and has the same name as this BasicPermission object, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if ((obj == null) || (obj.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        BasicPermission bp = (BasicPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return getName().equals(bp.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
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
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * 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
   220
     * {@code getName().hashCode()}, where {@code getName} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Returns the canonical string representation of the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * which currently is the empty string "", since there are no actions for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * a BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   236
    public String getActions() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns a new PermissionCollection object for storing BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>BasicPermission objects must be stored in a manner that allows them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * 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
   246
     * PermissionCollection {@code implies} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * storing BasicPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return new BasicPermissionCollection(this.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * readObject is called to restore the state of the BasicPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // init is called to initialize the rest of the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * All internal invocations of getName should invoke this method, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * that the pre-JDK 1.6 "exitVM" and current "exitVM.*" permission are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * equivalent in equals/hashCode methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    final String getCanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return exitVM ? "exitVM.*" : getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * A BasicPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * of BasicPermission permissions. BasicPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * method in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * A BasicPermissionCollection handles comparing a permission like "a.b.c.d.e"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * with a Permission such as "a.b.*", or "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
final class BasicPermissionCollection
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   301
    extends PermissionCollection
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   302
    implements java.io.Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static final long serialVersionUID = 739301742472979399L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
      * Key is name, value is permission. All permission objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
      * collection must be of the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
      * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private transient Map<String, Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   315
     * This is set to {@code true} if this BasicPermissionCollection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private boolean all_allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   328
    private Class<?> permClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Create an empty BasicPermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
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
    public BasicPermissionCollection(Class<?> clazz) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        perms = new HashMap<String, Permission>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        permClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Adds a permission to the BasicPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * permission.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *                                       BasicPermission, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *                                       the permission is not of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *                                       same Class as the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *                                       permissions in this collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @exception SecurityException - if this BasicPermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   356
    public void add(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (! (permission instanceof BasicPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // make sure we only add new BasicPermissions of the same class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // Also check null for compatibility with deserialized form from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // previous versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // adding first permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            permClass = bp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                throw new IllegalArgumentException("invalid permission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                                permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            perms.put(bp.getCanonicalName(), permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // No sync on all_allowed; staleness OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (bp.getCanonicalName().equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   392
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   397
    public boolean implies(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (! (permission instanceof BasicPermission))
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10336
diff changeset
   399
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // random subclasses of BasicPermission do not imply each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (all_allowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // path looking for matches on a.b..*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        String path = bp.getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        Permission x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        offset = path.length()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        while ((last = path.lastIndexOf(".", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            path = path.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // Convert Iterator of Map values into an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return Collections.enumeration(perms.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // @serial the Hashtable is indexed by the BasicPermission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *    The BasicPermissions in this BasicPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *    All BasicPermissions in the collection must belong to the same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *    The Hashtable is indexed by the BasicPermission name; the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *    of the Hashtable entry is the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @serialField all_allowed boolean
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14668
diff changeset
   480
     *   This is set to {@code true} if this BasicPermissionCollection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *   contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @serialField permClass java.lang.Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *   The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *   BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        new ObjectStreamField("all_allowed", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        new ObjectStreamField("permClass", Class.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Writes the contents of the perms field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * serialization compatibility with earlier releases. all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * and permClass unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            permissions.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        pfields.put("all_allowed", all_allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        pfields.put("permClass", permClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * BasicPermissionCollection from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // Get permissions
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   532
        // 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
   533
        // 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
   534
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        Hashtable<String, Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                (Hashtable<String, Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        perms = new HashMap<String, Permission>(permissions.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        perms.putAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        // Get all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        all_allowed = gfields.get("all_allowed", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // Get permClass
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   544
        permClass = (Class<?>) gfields.get("permClass", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // set permClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            Enumeration<Permission> e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                Permission p = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                permClass = p.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
}