jdk/src/java.base/share/classes/javax/security/auth/SubjectDomainCombiner.java
author avstepan
Mon, 16 Mar 2015 19:09:13 +0400
changeset 29492 a4bf9a570035
parent 28059 e576535359cc
child 39747 2d542b973871
permissions -rw-r--r--
8028266: Tidy warnings cleanup for packages java.security/javax.security Summary: some tidy warnings in docs were fixed Reviewed-by: mullan, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    42
 * A {@code SubjectDomainCombiner} updates ProtectionDomains
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    43
 * with Principals from the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    44
 * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class SubjectDomainCombiner implements java.security.DomainCombiner {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private WeakKeyValueMap<ProtectionDomain, ProtectionDomain> cachedPDs =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7291
diff changeset
    51
                new WeakKeyValueMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Set<Principal> principalSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        sun.security.util.Debug.getInstance("combiner",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                        "\t[SubjectDomainCombiner]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
    59
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Note: check only at classloading time, not dynamically during combine()
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
    61
    private static final boolean useJavaxPolicy =
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
    62
        javax.security.auth.Policy.isCustomPolicySet(debug);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Relevant only when useJavaxPolicy is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final boolean allowCaching =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                        (useJavaxPolicy && cachePolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    69
     * Associate the provided {@code Subject} with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    70
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    72
     * @param subject the {@code Subject} to be associated with
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    73
     *          with this {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public SubjectDomainCombiner(Subject subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            principalSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    86
     * Get the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    87
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    89
     * @return the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    90
     *          {@code SubjectDomainCombiner}, or {@code null}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    91
     *          if no {@code Subject} is associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    92
     *          {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @exception SecurityException if the caller does not have permission
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    95
     *          to get the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
    96
     *          {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public Subject getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            sm.checkPermission(new AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                ("getSubjectFromDomainCombiner"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Update the relevant ProtectionDomains with the Principals
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   109
     * from the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   110
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   112
     * <p> A new {@code ProtectionDomain} instance is created
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   113
     * for each {@code ProtectionDomain} in the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   114
     * {@code currentDomains} array.  Each new {@code ProtectionDomain}
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   115
     * instance is created using the {@code CodeSource},
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   116
     * {@code Permission}s and {@code ClassLoader}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   117
     * from the corresponding {@code ProtectionDomain} in
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   118
     * {@code currentDomains}, as well as with the Principals from
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   119
     * the {@code Subject} associated with this
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   120
     * {@code SubjectDomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p> All of the newly instantiated ProtectionDomains are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * 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
   124
     * {@code assignedDomains} array are appended to this new array,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * and the result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> Note that optimizations such as the removal of duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * ProtectionDomains may have occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * In addition, caching of ProtectionDomains may be permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param currentDomains the ProtectionDomains associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *          current execution Thread, up to the most recent
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   133
     *          privileged {@code ProtectionDomain}.
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   134
     *          The ProtectionDomains are listed in order of execution,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   135
     *          with the most recently executing {@code ProtectionDomain}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          residing at the beginning of the array. This parameter may
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   137
     *          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
   138
     *          has no associated ProtectionDomains.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param assignedDomains the ProtectionDomains inherited from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          parent Thread, or the ProtectionDomains from the
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   142
     *          privileged {@code context}, if a call to
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 28059
diff changeset
   143
     *          {@code AccessController.doPrivileged(..., context)}
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   144
     *          had occurred  This parameter may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          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
   146
     *          parent Thread, or from the privileged {@code context}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return a new array consisting of the updated ProtectionDomains,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 10709
diff changeset
   149
     *          or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public ProtectionDomain[] combine(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (subject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                debug.println("null subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                final Subject s = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    (new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        debug.println(s.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Note that we are returning a direct reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // to the input array - since ACC does not clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // the arrays when it calls combiner.combine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // multiple ACC instances may share the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // array instance in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return 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
        // optimize currentDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        currentDomains = optimize(currentDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            debug.println("after optimize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (currentDomains == null && assignedDomains == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
   197
        // maintain backwards compatibility for developers who provide
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
   198
        // their own custom javax.security.auth.Policy implementations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (useJavaxPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return combineJavaxPolicy(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int cLen = (currentDomains == null ? 0 : currentDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int aLen = (assignedDomains == null ? 0 : assignedDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // the ProtectionDomains for the new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        boolean allNew = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        synchronized(cachedPDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (!subject.isReadOnly() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                !subject.getPrincipals().equals(principalSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                // if the Subject was mutated, clear the PD cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                Set<Principal> newSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                synchronized(newSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    principalSet = new java.util.HashSet<Principal>(newSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                cachedPDs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    debug.println("Subject mutated - clearing cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            ProtectionDomain subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                ProtectionDomain pd = currentDomains[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                subjectPd = cachedPDs.getValue(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (subjectPd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    subjectPd = new ProtectionDomain(pd.getCodeSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                                pd.getPermissions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                                pd.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                                principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    cachedPDs.putValue(pd, subjectPd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    allNew = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                newDomains[i] = subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            debug.println("updated current: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                debug.println("\tupdated[" + i + "] = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // now add on the assigned domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (aLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // optimize the result (cached PDs might exist in assignedDomains)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (!allNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                newDomains = optimize(newDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // if aLen == 0 || allNew, no need to further optimize newDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                debug.println("returning null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                debug.println("combinedDomains: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                for (int i = 0; i < newDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    debug.println("newDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                  printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // return the new ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return newDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Use the javax.security.auth.Policy implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private ProtectionDomain[] combineJavaxPolicy(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!allowCaching) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                (new PrivilegedAction<Void>() {
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   298
                    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        // Call refresh only caching is disallowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        javax.security.auth.Policy.getPolicy().refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   307
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int cLen = (currentDomains == null ? 0 : currentDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int aLen = (assignedDomains == null ? 0 : assignedDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // the ProtectionDomains for the new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        synchronized(cachedPDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (!subject.isReadOnly() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                !subject.getPrincipals().equals(principalSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // if the Subject was mutated, clear the PD cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                Set<Principal> newSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                synchronized(newSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    principalSet = new java.util.HashSet<Principal>(newSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                cachedPDs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    debug.println("Subject mutated - clearing cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                ProtectionDomain pd = currentDomains[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                ProtectionDomain subjectPd = cachedPDs.getValue(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (subjectPd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    // we must first add the original permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    // that way when we later add the new JAAS permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // any unresolved JAAS-related permissions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    // automatically get resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // get the original perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    PermissionCollection coll = pd.getPermissions();
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   348
                    java.util.Enumeration<Permission> e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    if (coll != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        synchronized (coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            e = coll.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                Permission newPerm =
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   354
                                        e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                 perms.add(newPerm);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    // get perms from the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    final java.security.CodeSource finalCs = pd.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    final Subject finalS = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    PermissionCollection newPerms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        (new PrivilegedAction<PermissionCollection>() {
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   367
                        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                          return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                          javax.security.auth.Policy.getPolicy().getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                (finalS, finalCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    // add the newly granted perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // avoiding duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    synchronized (newPerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        e = newPerms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        while (e.hasMoreElements()) {
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   380
                            Permission newPerm = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            if (!perms.implies(newPerm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                perms.add(newPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                    debug.println (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                        "Adding perm " + newPerm + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    subjectPd = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        (finalCs, perms, pd.getClassLoader(), principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    if (allowCaching)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        cachedPDs.putValue(pd, subjectPd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                newDomains[i] = subjectPd;
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
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            debug.println("updated current: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                debug.println("\tupdated[" + i + "] = " + newDomains[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // now add on the assigned domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (aLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                debug.println("returning null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                debug.println("combinedDomains: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                for (int i = 0; i < newDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    debug.println("newDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        newDomains[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // return the new ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return newDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private static ProtectionDomain[] optimize(ProtectionDomain[] domains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (domains == null || domains.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        ProtectionDomain[] optimized = new ProtectionDomain[domains.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        ProtectionDomain pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        for (int i = 0; i < domains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // skip domains with AllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            //  if (domains[i].implies(ALL_PERMISSION))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            //  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // skip System Domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if ((pd = domains[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // remove duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                for (int j = 0; j < num && !found; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    found = (optimized[j] == pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    optimized[num++] = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // resize the array if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (num > 0 && num < domains.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            ProtectionDomain[] downSize = new ProtectionDomain[num];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            System.arraycopy(optimized, 0, downSize, 0, downSize.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            optimized = downSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return ((num == 0 || optimized.length == 0) ? null : optimized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private static boolean cachePolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        String s = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            (new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            public String run() {
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
   474
                return Security.getProperty("cache.auth.policy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return Boolean.parseBoolean(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // cache by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private static void printInputDomains(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            debug.println("currentDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            for (int i = 0; currentDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        i < currentDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                if (currentDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    debug.println("currentDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    debug.println("currentDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                printDomain(currentDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (assignedDomains == null || assignedDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            debug.println("assignedDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            debug.println("assignedDomains = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            for (int i = 0; assignedDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        i < assignedDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                if (assignedDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    debug.println("assignedDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    debug.println("assignedDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                printDomain(assignedDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private static String printDomain(final ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return pd.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * A HashMap that has weak keys and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Key objects in this map are the "current" ProtectionDomain instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * received via the combine method.  Each "current" PD is mapped to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * new PD instance that holds both the contents of the "current" PD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * as well as the principals from the Subject associated with this combiner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * The newly created "principal-based" PD values must be stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * WeakReferences since they contain strong references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * corresponding key object (the "current" non-principal-based PD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * which will prevent the key from being GC'd.  Specifically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * a "principal-based" PD contains strong references to the CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * signer certs, PermissionCollection and ClassLoader objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * in the "current PD".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private static class WeakKeyValueMap<K,V> extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                        WeakHashMap<K,WeakReference<V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public V getValue(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            WeakReference<V> wr = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        public V putValue(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            WeakReference<V> wr = super.put(key, new WeakReference<V>(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
}