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