jdk/src/share/classes/javax/security/auth/PrivateCredentialPermission.java
author xuelei
Wed, 28 Sep 2011 15:10:02 -0700
changeset 10709 d865c9f21240
parent 9035 1255eb81cc2f
child 18830 90956ead732f
permissions -rw-r--r--
7092375: Security Libraries don't build with javac -Werror Summary: Changes to security related java and make files to remove warnings Reviewed-by: xuelei Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 1999, 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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.ResourcesMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class is used to protect access to private Credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * belonging to a particular <code>Subject</code>.  The <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * is represented by a Set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> The target name of this <code>Permission</code> specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a Credential class name, and a Set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The only valid value for this Permission's actions is, "read".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The target name must abide by the following syntax:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      CredentialClass {PrincipalClass "PrincipalName"}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * For example, the following permission grants access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * com.sun.PrivateCredential owned by Subjects which have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a com.sun.Principal with the name, "duke".  Note that although
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * this example, as well as all the examples below, do not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Codebase, SignedBy, or Principal information in the grant statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * (for simplicity reasons), actual policy configurations should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * specify that information when appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    grant {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *              "com.sun.PrivateCredential com.sun.Principal \"duke\"",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * If CredentialClass is "*", then access is granted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * all private Credentials belonging to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * If "PrincipalName" is "*", then access is granted to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * specified Credential owned by any <code>Subject</code> that has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * specified <code>Principal</code> (the actual PrincipalName doesn't matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * For example, the following grants access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * a.b.Credential owned by any <code>Subject</code> that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * an a.b.Principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    grant {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *              "a.b.Credential a.b.Principal "*"",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * If both the PrincipalClass and "PrincipalName" are "*",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * then access is granted to the specified Credential owned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * any <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p> In addition, the PrincipalClass/PrincipalName pairing may be repeated:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *    grant {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *              "a.b.Credential a.b.Principal "duke" c.d.Principal "dukette"",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * The above grants access to the private Credential, "a.b.Credential",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * belonging to a <code>Subject</code> with at least two associated Principals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * "a.b.Principal" with the name, "duke", and "c.d.Principal", with the name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * "dukette".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
public final class PrivateCredentialPermission extends Permission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final long serialVersionUID = 5284372143517237068L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final CredOwner[] EMPTY_PRINCIPALS = new CredOwner[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private String credentialClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @serial The Principals associated with this permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          The set contains elements of type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          <code>PrivateCredentialPermission.CredOwner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   120
    private Set<Principal> principals;  // ignored - kept around for compatibility
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private transient CredOwner[] credOwners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private boolean testing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Create a new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * with the specified <code>credentialClass</code> and Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    PrivateCredentialPermission(String credentialClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        Set<Principal> principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        super(credentialClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.credentialClass = credentialClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        synchronized(principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (principals.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                this.credOwners = EMPTY_PRINCIPALS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                this.credOwners = new CredOwner[principals.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                Iterator<Principal> i = principals.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    Principal p = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    this.credOwners[index++] = new CredOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                (p.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Creates a new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * with the specified <code>name</code>.  The <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * specifies both a Credential class and a <code>Principal</code> Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param name the name specifying the Credential class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          <code>Principal</code> Set. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param actions the actions specifying that the Credential can be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws IllegalArgumentException if <code>name</code> does not conform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *          to the correct syntax or if <code>actions</code> is not "read".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public PrivateCredentialPermission(String name, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (!"read".equalsIgnoreCase(actions))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new IllegalArgumentException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   175
                (ResourcesMgr.getString("actions.can.only.be.read."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns the Class name of the Credential associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>PrivateCredentialPermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return the Class name of the Credential associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *          <code>PrivateCredentialPermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public String getCredentialClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return credentialClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns the <code>Principal</code> classes and names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * associated with this <code>PrivateCredentialPermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The information is returned as a two-dimensional array (array[x][y]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The 'x' value corresponds to the number of <code>Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * class and name pairs.  When (y==0), it corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * the <code>Principal</code> class value, and when (y==1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * it corresponds to the <code>Principal</code> name value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * For example, array[0][0] corresponds to the class name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * the first <code>Principal</code> in the array.  array[0][1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * corresponds to the <code>Principal</code> name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * first <code>Principal</code> in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the <code>Principal</code> class and names associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          with this <code>PrivateCredentialPermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public String[][] getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (credOwners == null || credOwners.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return new String[0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        String[][] pArray = new String[credOwners.length][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (int i = 0; i < credOwners.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            pArray[i][0] = credOwners[i].principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            pArray[i][1] = credOwners[i].principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return pArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Checks if this <code>PrivateCredentialPermission</code> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * the specified <code>Permission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * This method returns true if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <li> <i>p</i> is an instanceof PrivateCredentialPermission and <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <li> the target name for <i>p</i> is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *          target name.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *  [* P1 "duke"] implies [a.b.Credential P1 "duke"].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *  [C1 P1 "duke"] implies [C1 P1 "duke" P2 "dukette"].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *  [C1 P2 "dukette"] implies [C1 P1 "duke" P2 "dukette"].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param p the <code>Permission</code> to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return true if this <code>PrivateCredentialPermission</code> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the specified <code>Permission</code>, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (p == null || !(p instanceof PrivateCredentialPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        PrivateCredentialPermission that = (PrivateCredentialPermission)p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (!impliesCredentialClass(credentialClass, that.credentialClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return impliesPrincipalSet(credOwners, that.credOwners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Checks two <code>PrivateCredentialPermission</code> objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * equality.  Checks that <i>obj</i> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <code>PrivateCredentialPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * and has the same credential class as this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * as well as the same Principals as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The order of the Principals in the respective Permission's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * target names is not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return true if obj is a <code>PrivateCredentialPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          has the same credential class as this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          and has the same Principals as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (! (obj instanceof PrivateCredentialPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        PrivateCredentialPermission that = (PrivateCredentialPermission)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return (this.implies(that) && that.implies(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return this.credentialClass.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Returns the "canonical string representation" of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * This method always returns the String, "read".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return the actions (always returns "read").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Return a homogeneous collection of PrivateCredentialPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * in a <code>PermissionCollection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * No such <code>PermissionCollection</code> is defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * so this method always returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return null in all cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private void init(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (name == null || name.trim().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new IllegalArgumentException("invalid empty name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7179
diff changeset
   332
        ArrayList<CredOwner> pList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        StringTokenizer tokenizer = new StringTokenizer(name, " ", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        String principalClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        String principalName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            System.out.println("whole name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // get the Credential Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        credentialClass = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            System.out.println("Credential Class = " + credentialClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (tokenizer.hasMoreTokens() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   347
                ("permission.name.name.syntax.invalid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                (form.format(source) + ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   351
                        ("Credential.Class.not.followed.by.a.Principal.Class.and.Name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // skip delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // get the Principal Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            principalClass = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                System.out.println("    Principal Class = " + principalClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (tokenizer.hasMoreTokens() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   366
                        ("permission.name.name.syntax.invalid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        (form.format(source) + ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   370
                        ("Principal.Class.not.followed.by.a.Principal.Name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // skip delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // get the Principal Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            principalName = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (!principalName.startsWith("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   381
                        ("permission.name.name.syntax.invalid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        (form.format(source) + ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   385
                        ("Principal.Name.must.be.surrounded.by.quotes"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (!principalName.endsWith("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // we have a name with spaces in it --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // keep parsing until we find the end quote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // and keep the spaces in the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    principalName = principalName + tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    if (principalName.endsWith("\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (!principalName.endsWith("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   403
                        ("permission.name.name.syntax.invalid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        (form.format(source) + ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   407
                                ("Principal.Name.missing.end.quote"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                System.out.println("\tprincipalName = '" + principalName + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            principalName = principalName.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                        (1, principalName.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (principalClass.equals("*") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                !principalName.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    throw new IllegalArgumentException(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   420
                        ("PrivateCredentialPermission.Principal.Class.can.not.be.a.wildcard.value.if.Principal.Name.is.not.a.wildcard.value"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                System.out.println("\tprincipalName = '" + principalName + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            pList.add(new CredOwner(principalClass, principalName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        this.credOwners = new CredOwner[pList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        pList.toArray(this.credOwners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private boolean impliesCredentialClass(String thisC, String thatC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (thisC == null || thatC == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (testing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            System.out.println("credential class comparison: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                                thisC + "/" + thatC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (thisC.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * XXX let's not enable this for now --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         *      if people want it, we'll enable it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (thisC.endsWith("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            String cClass = thisC.substring(0, thisC.length() - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return thatC.startsWith(cClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return thisC.equals(thatC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private boolean impliesPrincipalSet(CredOwner[] thisP, CredOwner[] thatP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (thisP == null || thatP == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (thatP.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (thisP.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (int i = 0; i < thisP.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            boolean foundMatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            for (int j = 0; j < thatP.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (thisP[i].implies(thatP[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    foundMatch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (!foundMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Reads this object from a stream (i.e., deserializes it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private void readObject(java.io.ObjectInputStream s) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                        java.io.IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                        ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // perform new initialization from the permission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (getName().indexOf(" ") == -1 && getName().indexOf("\"") == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // name only has a credential class specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            credentialClass = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            credOwners = EMPTY_PRINCIPALS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // perform regular initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    static class CredOwner implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        private static final long serialVersionUID = -5607449830436408266L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        String principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        CredOwner(String principalClass, String principalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            this.principalClass = principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            this.principalName = principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        public boolean implies(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (obj == null || !(obj instanceof CredOwner))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            CredOwner that = (CredOwner)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (principalClass.equals("*") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                principalClass.equals(that.principalClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (principalName.equals("*") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    principalName.equals(that.principalName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
             * XXX no code yet to support a.b.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   556
                ("CredOwner.Principal.Class.class.Principal.Name.name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            Object[] source = {principalClass, principalName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return (form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
}