src/java.base/share/classes/javax/security/auth/SubjectDomainCombiner.java
author prappo
Wed, 30 Jan 2019 00:24:32 +0000
changeset 53563 a4b7ea85d668
parent 49150 a4a816f88e58
permissions -rw-r--r--
8218022: Repeated words typos in java.base Reviewed-by: alanb, lancea, mchung Contributed-by: Andrey Turbanov <turbanoff@gmail.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 49150
diff changeset
     2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 javax.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    37
 * A {@code SubjectDomainCombiner} updates ProtectionDomains
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    38
 * with Principals from the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    39
 * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 39751
diff changeset
    41
 * @since 1.4
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class SubjectDomainCombiner implements java.security.DomainCombiner {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private WeakKeyValueMap<ProtectionDomain, ProtectionDomain> cachedPDs =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7291
diff changeset
    47
                new WeakKeyValueMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Set<Principal> principalSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        sun.security.util.Debug.getInstance("combiner",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                        "\t[SubjectDomainCombiner]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    56
     * Associate the provided {@code Subject} with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    57
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    59
     * @param subject the {@code Subject} to be associated with
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 49150
diff changeset
    60
     *          this {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public SubjectDomainCombiner(Subject subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            principalSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    73
     * Get the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    74
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    76
     * @return the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    77
     *          {@code SubjectDomainCombiner}, or {@code null}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    78
     *          if no {@code Subject} is associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    79
     *          {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @exception SecurityException if the caller does not have permission
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    82
     *          to get the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    83
     *          {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public Subject getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            sm.checkPermission(new AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                ("getSubjectFromDomainCombiner"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Update the relevant ProtectionDomains with the Principals
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    96
     * from the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    97
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    99
     * <p> A new {@code ProtectionDomain} instance is created
39751
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   100
     * for each non-static {@code ProtectionDomain} (
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   101
     * (staticPermissionsOnly() == false)
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   102
     * in the {@code currentDomains} array.  Each new {@code ProtectionDomain}
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   103
     * instance is created using the {@code CodeSource},
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   104
     * {@code Permission}s and {@code ClassLoader}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   105
     * from the corresponding {@code ProtectionDomain} in
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   106
     * {@code currentDomains}, as well as with the Principals from
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   107
     * the {@code Subject} associated with this
39751
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   108
     * {@code SubjectDomainCombiner}. Static ProtectionDomains are
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   109
     * combined as-is and no new instance is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
39751
47948cf5c3c5 8147771: Construction of static protection domains under Javax custom policy
valeriep
parents: 39747
diff changeset
   111
     * <p> All of the ProtectionDomains (static and newly instantiated) are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * combined into a new array.  The ProtectionDomains from the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   113
     * {@code assignedDomains} array are appended to this new array,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * and the result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p> Note that optimizations such as the removal of duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * ProtectionDomains may have occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * In addition, caching of ProtectionDomains may be permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param currentDomains the ProtectionDomains associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          current execution Thread, up to the most recent
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   122
     *          privileged {@code ProtectionDomain}.
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   123
     *          The ProtectionDomains are listed in order of execution,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   124
     *          with the most recently executing {@code ProtectionDomain}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *          residing at the beginning of the array. This parameter may
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   126
     *          be {@code null} if the current execution Thread
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   127
     *          has no associated ProtectionDomains.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param assignedDomains the ProtectionDomains inherited from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *          parent Thread, or the ProtectionDomains from the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   131
     *          privileged {@code context}, if a call to
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   132
     *          {@code AccessController.doPrivileged(..., context)}
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   133
     *          had occurred  This parameter may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *          if there were no ProtectionDomains inherited from the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   135
     *          parent Thread, or from the privileged {@code context}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return a new array consisting of the updated ProtectionDomains,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   138
     *          or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public ProtectionDomain[] combine(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (subject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                debug.println("null subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                final Subject s = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    (new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        debug.println(s.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        return null;
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
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // Note that we are returning a direct reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // to the input array - since ACC does not clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // the arrays when it calls combiner.combine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // multiple ACC instances may share the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // array instance in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return assignedDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // optimize currentDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        currentDomains = optimize(currentDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            debug.println("after optimize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (currentDomains == null && assignedDomains == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int cLen = (currentDomains == null ? 0 : currentDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int aLen = (assignedDomains == null ? 0 : assignedDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // the ProtectionDomains for the new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        boolean allNew = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        synchronized(cachedPDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (!subject.isReadOnly() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                !subject.getPrincipals().equals(principalSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // if the Subject was mutated, clear the PD cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                Set<Principal> newSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                synchronized(newSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    principalSet = new java.util.HashSet<Principal>(newSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                cachedPDs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    debug.println("Subject mutated - clearing cache");
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
            ProtectionDomain subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                ProtectionDomain pd = currentDomains[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                subjectPd = cachedPDs.getValue(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (subjectPd == null) {
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   219
                    if (pd.staticPermissionsOnly()) {
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   220
                        // keep static ProtectionDomain objects static
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   221
                        subjectPd = pd;
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   222
                    } else {
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   223
                        subjectPd = new ProtectionDomain(pd.getCodeSource(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                pd.getPermissions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                pd.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                principals);
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 29492
diff changeset
   227
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    cachedPDs.putValue(pd, subjectPd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    allNew = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                newDomains[i] = subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            debug.println("updated current: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                debug.println("\tupdated[" + i + "] = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // now add on the assigned domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (aLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // optimize the result (cached PDs might exist in assignedDomains)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (!allNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                newDomains = optimize(newDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // if aLen == 0 || allNew, no need to further optimize newDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                debug.println("returning null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                debug.println("combinedDomains: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                for (int i = 0; i < newDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    debug.println("newDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                  printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // return the new ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return newDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private static ProtectionDomain[] optimize(ProtectionDomain[] domains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (domains == null || domains.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        ProtectionDomain[] optimized = new ProtectionDomain[domains.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        ProtectionDomain pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for (int i = 0; i < domains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // skip domains with AllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            //  if (domains[i].implies(ALL_PERMISSION))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            //  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // skip System Domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if ((pd = domains[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                // remove duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                for (int j = 0; j < num && !found; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    found = (optimized[j] == pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    optimized[num++] = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // resize the array if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (num > 0 && num < domains.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            ProtectionDomain[] downSize = new ProtectionDomain[num];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            System.arraycopy(optimized, 0, downSize, 0, downSize.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            optimized = downSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return ((num == 0 || optimized.length == 0) ? null : optimized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static void printInputDomains(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            debug.println("currentDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            for (int i = 0; currentDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        i < currentDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                if (currentDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    debug.println("currentDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    debug.println("currentDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                printDomain(currentDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (assignedDomains == null || assignedDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            debug.println("assignedDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            debug.println("assignedDomains = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            for (int i = 0; assignedDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        i < assignedDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (assignedDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    debug.println("assignedDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    debug.println("assignedDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                printDomain(assignedDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private static String printDomain(final ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                return pd.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * A HashMap that has weak keys and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Key objects in this map are the "current" ProtectionDomain instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * received via the combine method.  Each "current" PD is mapped to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * new PD instance that holds both the contents of the "current" PD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * as well as the principals from the Subject associated with this combiner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * The newly created "principal-based" PD values must be stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * WeakReferences since they contain strong references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * corresponding key object (the "current" non-principal-based PD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * which will prevent the key from being GC'd.  Specifically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * a "principal-based" PD contains strong references to the CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * signer certs, PermissionCollection and ClassLoader objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * in the "current PD".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private static class WeakKeyValueMap<K,V> extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                        WeakHashMap<K,WeakReference<V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public V getValue(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            WeakReference<V> wr = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        public V putValue(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            WeakReference<V> wr = super.put(key, new WeakReference<V>(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}