jdk/src/share/classes/javax/security/auth/Subject.java
author weijun
Thu, 13 Jun 2013 10:31:21 +0800
changeset 20806 d94588b7854d
parent 14342 8435a30053c1
child 20831 96cf18811858
permissions -rw-r--r--
8015731: Subject java.security.auth.subject to improvements Reviewed-by: skoivu, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10709
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4331
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4331
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4331
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4331
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4331
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.DomainCombiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.util.ResourcesMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> A <code>Subject</code> represents a grouping of related information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * for a single entity, such as a person.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Such information includes the Subject's identities as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * its security-related attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * (passwords and cryptographic keys, for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> Subjects may potentially have multiple identities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Each identity is represented as a <code>Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * within the <code>Subject</code>.  Principals simply bind names to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>Subject</code>.  For example, a <code>Subject</code> that happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to be a person, Alice, might have two Principals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * one which binds "Alice Bar", the name on her driver license,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to the <code>Subject</code>, and another which binds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * "999-99-9999", the number on her student identification card,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * to the <code>Subject</code>.  Both Principals refer to the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>Subject</code> even though each has a different name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p> A <code>Subject</code> may also own security-related attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * which are referred to as credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Sensitive credentials that require special protection, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * private cryptographic keys, are stored within a private credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>Set</code>.  Credentials intended to be shared, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * public key certificates or Kerberos server tickets are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * within a public credential <code>Set</code>.  Different permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * are required to access and modify the different credential Sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> To retrieve all the Principals associated with a <code>Subject</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * invoke the <code>getPrincipals</code> method.  To retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * all the public or private credentials belonging to a <code>Subject</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * invoke the <code>getPublicCredentials</code> method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <code>getPrivateCredentials</code> method, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * To modify the returned <code>Set</code> of Principals and credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * use the methods defined in the <code>Set</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      Object credential;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      // add a Principal and credential to the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      subject.getPrincipals().add(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      subject.getPublicCredentials().add(credential);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p> This <code>Subject</code> class implements <code>Serializable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * While the Principals associated with the <code>Subject</code> are serialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * the credentials associated with the <code>Subject</code> are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Note that the <code>java.security.Principal</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * does not implement <code>Serializable</code>.  Therefore all concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <code>Principal</code> implementations associated with Subjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * must implement <code>Serializable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see java.security.Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
public final class Subject implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final long serialVersionUID = -8308522755600156056L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * A <code>Set</code> that provides a view of all of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Subject's Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @serial Each element in this set is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *          <code>java.security.Principal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *          The set is a <code>Subject.SecureSet</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    Set<Principal> principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Sets that provide a view of all of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Subject's Credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    transient Set<Object> pubCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    transient Set<Object> privCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Whether this Subject is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private volatile boolean readOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final int PRINCIPAL_SET = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static final int PUB_CREDENTIAL_SET = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final int PRIV_CREDENTIAL_SET = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final ProtectionDomain[] NULL_PD_ARRAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        = new ProtectionDomain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Create an instance of a <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * with an empty <code>Set</code> of Principals and empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Sets of public and private credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p> The newly constructed Sets check whether this <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * has been set read-only before permitting subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The newly created Sets also prevent illegal modifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * by ensuring that callers have sufficient permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p> To modify the Principals Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>AuthPermission("modifyPrincipals")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * To modify the public credential Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>AuthPermission("modifyPublicCredentials")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * To modify the private credential Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <code>AuthPermission("modifyPrivateCredentials")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public Subject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.principals = Collections.synchronizedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        (new SecureSet<Principal>(this, PRINCIPAL_SET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.pubCredentials = Collections.synchronizedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        (new SecureSet<Object>(this, PUB_CREDENTIAL_SET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.privCredentials = Collections.synchronizedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        (new SecureSet<Object>(this, PRIV_CREDENTIAL_SET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Create an instance of a <code>Subject</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Principals and credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p> The Principals and credentials from the specified Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * are copied into newly constructed Sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * These newly created Sets check whether this <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * has been set read-only before permitting subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * The newly created Sets also prevent illegal modifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * by ensuring that callers have sufficient permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p> To modify the Principals Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>AuthPermission("modifyPrincipals")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * To modify the public credential Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <code>AuthPermission("modifyPublicCredentials")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * To modify the private credential Set, the caller must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>AuthPermission("modifyPrivateCredentials")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param readOnly true if the <code>Subject</code> is to be read-only,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *          and false otherwise. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param principals the <code>Set</code> of Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *          to be associated with this <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param pubCredentials the <code>Set</code> of public credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *          to be associated with this <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param privCredentials the <code>Set</code> of private credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          to be associated with this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception NullPointerException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          <code>principals</code>, <code>pubCredentials</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          or <code>privCredentials</code> are <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Subject(boolean readOnly, Set<? extends Principal> principals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                   Set<?> pubCredentials, Set<?> privCredentials)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (principals == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            pubCredentials == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            privCredentials == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   207
                (ResourcesMgr.getString("invalid.null.input.s."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        this.principals = Collections.synchronizedSet(new SecureSet<Principal>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                (this, PRINCIPAL_SET, principals));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this.pubCredentials = Collections.synchronizedSet(new SecureSet<Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                (this, PUB_CREDENTIAL_SET, pubCredentials));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.privCredentials = Collections.synchronizedSet(new SecureSet<Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                (this, PRIV_CREDENTIAL_SET, privCredentials));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.readOnly = readOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Set this <code>Subject</code> to be read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <p> Modifications (additions and removals) to this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <code>Principal</code> <code>Set</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * credential Sets will be disallowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * The <code>destroy</code> operation on this Subject's credentials will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * still be permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p> Subsequent attempts to modify the Subject's <code>Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * and credential Sets will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>IllegalStateException</code> being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Also, once a <code>Subject</code> is read-only,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * it can not be reset to being writable again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          to set this <code>Subject</code> to be read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void setReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   241
            sm.checkPermission(AuthPermissionHolder.SET_READ_ONLY_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.readOnly = true;
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
     * Query whether this <code>Subject</code> is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return true if this <code>Subject</code> is read-only, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public boolean isReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return this.readOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Get the <code>Subject</code> associated with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p> The <code>AccessControlContext</code> may contain many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Subjects (from nested <code>doAs</code> calls).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * In this situation, the most recent <code>Subject</code> associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * with the <code>AccessControlContext</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param  acc the <code>AccessControlContext</code> from which to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          the <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return  the <code>Subject</code> associated with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          <code>AccessControlContext</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          if no <code>Subject</code> is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          with the provided <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          to get the <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @exception NullPointerException if the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          <code>AccessControlContext</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static Subject getSubject(final AccessControlContext acc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   287
            sm.checkPermission(AuthPermissionHolder.GET_SUBJECT_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (acc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new NullPointerException(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   292
                ("invalid.null.AccessControlContext.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // return the Subject from the DomainCombiner of the provided context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            (new java.security.PrivilegedAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            public Subject run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                DomainCombiner dc = acc.getDomainCombiner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (!(dc instanceof SubjectDomainCombiner))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                SubjectDomainCombiner sdc = (SubjectDomainCombiner)dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                return sdc.getSubject();
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
     * Perform work as a particular <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p> This method first retrieves the current Thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code>AccessControlContext</code> via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>AccessController.getContext</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * and then instantiates a new <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * using the retrieved context along with a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <code>SubjectDomainCombiner</code> (constructed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the provided <code>Subject</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Finally, this method invokes <code>AccessController.doPrivileged</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * passing it the provided <code>PrivilegedAction</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * as well as the newly constructed <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param subject the <code>Subject</code> that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *                  <code>action</code> will run as.  This parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                  may be <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param action the code to be run as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *                  <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return the value returned by the PrivilegedAction's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                  <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @exception NullPointerException if the <code>PrivilegedAction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *                  is <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *                  to invoke this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static <T> T doAs(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        final java.security.PrivilegedAction<T> action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   345
            sm.checkPermission(AuthPermissionHolder.DO_AS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   349
                (ResourcesMgr.getString("invalid.null.action.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // set up the new Subject-based AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // for doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        final AccessControlContext currentAcc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // call doPrivileged and push this new context on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                        (action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                        createContext(subject, currentAcc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Perform work as a particular <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <p> This method first retrieves the current Thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <code>AccessControlContext</code> via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <code>AccessController.getContext</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * and then instantiates a new <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * using the retrieved context along with a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <code>SubjectDomainCombiner</code> (constructed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the provided <code>Subject</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Finally, this method invokes <code>AccessController.doPrivileged</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * passing it the provided <code>PrivilegedExceptionAction</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * as well as the newly constructed <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param subject the <code>Subject</code> that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *                  <code>action</code> will run as.  This parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *                  may be <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param action the code to be run as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *                  <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return the value returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *                  PrivilegedExceptionAction's <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @exception PrivilegedActionException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *                  <code>PrivilegedExceptionAction.run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *                  method throws a checked exception. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @exception NullPointerException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *                  <code>PrivilegedExceptionAction</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *                  <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *                  to invoke this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public static <T> T doAs(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        final java.security.PrivilegedExceptionAction<T> action)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        throws java.security.PrivilegedActionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   404
            sm.checkPermission(AuthPermissionHolder.DO_AS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   409
                (ResourcesMgr.getString("invalid.null.action.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // set up the new Subject-based AccessControlContext for doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        final AccessControlContext currentAcc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // call doPrivileged and push this new context on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                        (action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                        createContext(subject, currentAcc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Perform privileged work as a particular <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p> This method behaves exactly as <code>Subject.doAs</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * except that instead of retrieving the current Thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <code>AccessControlContext</code>, it uses the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <code>AccessControlContext</code>.  If the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <code>AccessControlContext</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * this method instantiates a new <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * with an empty collection of ProtectionDomains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param subject the <code>Subject</code> that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *                  <code>action</code> will run as.  This parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *                  may be <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param action the code to be run as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *                  <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param acc the <code>AccessControlContext</code> to be tied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *                  specified <i>subject</i> and <i>action</i>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return the value returned by the PrivilegedAction's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *                  <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @exception NullPointerException if the <code>PrivilegedAction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *                  is <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *                  to invoke this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public static <T> T doAsPrivileged(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        final java.security.PrivilegedAction<T> action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        final java.security.AccessControlContext acc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   458
            sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   463
                (ResourcesMgr.getString("invalid.null.action.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // set up the new Subject-based AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // for doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        final AccessControlContext callerAcc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                (acc == null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                new AccessControlContext(NULL_PD_ARRAY) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // call doPrivileged and push this new context on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                        (action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                        createContext(subject, callerAcc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Perform privileged work as a particular <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p> This method behaves exactly as <code>Subject.doAs</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * except that instead of retrieving the current Thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <code>AccessControlContext</code>, it uses the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>AccessControlContext</code>.  If the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>AccessControlContext</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * this method instantiates a new <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * with an empty collection of ProtectionDomains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param subject the <code>Subject</code> that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *                  <code>action</code> will run as.  This parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *                  may be <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param action the code to be run as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *                  <code>Subject</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param acc the <code>AccessControlContext</code> to be tied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *                  specified <i>subject</i> and <i>action</i>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return the value returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *                  PrivilegedExceptionAction's <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @exception PrivilegedActionException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *                  <code>PrivilegedExceptionAction.run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *                  method throws a checked exception. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @exception NullPointerException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *                  <code>PrivilegedExceptionAction</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *                  <code>null</code>. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *                  to invoke this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public static <T> T doAsPrivileged(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        final java.security.PrivilegedExceptionAction<T> action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        final java.security.AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        throws java.security.PrivilegedActionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (sm != null) {
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
   522
            sm.checkPermission(AuthPermissionHolder.DO_AS_PRIVILEGED_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   527
                (ResourcesMgr.getString("invalid.null.action.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // set up the new Subject-based AccessControlContext for doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        final AccessControlContext callerAcc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                (acc == null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                new AccessControlContext(NULL_PD_ARRAY) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // call doPrivileged and push this new context on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                        (action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                        createContext(subject, callerAcc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private static AccessControlContext createContext(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                                        final AccessControlContext acc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            (new java.security.PrivilegedAction<AccessControlContext>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            public AccessControlContext run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (subject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    return new AccessControlContext(acc, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    return new AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                        (acc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                        new SubjectDomainCombiner(subject));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Return the <code>Set</code> of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <code>Subject</code>.  Each <code>Principal</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * an identity for this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <p> The returned <code>Set</code> is backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * internal <code>Principal</code> <code>Set</code>.  Any modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * to the returned <code>Set</code> affects the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <code>Principal</code> <code>Set</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @return  The <code>Set</code> of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *          <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public Set<Principal> getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Return a <code>Set</code> of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <code>Subject</code> that are instances or subclasses of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p> The returned <code>Set</code> is not backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * internal <code>Principal</code> <code>Set</code>.  A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <code>Set</code> is created and returned for each method invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Modifications to the returned <code>Set</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * will not affect the internal <code>Principal</code> <code>Set</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param c the returned <code>Set</code> of Principals will all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *          instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return a <code>Set</code> of Principals that are instances of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *          specified <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @exception NullPointerException if the specified <code>Class</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *                  is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public <T extends Principal> Set<T> getPrincipals(Class<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   606
                (ResourcesMgr.getString("invalid.null.Class.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return new ClassSet<T>(PRINCIPAL_SET, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Return the <code>Set</code> of public credentials held by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <p> The returned <code>Set</code> is backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * internal public Credential <code>Set</code>.  Any modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * to the returned <code>Set</code> affects the internal public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Credential <code>Set</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @return  A <code>Set</code> of public credentials held by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *          <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public Set<Object> getPublicCredentials() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return pubCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Return the <code>Set</code> of private credentials held by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <p> The returned <code>Set</code> is backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * internal private Credential <code>Set</code>.  Any modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * to the returned <code>Set</code> affects the internal private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Credential <code>Set</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <p> A caller requires permissions to access the Credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * in the returned <code>Set</code>, or to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <code>Set</code> itself.  A <code>SecurityException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * is thrown if the caller does not have the proper permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <p> While iterating through the <code>Set</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * a <code>SecurityException</code> is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * if the caller does not have permission to access a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * particular Credential.  The <code>Iterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * is nevertheless advanced to next element in the <code>Set</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @return  A <code>Set</code> of private credentials held by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *          <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public Set<Object> getPrivateCredentials() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // we do not need a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        // AuthPermission(getPrivateCredentials)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // because we already restrict access to private credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // via the PrivateCredentialPermission.  all the extra AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // would do is protect the set operations themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // (like size()), which don't seem security-sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return privCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Return a <code>Set</code> of public credentials associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <code>Subject</code> that are instances or subclasses of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p> The returned <code>Set</code> is not backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * internal public Credential <code>Set</code>.  A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <code>Set</code> is created and returned for each method invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Modifications to the returned <code>Set</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * will not affect the internal public Credential <code>Set</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param c the returned <code>Set</code> of public credentials will all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *          instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @return a <code>Set</code> of public credentials that are instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *          of the  specified <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @exception NullPointerException if the specified <code>Class</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *          is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public <T> Set<T> getPublicCredentials(Class<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   700
                (ResourcesMgr.getString("invalid.null.Class.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return new ClassSet<T>(PUB_CREDENTIAL_SET, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Return a <code>Set</code> of private credentials associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <code>Subject</code> that are instances or subclasses of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <p> The caller must have permission to access all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * requested Credentials, or a <code>SecurityException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * <p> The returned <code>Set</code> is not backed by this Subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * internal private Credential <code>Set</code>.  A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>Set</code> is created and returned for each method invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Modifications to the returned <code>Set</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * will not affect the internal private Credential <code>Set</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param c the returned <code>Set</code> of private credentials will all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *          instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @return a <code>Set</code> of private credentials that are instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *          of the  specified <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @exception NullPointerException if the specified <code>Class</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *          is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public <T> Set<T> getPrivateCredentials(Class<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // we do not need a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        // AuthPermission(getPrivateCredentials)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        // because we already restrict access to private credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // via the PrivateCredentialPermission.  all the extra AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // would do is protect the set operations themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        // (like size()), which don't seem security-sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            throw new NullPointerException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   745
                (ResourcesMgr.getString("invalid.null.Class.provided"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // always return an empty Set instead of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // so LoginModules can add to the Set if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return new ClassSet<T>(PRIV_CREDENTIAL_SET, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Compares the specified Object with this <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * for equality.  Returns true if the given object is also a Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * and the two <code>Subject</code> instances are equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * More formally, two <code>Subject</code> instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * equal if their <code>Principal</code> and <code>Credential</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param o Object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *          <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return true if the specified Object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *          to access the private credentials for this <code>Subject</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          or if the caller does not have permission to access the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *          private credentials for the provided <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (this == o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (o instanceof Subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            final Subject that = (Subject)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            // check the principal and credential sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            Set<Principal> thatPrincipals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            synchronized(that.principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                // avoid deadlock from dual locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                thatPrincipals = new HashSet<Principal>(that.principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (!principals.equals(thatPrincipals)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            Set<Object> thatPubCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            synchronized(that.pubCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                // avoid deadlock from dual locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                thatPubCredentials = new HashSet<Object>(that.pubCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (!pubCredentials.equals(thatPubCredentials)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            Set<Object> thatPrivCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            synchronized(that.privCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                // avoid deadlock from dual locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                thatPrivCredentials = new HashSet<Object>(that.privCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (!privCredentials.equals(thatPrivCredentials)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Return the String representation of this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @return the String representation of this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return toString(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * package private convenience method to print out the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * without firing off a security check when trying to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * the Private Credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    String toString(boolean includePrivateCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   835
        String s = ResourcesMgr.getString("Subject.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        String suffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        synchronized(principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            Iterator<Principal> pI = principals.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            while (pI.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                Principal p = pI.next();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   842
                suffix = suffix + ResourcesMgr.getString(".Principal.") +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   843
                        p.toString() + ResourcesMgr.getString("NEWLINE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        synchronized(pubCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            Iterator<Object> pI = pubCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            while (pI.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                Object o = pI.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                suffix = suffix +
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   852
                        ResourcesMgr.getString(".Public.Credential.") +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   853
                        o.toString() + ResourcesMgr.getString("NEWLINE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (includePrivateCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            synchronized(privCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                Iterator<Object> pI = privCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                while (pI.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        Object o = pI.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        suffix += ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   864
                                        (".Private.Credential.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                                        o.toString() +
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   866
                                        ResourcesMgr.getString("NEWLINE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        suffix += ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   869
                                (".Private.Credential.inaccessible.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return s + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Returns a hashcode for this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @return a hashcode for this <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *          to access this Subject's private credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * The hashcode is derived exclusive or-ing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * hashcodes of this Subject's Principals and credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * If a particular credential was destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         * (<code>credential.hashCode()</code> throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
         * <code>IllegalStateException</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         * the hashcode for that credential is derived via:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         * <code>credential.getClass().toString().hashCode()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        synchronized(principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            Iterator<Principal> pIterator = principals.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            while (pIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                Principal p = pIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                hashCode ^= p.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        synchronized(pubCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            Iterator<Object> pubCIterator = pubCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            while (pubCIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                hashCode ^= getCredHashCode(pubCIterator.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * get a credential's hashcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    private int getCredHashCode(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            return o.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        } catch (IllegalStateException ise) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return o.getClass().toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Writes this object out to a stream (i.e., serializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    private void writeObject(java.io.ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        synchronized(principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            oos.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Reads this object from a stream (i.e., deserializes it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // The Credential <code>Set</code> is not serialized, but we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        // want the default deserialization routine to set it to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        this.pubCredentials = Collections.synchronizedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        (new SecureSet<Object>(this, PUB_CREDENTIAL_SET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        this.privCredentials = Collections.synchronizedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        (new SecureSet<Object>(this, PRIV_CREDENTIAL_SET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * Prevent modifications unless caller has permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    private static class SecureSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        private static final long serialVersionUID = 7911754171111800359L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         * @serialField this$0 Subject The outer Subject instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         * @serialField elements LinkedList The elements in this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            new ObjectStreamField("this$0", Subject.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            new ObjectStreamField("elements", LinkedList.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            new ObjectStreamField("which", int.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        LinkedList<E> elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         * @serial An integer identifying the type of objects contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         *      in this set.  If <code>which == 1</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         *      this is a Principal set and all the elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         *      of type <code>java.security.Principal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         *      If <code>which == 2</code>, this is a public credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         *      set and all the elements are of type <code>Object</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         *      If <code>which == 3</code>, this is a private credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         *      set and all the elements are of type <code>Object</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        private int which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        SecureSet(Subject subject, int which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            this.which = which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            this.elements = new LinkedList<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        SecureSet(Subject subject, int which, Set<? extends E> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            this.which = which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            this.elements = new LinkedList<E>(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            return elements.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            final LinkedList<E> list = elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                ListIterator<E> i = list.listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                        return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                            sm.checkPermission(new PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                                (list.get(i.nextIndex()).getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                                subject.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                            i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                            throw (se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        throw new IllegalStateException(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1039
                                ("Subject.is.read.only"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                        case Subject.PRINCIPAL_SET:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1046
                            sm.checkPermission(AuthPermissionHolder.MODIFY_PRINCIPALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        case Subject.PUB_CREDENTIAL_SET:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1049
                            sm.checkPermission(AuthPermissionHolder.MODIFY_PUBLIC_CREDENTIALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        default:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1052
                            sm.checkPermission(AuthPermissionHolder.MODIFY_PRIVATE_CREDENTIALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        public boolean add(E o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                throw new IllegalStateException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1065
                        (ResourcesMgr.getString("Subject.is.read.only"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                case Subject.PRINCIPAL_SET:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1072
                    sm.checkPermission(AuthPermissionHolder.MODIFY_PRINCIPALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                case Subject.PUB_CREDENTIAL_SET:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1075
                    sm.checkPermission(AuthPermissionHolder.MODIFY_PUBLIC_CREDENTIALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                default:
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1078
                    sm.checkPermission(AuthPermissionHolder.MODIFY_PRIVATE_CREDENTIALS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            case Subject.PRINCIPAL_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                if (!(o instanceof Principal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    throw new SecurityException(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1087
                        ("attempting.to.add.an.object.which.is.not.an.instance.of.java.security.Principal.to.a.Subject.s.Principal.Set"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                // ok to add Objects of any kind to credential sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // check for duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            if (!elements.contains(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                return elements.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            final Iterator<E> e = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                E next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    next = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        (new java.security.PrivilegedAction<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        public E run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                            return e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                if (next == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                } else if (next.equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            final Iterator<E> e = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                E next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    next = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    // For private credentials:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                    // If the caller does not have read permission for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    // for o.getClass(), we throw a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    // Otherwise we check the private cred set to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    // it contains the Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                        sm.checkPermission(new PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                                                (o.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                                                subject.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                        (new java.security.PrivilegedAction<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                        public E run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                            return e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                if (next == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                } else if (next.equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            final Iterator<E> e = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                E next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    next = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                        (new java.security.PrivilegedAction<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                        public E run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                            return e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                Iterator<?> ce = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                while (ce.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                    Object o = ce.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    if (next == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                            e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                            modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    } else if (next.equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                        e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                        modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            boolean retain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            final Iterator<E> e = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                retain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                E next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    next = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                        (new java.security.PrivilegedAction<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                        public E run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                            return e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                Iterator<?> ce = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                while (ce.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    Object o = ce.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    if (next == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                            retain = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    } else if (next.equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                        retain = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                if (!retain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    retain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            final Iterator<E> e = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                E next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    next = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                        (new java.security.PrivilegedAction<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                        public E run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                            return e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * Writes this object out to a stream (i.e., serializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         * @serialData If this is a private credential set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         *      a security check is performed to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         *      the caller has permission to access each credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         *      in the set.  If the security check passes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         *      the set is serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        private void writeObject(java.io.ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            if (which == Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                // check permissions before serializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                Iterator<E> i = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            ObjectOutputStream.PutField fields = oos.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            fields.put("this$0", subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            fields.put("elements", elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            fields.put("which", which);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            oos.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 7179
diff changeset
  1294
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        private void readObject(ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            ObjectInputStream.GetField fields = ois.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            subject = (Subject) fields.get("this$0", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            which = fields.get("which", 0);
20806
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1301
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1302
            LinkedList<E> tmp = (LinkedList<E>) fields.get("elements", null);
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1303
            if (tmp.getClass() != LinkedList.class) {
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1304
                elements = new LinkedList<E>(tmp);
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1305
            } else {
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1306
                elements = tmp;
d94588b7854d 8015731: Subject java.security.auth.subject to improvements
weijun
parents: 14342
diff changeset
  1307
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * This class implements a <code>Set</code> which returns only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * members that are an instance of a specified Class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    private class ClassSet<T> extends AbstractSet<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        private int which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        private Class<T> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        private Set<T> set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        ClassSet(int which, Class<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            this.which = which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            set = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            case Subject.PRINCIPAL_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                synchronized(principals) { populateSet(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            case Subject.PUB_CREDENTIAL_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                synchronized(pubCredentials) { populateSet(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                synchronized(privCredentials) { populateSet(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 7179
diff changeset
  1339
        @SuppressWarnings("unchecked")     /*To suppress warning from line 1374*/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        private void populateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            final Iterator<?> iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            switch(which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            case Subject.PRINCIPAL_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                iterator = Subject.this.principals.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            case Subject.PUB_CREDENTIAL_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                iterator = Subject.this.pubCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                iterator = Subject.this.privCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            // Check whether the caller has permisson to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            // credentials of Class c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                Object next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                if (which == Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    next = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        (new java.security.PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                            return iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    next = iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                if (c.isAssignableFrom(next.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    if (which != Subject.PRIV_CREDENTIAL_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        set.add((T)next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                        // Check permission for private creds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                            sm.checkPermission(new PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                                (next.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                                Subject.this.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                        set.add((T)next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            return set.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        public Iterator<T> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            return set.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        public boolean add(T o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            if (!o.getClass().isAssignableFrom(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1398
                        ("attempting.to.add.an.object.which.is.not.an.instance.of.class"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                Object[] source = {c.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                throw new SecurityException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            return set.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    }
4331
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1406
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1407
    static class AuthPermissionHolder {
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1408
        static final AuthPermission DO_AS_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1409
            new AuthPermission("doAs");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1410
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1411
        static final AuthPermission DO_AS_PRIVILEGED_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1412
            new AuthPermission("doAsPrivileged");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1413
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1414
        static final AuthPermission SET_READ_ONLY_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1415
            new AuthPermission("setReadOnly");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1416
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1417
        static final AuthPermission GET_SUBJECT_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1418
            new AuthPermission("getSubject");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1419
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1420
        static final AuthPermission MODIFY_PRINCIPALS_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1421
            new AuthPermission("modifyPrincipals");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1422
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1423
        static final AuthPermission MODIFY_PUBLIC_CREDENTIALS_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1424
            new AuthPermission("modifyPublicCredentials");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1425
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1426
        static final AuthPermission MODIFY_PRIVATE_CREDENTIALS_PERMISSION =
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1427
            new AuthPermission("modifyPrivateCredentials");
ba7050c934f2 6903638: Remove dependency on AuthPermission from SecurityConstants
mchung
parents: 2
diff changeset
  1428
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
}