jdk/src/java.base/share/classes/java/security/ProtectionDomain.java
author jbachorik
Thu, 12 Feb 2015 17:01:10 +0100
changeset 29030 725688906a2b
parent 29027 2df5737b2c8a
child 29492 a4bf9a570035
permissions -rw-r--r--
8072932: Test fails with java.security.AccessControlException: access denied ("java.security.SecurityPermission" "getDomainCombiner") Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
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: 5183
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: 5183
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: 5183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
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
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
    28
import java.lang.ref.WeakReference;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    29
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    32
import java.util.Map;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    33
import java.util.WeakHashMap;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    34
import sun.misc.JavaSecurityProtectionDomainAccess;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    35
import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.SecurityConstants;
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    38
import sun.misc.JavaSecurityAccess;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    39
import sun.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This ProtectionDomain class encapsulates the characteristics of a domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * which encloses a set of classes whose instances are granted a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of permissions when being executed on behalf of a given set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * A static set of permissions can be bound to a ProtectionDomain when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * constructed; such permissions are granted to the domain regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Policy in force. However, to support dynamic security policies, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ProtectionDomain can also be constructed such that it is dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * mapped to a set of permissions by the current Policy whenever a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Gary Ellison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class ProtectionDomain {
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    62
    private static class JavaSecurityAccessImpl implements JavaSecurityAccess {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    63
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    64
        private JavaSecurityAccessImpl() {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    65
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    66
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    67
        @Override
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    68
        public <T> T doIntersectionPrivilege(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    69
                PrivilegedAction<T> action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    70
                final AccessControlContext stack,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    71
                final AccessControlContext context) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    72
            if (action == null) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    73
                throw new NullPointerException();
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    74
            }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    75
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    76
            return AccessController.doPrivileged(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    77
                action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    78
                getCombinedACC(context, stack)
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    79
            );
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    80
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    81
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    82
        @Override
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    83
        public <T> T doIntersectionPrivilege(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    84
                PrivilegedAction<T> action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    85
                AccessControlContext context) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    86
            return doIntersectionPrivilege(action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    87
                AccessController.getContext(), context);
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    88
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    89
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    90
        private static AccessControlContext getCombinedACC(AccessControlContext context, AccessControlContext stack) {
29030
725688906a2b 8072932: Test fails with java.security.AccessControlException: access denied ("java.security.SecurityPermission" "getDomainCombiner")
jbachorik
parents: 29027
diff changeset
    91
            AccessControlContext acc = new AccessControlContext(context, stack.getCombiner(), true);
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    92
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    93
            return new AccessControlContext(stack.getContext(), acc).optimize();
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    94
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    95
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    97
    static {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    98
        // Set up JavaSecurityAccess in SharedSecrets
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    99
        SharedSecrets.setJavaSecurityAccess(new JavaSecurityAccessImpl());
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   100
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /* CodeSource */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private CodeSource codesource ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* ClassLoader the protection domain was consed from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private ClassLoader classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /* Principals running-as within this protection domain */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* the rights this protection domain is granted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private PermissionCollection permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /* if the permissions object has AllPermission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private boolean hasAllPerm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* the PermissionCollection is static (pre 1.4 constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
       or dynamic (via a policy refresh) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private boolean staticPermissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   121
    /*
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   122
     * An object used as a key when the ProtectionDomain is stored in a Map.
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   123
     */
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   124
    final Key key = new Key();
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final Debug debug = Debug.getInstance("domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Creates a new ProtectionDomain with the given CodeSource and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Permissions. If the permissions object is not null, then
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18222
diff changeset
   131
     *  {@code setReadOnly())} will be called on the passed in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Permissions object. The only permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * are the ones specified; the current Policy will not be consulted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param codesource the codesource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            PermissionCollection permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.classloader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.principals = new Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        staticPermissions = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Creates a new ProtectionDomain qualified by the given CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Permissions, ClassLoader and array of Principals. If the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18222
diff changeset
   157
     * permissions object is not null, then {@code setReadOnly()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * will be called on the passed in Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * The permissions granted to this domain are dynamic; they include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * both the static permissions passed to this constructor, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * permissions granted to this domain by the current Policy at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * time a permission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * This constructor is typically used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * {@link SecureClassLoader ClassLoaders}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * and {@link DomainCombiner DomainCombiners} which delegate to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18222
diff changeset
   167
     * {@code Policy} to actively associate the permissions granted to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * this domain. This constructor affords the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Policy provider the opportunity to augment the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * PermissionCollection to reflect policy changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param codesource the CodeSource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param classloader the ClassLoader associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param principals the array of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * domain. The contents of the array are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            PermissionCollection permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            ClassLoader classloader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            Principal[] principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.classloader = classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.principals = (principals != null ? principals.clone():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                           new Principal[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        staticPermissions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns the CodeSource of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the CodeSource of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public final CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return this.codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns the ClassLoader of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return the ClassLoader of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public final ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return this.classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns an array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return a non-null array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public final Principal[] getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return this.principals.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns the static permissions granted to this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return the static set of permissions for this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public final PermissionCollection getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Check and see if this ProtectionDomain implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * expressed in the Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * The set of permissions evaluated is a function of whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * ProtectionDomain was constructed with a static set of permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * or it was bound to a dynamically mapped set of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If the ProtectionDomain was constructed to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * {@link #ProtectionDomain(CodeSource, PermissionCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * statically bound} PermissionCollection then the permission will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * only be checked against the PermissionCollection supplied at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * However, if the ProtectionDomain was constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * the constructor variant which supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * {@link #ProtectionDomain(CodeSource, PermissionCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * ClassLoader, java.security.Principal[]) dynamically binding}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * permissions, then the permission will be checked against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * combination of the PermissionCollection supplied at construction and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the current Policy binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param permission the Permission object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return true if "permission" is implicit to this ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (hasAllPerm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // internal permission collection already has AllPermission -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // no need to go to policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (!staticPermissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Policy.getPolicyNoCheck().implies(this, permission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (permissions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return permissions.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   289
    // called by the VM -- do not remove
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   290
    boolean impliesCreateAccessControlContext() {
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   291
        return implies(SecurityConstants.CREATE_ACC_PERMISSION);
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   292
    }
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   293
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Convert a ProtectionDomain to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   297
    @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String pals = "<no principals>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (principals != null && principals.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            StringBuilder palBuf = new StringBuilder("(principals ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                palBuf.append(principals[i].getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                            " \"" + principals[i].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                            "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (i < principals.length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    palBuf.append(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    palBuf.append(")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            pals = palBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // Check if policy is set; we don't want to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // the policy prematurely here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        PermissionCollection pc = Policy.isSet() && seeAllp() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                      mergePermissions():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                      getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return "ProtectionDomain "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            " "+codesource+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            " "+classloader+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            " "+pals+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            " "+pc+"\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Return true (merge policy permissions) in the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * . SecurityManager is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          debug is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          SecurityManager impelmentation is in bootclasspath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *          Policy implementation is in bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *          (the bootclasspath restrictions avoid recursion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *          debug is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *          caller has Policy.getPolicy permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static boolean seeAllp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (sm.getClass().getClassLoader() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    Policy.getPolicyNoCheck().getClass().getClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                                == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    // fall thru and return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private PermissionCollection mergePermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (staticPermissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        PermissionCollection perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            (new java.security.PrivilegedAction<PermissionCollection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        Policy p = Policy.getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        return p.getPermissions(ProtectionDomain.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        Permissions mergedPerms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int swag = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int vcap = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Enumeration<Permission> e;
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   384
        List<Permission> pdVector = new ArrayList<>(vcap);
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   385
        List<Permission> plVector = new ArrayList<>(swag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // Build a vector of domain permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    pdVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // Build a vector of Policy permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (perms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                e = perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    plVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    vcap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (perms != null && permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // Weed out the duplicates from the policy. Unless a refresh
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18579
diff changeset
   413
            // has occurred since the pd was consed this should result in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // an empty vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                e = permissions.elements();   // domain vs policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    Permission pdp = e.nextElement();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   419
                    Class<?> pdpClass = pdp.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    String pdpActions = pdp.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    String pdpName = pdp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    for (int i = 0; i < plVector.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        Permission pp = plVector.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (pdpClass.isInstance(pp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            // The equals() method on some permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            // have some side effects so this manual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            // comparison is sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            if (pdpName.equals(pp.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                pdpActions.equals(pp.getActions())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                plVector.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (perms !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // the order of adding to merged perms and permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // needs to preserve the bugfix 4301064
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            for (int i = plVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                mergedPerms.add(plVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            for (int i = pdVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                mergedPerms.add(pdVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return mergedPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   455
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   456
    /**
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   457
     * Used for storing ProtectionDomains as keys in a Map.
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   458
     */
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   459
    final static class Key {}
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   460
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   461
    // A cache of ProtectionDomains and their Permissions
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   462
    private static class PDCache implements ProtectionDomainCache {
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   463
        // We must wrap the PermissionCollection in a WeakReference as there
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   464
        // are some PermissionCollections which contain strong references
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   465
        // back to a ProtectionDomain and otherwise would never be removed
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   466
        // from the WeakHashMap
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   467
        private final Map<Key, WeakReference<PermissionCollection>>
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   468
            map = new WeakHashMap<>();
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   469
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   470
        @Override
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   471
        public synchronized void put(ProtectionDomain pd,
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   472
                                     PermissionCollection pc) {
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   473
            map.put(pd == null ? null : pd.key, new WeakReference<>(pc));
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   474
        }
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   475
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   476
        @Override
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   477
        public synchronized PermissionCollection get(ProtectionDomain pd) {
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   478
            WeakReference<PermissionCollection> ref =
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   479
                map.get(pd == null ? null : pd.key);
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   480
            return ref == null ? null : ref.get();
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   481
        }
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   482
    }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   483
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   484
    static {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   485
        SharedSecrets.setJavaSecurityProtectionDomainAccess(
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   486
            new JavaSecurityProtectionDomainAccess() {
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   487
                @Override
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   488
                public ProtectionDomainCache getProtectionDomainCache() {
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   489
                    return new PDCache();
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   490
                }
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   491
            });
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   492
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
}