jdk/src/share/classes/javax/security/auth/SubjectDomainCombiner.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
child 10709 d865c9f21240
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A <code>SubjectDomainCombiner</code> updates ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with Principals from the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>SubjectDomainCombiner</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // 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
    60
    private static final boolean useJavaxPolicy =
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
    61
        javax.security.auth.Policy.isCustomPolicySet(debug);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Relevant only when useJavaxPolicy is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final boolean allowCaching =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                        (useJavaxPolicy && cachePolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Associate the provided <code>Subject</code> with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param subject the <code>Subject</code> to be associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *          with this <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public SubjectDomainCombiner(Subject subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            principalSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Get the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *          <code>SubjectDomainCombiner</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *          if no <code>Subject</code> is associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *          to get the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *          <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Subject getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            sm.checkPermission(new AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ("getSubjectFromDomainCombiner"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Update the relevant ProtectionDomains with the Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * from the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p> A new <code>ProtectionDomain</code> instance is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * for each <code>ProtectionDomain</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <i>currentDomains</i> array.  Each new <code>ProtectionDomain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * instance is created using the <code>CodeSource</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>Permission</code>s and <code>ClassLoader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * from the corresponding <code>ProtectionDomain</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <i>currentDomains</i>, as well as with the Principals from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>SubjectDomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p> All of the newly instantiated ProtectionDomains are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * combined into a new array.  The ProtectionDomains from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <i>assignedDomains</i> array are appended to this new array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * and the result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p> Note that optimizations such as the removal of duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * ProtectionDomains may have occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * In addition, caching of ProtectionDomains may be permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param currentDomains the ProtectionDomains associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *          current execution Thread, up to the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *          privileged <code>ProtectionDomain</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          The ProtectionDomains are are listed in order of execution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *          with the most recently executing <code>ProtectionDomain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          residing at the beginning of the array. This parameter may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          be <code>null</code> if the current execution Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          has no associated ProtectionDomains.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param assignedDomains the ProtectionDomains inherited from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          parent Thread, or the ProtectionDomains from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *          privileged <i>context</i>, if a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *          AccessController.doPrivileged(..., <i>context</i>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          had occurred  This parameter may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          if there were no ProtectionDomains inherited from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          parent Thread, or from the privileged <i>context</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return a new array consisting of the updated ProtectionDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public ProtectionDomain[] combine(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (subject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                debug.println("null subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                final Subject s = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    (new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        debug.println(s.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // Note that we are returning a direct reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // to the input array - since ACC does not clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // the arrays when it calls combiner.combine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // multiple ACC instances may share the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // array instance in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return assignedDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // optimize currentDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // No need to optimize assignedDomains because it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // have been previously optimized (when it was set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        currentDomains = optimize(currentDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            debug.println("after optimize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            printInputDomains(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (currentDomains == null && assignedDomains == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
   202
        // 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
   203
        // their own custom javax.security.auth.Policy implementations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (useJavaxPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return combineJavaxPolicy(currentDomains, assignedDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int cLen = (currentDomains == null ? 0 : currentDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int aLen = (assignedDomains == null ? 0 : assignedDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // the ProtectionDomains for the new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        boolean allNew = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        synchronized(cachedPDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (!subject.isReadOnly() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                !subject.getPrincipals().equals(principalSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // if the Subject was mutated, clear the PD cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                Set<Principal> newSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                synchronized(newSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    principalSet = new java.util.HashSet<Principal>(newSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                cachedPDs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    debug.println("Subject mutated - clearing cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            ProtectionDomain subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                ProtectionDomain pd = currentDomains[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                subjectPd = cachedPDs.getValue(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (subjectPd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    subjectPd = new ProtectionDomain(pd.getCodeSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                                pd.getPermissions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                                pd.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                                principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    cachedPDs.putValue(pd, subjectPd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    allNew = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                newDomains[i] = subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            debug.println("updated current: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                debug.println("\tupdated[" + i + "] = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // now add on the assigned domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (aLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // optimize the result (cached PDs might exist in assignedDomains)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (!allNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                newDomains = optimize(newDomains);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // if aLen == 0 || allNew, no need to further optimize newDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                debug.println("returning null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                debug.println("combinedDomains: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                for (int i = 0; i < newDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    debug.println("newDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                  printDomain(newDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // return the new ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return newDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Use the javax.security.auth.Policy implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private ProtectionDomain[] combineJavaxPolicy(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (!allowCaching) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                (new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        // Call refresh only caching is disallowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        javax.security.auth.Policy.getPolicy().refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int cLen = (currentDomains == null ? 0 : currentDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int aLen = (assignedDomains == null ? 0 : assignedDomains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // the ProtectionDomains for the new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        synchronized(cachedPDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (!subject.isReadOnly() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                !subject.getPrincipals().equals(principalSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // if the Subject was mutated, clear the PD cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                Set<Principal> newSet = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                synchronized(newSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    principalSet = new java.util.HashSet<Principal>(newSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                principals = principalSet.toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        (new Principal[principalSet.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                cachedPDs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    debug.println("Subject mutated - clearing cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                ProtectionDomain pd = currentDomains[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                ProtectionDomain subjectPd = cachedPDs.getValue(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (subjectPd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    // we must first add the original permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    // that way when we later add the new JAAS permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // any unresolved JAAS-related permissions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    // automatically get resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    // get the original perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    PermissionCollection coll = pd.getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    java.util.Enumeration e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (coll != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        synchronized (coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            e = coll.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                Permission newPerm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                        (Permission)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                 perms.add(newPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    // get perms from the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    final java.security.CodeSource finalCs = pd.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    final Subject finalS = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    PermissionCollection newPerms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        (new PrivilegedAction<PermissionCollection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                          return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                          javax.security.auth.Policy.getPolicy().getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                (finalS, finalCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // add the newly granted perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    // avoiding duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    synchronized (newPerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        e = newPerms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                            Permission newPerm = (Permission)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            if (!perms.implies(newPerm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                perms.add(newPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                    debug.println (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                        "Adding perm " + newPerm + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    subjectPd = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        (finalCs, perms, pd.getClassLoader(), principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    if (allowCaching)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        cachedPDs.putValue(pd, subjectPd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                newDomains[i] = subjectPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            debug.println("updated current: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            for (int i = 0; i < cLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                debug.println("\tupdated[" + i + "] = " + newDomains[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // now add on the assigned domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (aLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                debug.println("returning null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                debug.println("combinedDomains: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (int i = 0; i < newDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    debug.println("newDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        newDomains[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // return the new ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (newDomains == null || newDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return newDomains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private static ProtectionDomain[] optimize(ProtectionDomain[] domains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (domains == null || domains.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        ProtectionDomain[] optimized = new ProtectionDomain[domains.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        ProtectionDomain pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        for (int i = 0; i < domains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // skip domains with AllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            //  if (domains[i].implies(ALL_PERMISSION))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            //  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // skip System Domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if ((pd = domains[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // remove duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                for (int j = 0; j < num && !found; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    found = (optimized[j] == pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    optimized[num++] = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // resize the array if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (num > 0 && num < domains.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            ProtectionDomain[] downSize = new ProtectionDomain[num];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            System.arraycopy(optimized, 0, downSize, 0, downSize.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            optimized = downSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return ((num == 0 || optimized.length == 0) ? null : optimized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private static boolean cachePolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        String s = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            (new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            public String run() {
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 5506
diff changeset
   476
                return Security.getProperty("cache.auth.policy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return Boolean.parseBoolean(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // cache by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static void printInputDomains(ProtectionDomain[] currentDomains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                ProtectionDomain[] assignedDomains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (currentDomains == null || currentDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            debug.println("currentDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            for (int i = 0; currentDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        i < currentDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (currentDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    debug.println("currentDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    debug.println("currentDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                printDomain(currentDomains[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (assignedDomains == null || assignedDomains.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            debug.println("assignedDomains null or 0 length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            debug.println("assignedDomains = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            for (int i = 0; assignedDomains != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        i < assignedDomains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (assignedDomains[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    debug.println("assignedDomain " + i + ": SystemDomain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    debug.println("assignedDomain " + i + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                printDomain(assignedDomains[i]));
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private static String printDomain(final ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return pd.toString();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * A HashMap that has weak keys and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Key objects in this map are the "current" ProtectionDomain instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * received via the combine method.  Each "current" PD is mapped to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * new PD instance that holds both the contents of the "current" PD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * as well as the principals from the Subject associated with this combiner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * The newly created "principal-based" PD values must be stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * WeakReferences since they contain strong references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * corresponding key object (the "current" non-principal-based PD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * which will prevent the key from being GC'd.  Specifically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * a "principal-based" PD contains strong references to the CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * signer certs, PermissionCollection and ClassLoader objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * in the "current PD".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    private static class WeakKeyValueMap<K,V> extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                        WeakHashMap<K,WeakReference<V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        public V getValue(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            WeakReference<V> wr = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        public V putValue(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            WeakReference<V> wr = super.put(key, new WeakReference<V>(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (wr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
}