jdk/src/java.base/share/classes/java/security/Permissions.java
author coleenp
Wed, 30 Aug 2017 19:18:22 -0400
changeset 47098 e704f55561c3
parent 45434 4582657c7260
permissions -rw-r--r--
8164207: Checking missing load-acquire in relation to _pd_set in dictionary.cpp Summary: Use load_acquire for accessing DictionaryEntry::_pd_set since it's accessed outside the SystemDictionary_lock Reviewed-by: zgu, twisti, dholmes, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.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.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Collections;
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class represents a heterogeneous collection of Permissions. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * it contains different types of Permission objects, organized into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * PermissionCollections. For example, if any
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    48
 * {@code java.io.FilePermission} objects are added to an instance of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * this class, they are all stored in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * PermissionCollection. It is the PermissionCollection returned by a call to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    51
 * the {@code newPermissionCollection} method in the FilePermission class.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    52
 * Similarly, any {@code java.lang.RuntimePermission} objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * stored in the PermissionCollection returned by a call to the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    54
 * {@code newPermissionCollection} method in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * RuntimePermission class. Thus, this class represents a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * PermissionCollections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    58
 * <p>When the {@code add} method is called to add a Permission, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Permission is stored in the appropriate PermissionCollection. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * collection exists yet, the Permission object's class is determined and the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    61
 * {@code newPermissionCollection} method is called on that class to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the PermissionCollection and add it to the Permissions object. If
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    63
 * {@code newPermissionCollection} returns null, then a default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * PermissionCollection that uses a hashtable will be created and used. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * hashtable entry stores a Permission object as both the key and the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
    67
 * <p> Enumerations returned via the {@code elements} method are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * not <em>fail-fast</em>.  Modifications to a collection should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * performed while enumerating over that collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @see PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see AllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author Roland Schemers
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 31538
diff changeset
    78
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public final class Permissions extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Key is permissions Class, value is PermissionCollection for that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
    90
    private transient ConcurrentHashMap<Class<?>, PermissionCollection> permsMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // optimization. keep track of whether unresolved permissions need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private transient boolean hasUnresolved = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // optimization. keep track of the AllPermission collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // - package private for ProtectionDomain optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    PermissionCollection allPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates a new Permissions object containing no PermissionCollections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Permissions() {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   104
        permsMap = new ConcurrentHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        allPermission = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Adds a permission object to the PermissionCollection for the class the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * permission belongs to. For example, if <i>permission</i> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * FilePermission, it is added to the FilePermissionCollection stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * in this Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * This method creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * a new PermissionCollection object (and adds the permission to it)
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   116
     * if an appropriate collection does not yet exist.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @exception SecurityException if this Permissions object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * marked as readonly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see PermissionCollection#isReadOnly()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   125
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
              "attempt to add a Permission to a readonly Permissions object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   131
        PermissionCollection pc = getPermissionCollection(permission, true);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   132
        pc.add(permission);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // No sync; staleness -> optimizations delayed, which is OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (permission instanceof AllPermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            allPermission = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (permission instanceof UnresolvedPermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            hasUnresolved = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Checks to see if this object's PermissionCollection for permissions of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the specified permission's class implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * expressed in the <i>permission</i> object. Returns true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * combination of permissions in the appropriate PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * (e.g., a FilePermissionCollection for a FilePermission) together
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * imply the specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p>For example, suppose there is a FilePermissionCollection in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Permissions object, and it contains one FilePermission that specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * "read" access for  all files in all subdirectories of the "/tmp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * directory, and another FilePermission that specifies "write" access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * for all files in the "/tmp/scratch/foo" directory.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
   156
     * Then if the {@code implies} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * is called with a permission specifying both "read" and "write" access
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
   158
     * to files in the "/tmp/scratch/foo" directory, {@code true} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>Additionally, if this PermissionCollection contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * AllPermission, this method will always return true.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   163
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param permission the Permission object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return true if "permission" is implied by the permissions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * PermissionCollection it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * belongs to, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   170
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // No sync; staleness -> skip optimization, which is OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return true; // AllPermission has already been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   176
            PermissionCollection pc = getPermissionCollection(permission,
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   177
                false);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   178
            if (pc != null) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   179
                return pc.implies(permission);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   180
            } else {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   181
                // none found
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   182
                return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns an enumeration of all the Permission objects in all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * PermissionCollections in this Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return an enumeration of all the Permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   193
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // go through each Permissions in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // and call their elements() function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   198
        return new PermissionsEnumerator(permsMap.values().iterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Gets the PermissionCollection in this Permissions object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * permissions whose type is the same as that of <i>p</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * For example, if <i>p</i> is a FilePermission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * the FilePermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * stored in this Permissions object will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * If createEmpty is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * this method creates a new PermissionCollection object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * type of permission objects if one does not yet exist.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
   211
     * To do so, it first calls the {@code newPermissionCollection} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * on <i>p</i>.  Subclasses of class Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * override that method if they need to store their permissions in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * particular PermissionCollection object in order to provide the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 10336
diff changeset
   215
     * correct semantics when the {@code PermissionCollection.implies}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * If the call returns a PermissionCollection, that collection is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * in this Permissions object. If the call returns null and createEmpty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * is true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * this method instantiates and stores a default PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * that uses a hashtable to store its permission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * createEmpty is ignored when creating empty PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * for unresolved permissions because of the overhead of determining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * PermissionCollection to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * createEmpty should be set to false when this method is invoked from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * implies() because it incurs the additional overhead of creating and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * adding an empty PermissionCollection that will just return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * It should be set to true when invoked from add().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private PermissionCollection getPermissionCollection(Permission p,
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   233
                                                         boolean createEmpty) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   234
        Class<?> c = p.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (!hasUnresolved && !createEmpty) {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   237
            return permsMap.get(c);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   238
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   240
        // Create and add permission collection to map if it is absent.
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   241
        // NOTE: cannot use lambda for mappingFunction parameter until
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   242
        // JDK-8076596 is fixed.
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   243
        return permsMap.computeIfAbsent(c,
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   244
            new java.util.function.Function<>() {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   245
                @Override
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   246
                public PermissionCollection apply(Class<?> k) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   247
                    // Check for unresolved permissions
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   248
                    PermissionCollection pc =
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   249
                        (hasUnresolved ? getUnresolvedPermissions(p) : null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   251
                    // if still null, create a new collection
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   252
                    if (pc == null && createEmpty) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   253
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   254
                        pc = p.newPermissionCollection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   256
                        // still no PermissionCollection?
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   257
                        // We'll give them a PermissionsHash.
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   258
                        if (pc == null) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   259
                            pc = new PermissionsHash();
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   260
                        }
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   261
                    }
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   262
                    return pc;
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   263
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   265
        );
2
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
     * Resolves any unresolved permissions of type p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param p the type of unresolved permission to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return PermissionCollection containing the unresolved permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *  or null if there were no unresolved permissions of type p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private PermissionCollection getUnresolvedPermissions(Permission p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        UnresolvedPermissionCollection uc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        (UnresolvedPermissionCollection) permsMap.get(UnresolvedPermission.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // we have no unresolved permissions if uc is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (uc == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        List<UnresolvedPermission> unresolvedPerms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                        uc.getUnresolvedPermissions(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // we have no unresolved permissions of this type if unresolvedPerms is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (unresolvedPerms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31080
diff changeset
   293
        java.security.cert.Certificate[] certs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31080
diff changeset
   295
        Object[] signers = p.getClass().getSigners();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (int j=0; j < signers.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (signers[j] instanceof java.security.cert.Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            certs = new java.security.cert.Certificate[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (int j=0; j < signers.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (signers[j] instanceof java.security.cert.Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    certs[n++] = (java.security.cert.Certificate)signers[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        PermissionCollection pc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized (unresolvedPerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            int len = unresolvedPerms.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                UnresolvedPermission up = unresolvedPerms.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                Permission perm = up.resolve(p, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    if (pc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        pc = p.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        if (pc == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            pc = new PermissionsHash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    pc.add(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static final long serialVersionUID = 4858622370623524688L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // private Hashtable perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @serialField perms java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *     A table of the Permission classes and PermissionCollections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @serialField allPermission java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        new ObjectStreamField("perms", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        new ObjectStreamField("allPermission", PermissionCollection.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Writes the contents of the permsMap field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * serialization compatibility with earlier releases. allPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        Hashtable<Class<?>, PermissionCollection> perms =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   361
            new Hashtable<>(permsMap.size()*2); // no sync; estimate
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   362
        perms.putAll(permsMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        pfields.put("allPermission", allPermission); // no sync; staleness OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        pfields.put("perms", perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Reads in a Hashtable of Class/PermissionCollections and saves them in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * permsMap field. Reads in allPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Get allPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        allPermission = (PermissionCollection) gfields.get("allPermission", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // Get permissions
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   387
        // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   388
        // the perms 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
   389
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Hashtable<Class<?>, PermissionCollection> perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            (Hashtable<Class<?>, PermissionCollection>)gfields.get("perms", null);
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   392
        permsMap = new ConcurrentHashMap<>(perms.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        permsMap.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // Set hasUnresolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        UnresolvedPermissionCollection uc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        (UnresolvedPermissionCollection) permsMap.get(UnresolvedPermission.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        hasUnresolved = (uc != null && uc.elements().hasMoreElements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
final class PermissionsEnumerator implements Enumeration<Permission> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    // all the perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private Iterator<PermissionCollection> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // the current set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private Enumeration<Permission> permset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    PermissionsEnumerator(Iterator<PermissionCollection> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        perms = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        permset = getNextEnumWithMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    // No need to synchronize; caller should sync on object as required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // if we enter with permissionimpl null, we know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // there are no more left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (permset == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return  false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        // try to see if there are any left in the current one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (permset.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // get the next one that has something in it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        permset = getNextEnumWithMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // if it is null, we are done!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return (permset != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // No need to synchronize; caller should sync on object as required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public Permission nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // hasMoreElements will update permset to the next permset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // with something in it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return permset.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new NoSuchElementException("PermissionsEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private Enumeration<Permission> getNextEnumWithMore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        while (perms.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            PermissionCollection pc = perms.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            Enumeration<Permission> next =pc.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (next.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * A PermissionsHash stores a homogeneous set of permissions in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 * @see Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 * @see Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
final class PermissionsHash extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Key and value are (same) permissions objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   479
    private transient ConcurrentHashMap<Permission, Permission> permsMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Create an empty PermissionsHash object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    PermissionsHash() {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   485
        permsMap = new ConcurrentHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Adds a permission to the PermissionsHash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   493
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public void add(Permission permission) {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   495
        permsMap.put(permission, permission);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param permission the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * the set, false if not.
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
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // attempt a fast lookup and implies. If that fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // then enumerate through all the permissions.
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   511
        Permission p = permsMap.get(permission);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   513
        // If permission is found, then p.equals(permission)
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   514
        if (p == null) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   515
            for (Permission p_ : permsMap.values()) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   516
                if (p_.implies(permission))
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   517
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   519
            return false;
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   520
        } else {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   521
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Returns an enumeration of all the Permission objects in the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return an enumeration of all the Permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   530
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public Enumeration<Permission> elements() {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   532
        return permsMap.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private static final long serialVersionUID = -8491988220802933440L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    // private Hashtable perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @serialField perms java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *     A table of the Permissions (both key and value are same).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        new ObjectStreamField("perms", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Writes the contents of the permsMap field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        Hashtable<Permission, Permission> perms =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   559
                new Hashtable<>(permsMap.size()*2);
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   560
        perms.putAll(permsMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        pfields.put("perms", perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Reads in a Hashtable of Permission/Permission and saves them in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * permsMap field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // Get permissions
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   580
        // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   581
        // the perms 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
   582
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        Hashtable<Permission, Permission> perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                (Hashtable<Permission, Permission>)gfields.get("perms", null);
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 30033
diff changeset
   585
        permsMap = new ConcurrentHashMap<>(perms.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        permsMap.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
}