jdk/src/share/classes/java/security/AccessControlContext.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 97 5c029f131927
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Create an AccessControlContext with the given set of ProtectionDomains.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public AccessControlContext(ProtectionDomain context[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (context.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else if (context.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (context[0] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                this.context = context.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            List<ProtectionDomain> v = new ArrayList<ProtectionDomain>(context.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (int i =0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if ((context[i] != null) &&  (!v.contains(context[i])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    v.add(context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            this.context = new ProtectionDomain[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            this.context = v.toArray(this.context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Create a new <code>AccessControlContext</code> with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>AccessControlContext</code> and <code>DomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * This constructor associates the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <code>DomainCombiner</code> with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param acc the <code>AccessControlContext</code> associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          with the provided <code>DomainCombiner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param combiner the <code>DomainCombiner</code> to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          with the provided <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception NullPointerException if the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          <code>context</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception SecurityException if a security manager is installed and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *          caller does not have the "createAccessControlContext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public AccessControlContext(AccessControlContext acc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                DomainCombiner combiner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            sm.checkPermission(SecurityConstants.CREATE_ACC_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.context = acc.context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // we do not need to run the combine method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // provided ACC.  it was already "combined" when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // context was originally retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // at this point in time, we simply throw away the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // combiner and use the newly provided one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.combiner = combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * package private for AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    AccessControlContext(ProtectionDomain context[], DomainCombiner combiner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (context != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            this.context = context.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this.combiner = combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * package private constructor for AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    AccessControlContext(ProtectionDomain context[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                 boolean isPrivileged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.isPrivileged = isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns true if this context is privileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    boolean isPrivileged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * get the assigned combiner from the privileged or inherited context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    DomainCombiner getAssignedCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            acc = privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            acc = AccessController.getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (acc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return acc.combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Get the <code>DomainCombiner</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return the <code>DomainCombiner</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *          <code>AccessControlContext</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *          if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @exception SecurityException if a security manager is installed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *          the caller does not have the "getDomainCombiner"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public DomainCombiner getDomainCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            sm.checkPermission(SecurityConstants.GET_COMBINER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return combiner;
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
     * Determines whether the access request indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * specified permission should be allowed or denied, based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the security policy currently in effect, and the context in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * this object. The request is allowed only if every ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * in the context implies the permission. Otherwise the request is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * This method quietly returns if the access request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * is permitted, or throws a suitable AccessControlException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param perm the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception AccessControlException if the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * is not permitted, based on the current security policy and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * context encapsulated by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception NullPointerException if the permission to check for is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void checkPermission(Permission perm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        throws AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        boolean dumpDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (perm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new NullPointerException("permission can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (getDebug() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // If "codebase" is not specified, we dump the info by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            dumpDebug = !Debug.isOn("codebase=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // If "codebase" is specified, only dump if the specified code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // value is in the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                for (int i = 0; context != null && i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    if (context[i].getCodeSource() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        context[i].getCodeSource().getLocation() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        Debug.isOn("codebase=" + context[i].getCodeSource().getLocation().toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        dumpDebug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            dumpDebug &= !Debug.isOn("permission=") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                Debug.isOn("permission=" + perm.getClass().getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (dumpDebug && Debug.isOn("stack")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (dumpDebug && Debug.isOn("domain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    debug.println("domain (context is null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    for (int i=0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        debug.println("domain "+i+" "+context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
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
         * iterate through the ProtectionDomains in the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * Stop at the first one that doesn't allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * requested permission (throwing an exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /* if ctxt is null, all we had on the stack were system domains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
           or the first domain was a Privileged system domain. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
           is to make the common case for system code very fast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        for (int i=0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (context[i] != null &&  !context[i].implies(perm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (Debug.isOn("failure")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    // Want to make sure this is always displayed for failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    // but do not want to display again if already displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    // above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    final ProtectionDomain pd = context[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    final Debug db = debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    AccessController.doPrivileged (new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            db.println("domain that failed "+pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                throw new AccessControlException("access denied "+perm, perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // allow if all of them allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            debug.println("access allowed "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Take the stack-based context (this) and combine it with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * privileged or inherited context, if need be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    AccessControlContext optimize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // the assigned (privileged or inherited) context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            acc = privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            acc = AccessController.getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // this.context could be null if only system code is on the stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // in that case, ignore the stack context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        boolean skipStack = (context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // acc.context could be null if only system code was involved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // in that case, ignore the assigned context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        boolean skipAssigned = (acc == null || acc.context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (acc != null && acc.combiner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // let the assigned acc's combiner do its thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return goCombiner(context, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // optimization: if neither have contexts; return acc if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // rather than this, because acc might have a combiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (skipAssigned && skipStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // optimization: if there is no stack context; there is no reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // to compress the assigned context, it already is compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (skipStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int slen = context.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // optimization: if there is no assigned context and the stack length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // is less then or equal to two; there is no reason to compress the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // stack context, it already is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (skipAssigned && slen <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // optimization: if there is a single stack domain and that domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // is already in the assigned context; no need to combine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if ((slen == 1) && (context[0] == acc.context[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int n = (skipAssigned) ? 0 : acc.context.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // now we combine both of them, and create a new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        ProtectionDomain pd[] = new ProtectionDomain[slen + n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // first copy in the assigned context domains, no need to compress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (!skipAssigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            System.arraycopy(acc.context, 0, pd, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // now add the stack context domains, discarding nulls and duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    outer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            ProtectionDomain sd = context[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (sd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                for (int j = 0; j < n; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (sd == pd[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        continue outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                pd[n++] = sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // if length isn't equal, we need to shorten the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (n != pd.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            // optimization: if we didn't really combine anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (!skipAssigned && n == acc.context.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } else if (skipAssigned && n == slen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            ProtectionDomain tmp[] = new ProtectionDomain[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            System.arraycopy(pd, 0, tmp, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            pd = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        //      return new AccessControlContext(pd, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // Reuse existing ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        this.context = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        this.combiner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        this.isPrivileged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private AccessControlContext goCombiner(ProtectionDomain[] current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                        AccessControlContext assigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // the assigned ACC's combiner is not null --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // let the combiner do its thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // XXX we could add optimizations to 'current' here ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (getDebug() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            debug.println("AccessControlContext invoking the Combiner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // No need to clone current and assigned.context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // combine() will not update them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        ProtectionDomain[] combinedPds = assigned.combiner.combine(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            current, assigned.context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // return new AccessControlContext(combinedPds, assigned.combiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // Reuse existing ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        this.context = combinedPds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        this.combiner = assigned.combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        this.isPrivileged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Checks two AccessControlContext objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Checks that <i>obj</i> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * an AccessControlContext and has the same set of ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * as this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @return true if <i>obj</i> is an AccessControlContext, and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * same set of ProtectionDomains as this context, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (! (obj instanceof AccessControlContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        AccessControlContext that = (AccessControlContext) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return (that.context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (that.context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (!(this.containsAllPDs(that) && that.containsAllPDs(this)))
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.combiner == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return (that.combiner == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (that.combiner == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (!this.combiner.equals(that.combiner))
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
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    private boolean containsAllPDs(AccessControlContext that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        boolean match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // ProtectionDomains within an ACC currently cannot be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // and this is enforced by the constructor and the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // optimize methods. However, historically this logic made attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // to support the notion of a null PD and therefore this logic continues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // to support that notion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        ProtectionDomain thisPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if ((thisPd = context[i]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                for (int j = 0; (j < that.context.length) && !match; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    match = (that.context[j] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                Class thisPdClass = thisPd.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                ProtectionDomain thatPd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                for (int j = 0; (j < that.context.length) && !match; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    thatPd = that.context[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    // Class check required to avoid PD exposure (4285406)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    match = (thatPd != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        thisPdClass == thatPd.getClass() && thisPd.equals(thatPd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the hash code value for this context. The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * is computed by exclusive or-ing the hash code of all the protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * domains in the context together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return a hash code value for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        for (int i =0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (context[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                hashCode ^= context[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}