jdk/src/share/classes/java/security/AccessControlContext.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8816 29f983feda95
child 10336 0bb1999251f8
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: 8816
diff changeset
     2
 * Copyright (c) 1997, 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: 476
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: 476
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: 476
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 476
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 476
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.SecurityConstants;
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    32
import sun.misc.JavaSecurityAccess;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    33
import sun.misc.SharedSecrets;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An AccessControlContext is used to make system resource access decisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * based on the context it encapsulates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>More specifically, it encapsulates a context and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * has a single method, <code>checkPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that is equivalent to the <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in the AccessController class, with one difference: The AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>checkPermission</code> method makes access decisions based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * context it encapsulates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * rather than that of the current execution thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Thus, the purpose of AccessControlContext is for those situations where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a security check that should be made within a given context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * actually needs to be done from within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <i>different</i> context (for example, from within a worker thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> An AccessControlContext is created by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>AccessController.getContext</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The <code>getContext</code> method takes a "snapshot"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * of the current calling context, and places
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * it in an AccessControlContext object, which it returns. A sample call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   AccessControlContext acc = AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Code within a different context can subsequently call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>checkPermission</code> method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * previously-saved AccessControlContext object. A sample call is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   acc.checkPermission(permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public final class AccessControlContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private ProtectionDomain context[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private boolean isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Note: This field is directly used by the virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // native codes. Don't touch it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private AccessControlContext privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private DomainCombiner combiner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static boolean debugInit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static Debug debug = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static Debug getDebug()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (debugInit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (Policy.isSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                debug = Debug.getInstance("access");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                debugInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
476
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   107
     * Create an AccessControlContext with the given array of ProtectionDomains.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Context must not be null. Duplicate domains will be removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param context the ProtectionDomains associated with this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The non-duplicate domains are copied from the array. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * changes to the array will not affect this AccessControlContext.
476
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   114
     * @throws NullPointerException if <code>context</code> is <code>null</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public AccessControlContext(ProtectionDomain context[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (context.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } else if (context.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (context[0] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                this.context = context.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   127
            List<ProtectionDomain> v = new ArrayList<>(context.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            for (int i =0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if ((context[i] != null) &&  (!v.contains(context[i])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    v.add(context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
476
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   132
            if (!v.isEmpty()) {
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   133
                this.context = new ProtectionDomain[v.size()];
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   134
                this.context = v.toArray(this.context);
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   135
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Create a new <code>AccessControlContext</code> with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <code>AccessControlContext</code> and <code>DomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * This constructor associates the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <code>DomainCombiner</code> with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param acc the <code>AccessControlContext</code> associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          with the provided <code>DomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param combiner the <code>DomainCombiner</code> to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *          with the provided <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception NullPointerException if the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *          <code>context</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception SecurityException if a security manager is installed and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          caller does not have the "createAccessControlContext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public AccessControlContext(AccessControlContext acc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                DomainCombiner combiner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            sm.checkPermission(SecurityConstants.CREATE_ACC_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.context = acc.context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // we do not need to run the combine method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // provided ACC.  it was already "combined" when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // context was originally retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // at this point in time, we simply throw away the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // combiner and use the newly provided one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.combiner = combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * package private for AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    AccessControlContext(ProtectionDomain context[], DomainCombiner combiner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (context != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.context = context.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.combiner = combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * package private constructor for AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    AccessControlContext(ProtectionDomain context[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                 boolean isPrivileged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.isPrivileged = isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   203
     * Constructor for JavaSecurityAccess.doIntersectionPrivilege()
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   204
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   205
    AccessControlContext(ProtectionDomain[] context,
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   206
                         AccessControlContext privilegedContext)
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   207
    {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   208
        this.context = context;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   209
        this.privilegedContext = privilegedContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   210
        this.isPrivileged = true;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   211
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   212
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   213
    /**
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   214
     * Returns this context's context.
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   215
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   216
    ProtectionDomain[] getContext() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   217
        return context;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   218
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   219
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   220
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns true if this context is privileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    boolean isPrivileged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * get the assigned combiner from the privileged or inherited context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    DomainCombiner getAssignedCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            acc = privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            acc = AccessController.getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (acc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return acc.combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Get the <code>DomainCombiner</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the <code>DomainCombiner</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *          <code>AccessControlContext</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *          if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @exception SecurityException if a security manager is installed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *          the caller does not have the "getDomainCombiner"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public DomainCombiner getDomainCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            sm.checkPermission(SecurityConstants.GET_COMBINER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Determines whether the access request indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * specified permission should be allowed or denied, based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the security policy currently in effect, and the context in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * this object. The request is allowed only if every ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * in the context implies the permission. Otherwise the request is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * This method quietly returns if the access request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * is permitted, or throws a suitable AccessControlException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param perm the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @exception AccessControlException if the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * is not permitted, based on the current security policy and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * context encapsulated by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception NullPointerException if the permission to check for is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public void checkPermission(Permission perm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        throws AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        boolean dumpDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (perm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new NullPointerException("permission can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (getDebug() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // If "codebase" is not specified, we dump the info by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            dumpDebug = !Debug.isOn("codebase=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                // If "codebase" is specified, only dump if the specified code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                // value is in the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                for (int i = 0; context != null && i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    if (context[i].getCodeSource() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        context[i].getCodeSource().getLocation() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        Debug.isOn("codebase=" + context[i].getCodeSource().getLocation().toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        dumpDebug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        break;
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
            dumpDebug &= !Debug.isOn("permission=") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                Debug.isOn("permission=" + perm.getClass().getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (dumpDebug && Debug.isOn("stack")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (dumpDebug && Debug.isOn("domain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    debug.println("domain (context is null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    for (int i=0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        debug.println("domain "+i+" "+context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * iterate through the ProtectionDomains in the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * Stop at the first one that doesn't allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * requested permission (throwing an exception).
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
        /* if ctxt is null, all we had on the stack were system domains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
           or the first domain was a Privileged system domain. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
           is to make the common case for system code very fast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        for (int i=0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (context[i] != null &&  !context[i].implies(perm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
97
5c029f131927 6648816: REGRESSION: setting -Djava.security.debug=failure result in NPE in ACC
xuelei
parents: 2
diff changeset
   349
                if (Debug.isOn("failure") && debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // Want to make sure this is always displayed for failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    // but do not want to display again if already displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    final ProtectionDomain pd = context[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    final Debug db = debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    AccessController.doPrivileged (new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            db.println("domain that failed "+pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                            return null;
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
                throw new AccessControlException("access denied "+perm, perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // allow if all of them allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            debug.println("access allowed "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Take the stack-based context (this) and combine it with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * privileged or inherited context, if need be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    AccessControlContext optimize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // the assigned (privileged or inherited) context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            acc = privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            acc = AccessController.getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // this.context could be null if only system code is on the stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // in that case, ignore the stack context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        boolean skipStack = (context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // acc.context could be null if only system code was involved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // in that case, ignore the assigned context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        boolean skipAssigned = (acc == null || acc.context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (acc != null && acc.combiner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // let the assigned acc's combiner do its thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return goCombiner(context, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // optimization: if neither have contexts; return acc if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // rather than this, because acc might have a combiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (skipAssigned && skipStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // optimization: if there is no stack context; there is no reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // to compress the assigned context, it already is compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (skipStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int slen = context.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // optimization: if there is no assigned context and the stack length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // is less then or equal to two; there is no reason to compress the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // stack context, it already is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (skipAssigned && slen <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // optimization: if there is a single stack domain and that domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // is already in the assigned context; no need to combine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if ((slen == 1) && (context[0] == acc.context[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int n = (skipAssigned) ? 0 : acc.context.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // now we combine both of them, and create a new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        ProtectionDomain pd[] = new ProtectionDomain[slen + n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // first copy in the assigned context domains, no need to compress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (!skipAssigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            System.arraycopy(acc.context, 0, pd, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // now add the stack context domains, discarding nulls and duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    outer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            ProtectionDomain sd = context[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (sd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                for (int j = 0; j < n; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    if (sd == pd[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        continue outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                pd[n++] = sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // if length isn't equal, we need to shorten the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (n != pd.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // optimization: if we didn't really combine anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (!skipAssigned && n == acc.context.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } else if (skipAssigned && n == slen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            ProtectionDomain tmp[] = new ProtectionDomain[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            System.arraycopy(pd, 0, tmp, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            pd = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        //      return new AccessControlContext(pd, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Reuse existing ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        this.context = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        this.combiner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        this.isPrivileged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private AccessControlContext goCombiner(ProtectionDomain[] current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                        AccessControlContext assigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // the assigned ACC's combiner is not null --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // let the combiner do its thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // XXX we could add optimizations to 'current' here ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (getDebug() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            debug.println("AccessControlContext invoking the Combiner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // No need to clone current and assigned.context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // combine() will not update them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        ProtectionDomain[] combinedPds = assigned.combiner.combine(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            current, assigned.context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // return new AccessControlContext(combinedPds, assigned.combiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // Reuse existing ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.context = combinedPds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        this.combiner = assigned.combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        this.isPrivileged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Checks two AccessControlContext objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Checks that <i>obj</i> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * an AccessControlContext and has the same set of ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * as this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return true if <i>obj</i> is an AccessControlContext, and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * same set of ProtectionDomains as this context, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (! (obj instanceof AccessControlContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        AccessControlContext that = (AccessControlContext) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return (that.context == null);
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 (that.context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (!(this.containsAllPDs(that) && that.containsAllPDs(this)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (this.combiner == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return (that.combiner == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (that.combiner == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (!this.combiner.equals(that.combiner))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    private boolean containsAllPDs(AccessControlContext that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        boolean match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // ProtectionDomains within an ACC currently cannot be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // and this is enforced by the constructor and the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // optimize methods. However, historically this logic made attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // to support the notion of a null PD and therefore this logic continues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // to support that notion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        ProtectionDomain thisPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if ((thisPd = context[i]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                for (int j = 0; (j < that.context.length) && !match; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    match = (that.context[j] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                Class thisPdClass = thisPd.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                ProtectionDomain thatPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                for (int j = 0; (j < that.context.length) && !match; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    thatPd = that.context[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    // Class check required to avoid PD exposure (4285406)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    match = (thatPd != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        thisPdClass == thatPd.getClass() && thisPd.equals(thatPd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Returns the hash code value for this context. The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * is computed by exclusive or-ing the hash code of all the protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * domains in the context together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return a hash code value for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        for (int i =0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (context[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                hashCode ^= context[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
}