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