jdk/src/share/classes/java/security/UnresolvedPermission.java
author smarks
Fri, 07 Jan 2011 15:39:58 -0800
changeset 7970 af1579474d16
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
permissions -rw-r--r--
7008728: diamond conversion of basic security, permissions, authentication Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The UnresolvedPermission class is used to hold Permissions that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * were "unresolved" when the Policy was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An unresolved permission is one whose actual Permission class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * does not yet exist at the time the Policy is initialized (see below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>The policy for a Java runtime (specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * which permissions are available for code from various principals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * is represented by a Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Whenever a Policy is initialized or refreshed, Permission objects of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * appropriate classes are created for all permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * allowed by the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>Many permission class types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * referenced by the policy configuration are ones that exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * locally (i.e., ones that can be found on CLASSPATH).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Objects for such permissions can be instantiated during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Policy initialization. For example, it is always possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to instantiate a java.io.FilePermission, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * FilePermission class is found on the CLASSPATH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>Other permission classes may not yet exist during Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * initialization. For example, a referenced permission class may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * be in a JAR file that will later be loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * For each such class, an UnresolvedPermission is instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Thus, an UnresolvedPermission is essentially a "placeholder"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * containing information about the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>Later, when code calls AccessController.checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * on a permission of a type that was previously unresolved,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * but whose class has since been loaded, previously-unresolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * permissions of that type are "resolved". That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * for each such UnresolvedPermission, a new object of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the appropriate class type is instantiated, based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * information in the UnresolvedPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p> To instantiate the new class, UnresolvedPermission assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * the class provides a zero, one, and/or two-argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * The zero-argument constructor would be used to instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * a permission without a name and without actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * A one-arg constructor is assumed to take a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * name as input, and a two-arg constructor is assumed to take a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>String</code> name and <code>String</code> actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * as input.  UnresolvedPermission may invoke a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * constructor with a <code>null</code> name and/or actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * If an appropriate permission constructor is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the UnresolvedPermission is ignored and the relevant permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * will not be granted to executing code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p> The newly created permission object replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * UnresolvedPermission, which is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p> Note that the <code>getName</code> method for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <code>UnresolvedPermission</code> returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <code>type</code> (class name) for the underlying permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * that has not been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see java.security.Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
public final class UnresolvedPermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final long serialVersionUID = -4821973115467008846L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        sun.security.util.Debug.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ("policy,access", "UnresolvedPermission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The class name of the Permission class that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * created when this unresolved permission is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * The permission name.
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 String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The actions of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private String actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private transient java.security.cert.Certificate certs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Creates a new UnresolvedPermission containing the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * information needed later to actually create a Permission of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * specified class, when the permission is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param type the class name of the Permission class that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * created when this unresolved permission is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param name the name of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param actions the actions of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param certs the certificates the permission's class was signed with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * This is a list of certificate chains, where each chain is composed of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * signer certificate and optionally its supporting certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Each chain is ordered bottom-to-top (i.e., with the signer certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * first and the (root) certificate authority last). The signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * certificates are copied from the array. Subsequent changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the array will not affect this UnsolvedPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public UnresolvedPermission(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                String actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                java.security.cert.Certificate certs[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        super(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (type == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw new NullPointerException("type can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.actions = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // Extract the signer certs from the list of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (!(certs[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    // there is no concept of signer certs, so we store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    // entire cert array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (this.certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // Go through the list of certs and see if all the certs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // signer certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    while (((i+1) < certs.length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                           ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (count == certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    // All the certs are signer certs, so we store the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    // array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (this.certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // extract the signer certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    ArrayList<java.security.cert.Certificate> signerCerts =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   201
                        new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        signerCerts.add(certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        while (((i+1) < certs.length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                              ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    this.certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        new java.security.cert.Certificate[signerCerts.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    signerCerts.toArray(this.certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static final Class[] PARAMS0 = { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private static final Class[] PARAMS1 = { String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static final Class[] PARAMS2 = { String.class, String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * try and resolve this permission using the class loader of the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * that was passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    Permission resolve(Permission p, java.security.cert.Certificate certs[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (this.certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // if p wasn't signed, we don't have a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // all certs in this.certs must be present in certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            for (int i = 0; i < this.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (int j = 0; j < certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (this.certs[i].equals(certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (!match) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            Class pc = p.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (name == null && actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    Constructor c = pc.getConstructor(PARAMS0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    return (Permission)c.newInstance(new Object[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                } catch (NoSuchMethodException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        Constructor c = pc.getConstructor(PARAMS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                              new Object[] { name});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    } catch (NoSuchMethodException ne1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        Constructor c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                              new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (name != null && actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        Constructor c = pc.getConstructor(PARAMS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                              new Object[] { name});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    } catch (NoSuchMethodException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        Constructor c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                              new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    Constructor c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                          new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (debug != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                debug.println("NoSuchMethodException:\n  could not find " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        "proper constructor for " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                nsme.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (debug != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                debug.println("unable to instantiate " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
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
     * This method always returns false for unresolved permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * That is, an UnresolvedPermission is never considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * imply another permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Checks two UnresolvedPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Checks that <i>obj</i> is an UnresolvedPermission, and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the same type (class) name, permission name, actions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * certificates as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p> To determine certificate equality, this method only compares
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * actual signer certificates.  Supporting certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * are not taken into consideration by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return true if obj is an UnresolvedPermission, and has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * type (class) name, permission name, actions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * certificates as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (! (obj instanceof UnresolvedPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        UnresolvedPermission that = (UnresolvedPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // check type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (!this.type.equals(that.type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // check name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (this.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (that.name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else if (!this.name.equals(that.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // check actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (this.actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (that.actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (!this.actions.equals(that.actions)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return false;
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
        // check certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if ((this.certs == null && that.certs != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            (this.certs != null && that.certs == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            (this.certs != null && that.certs != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                this.certs.length != that.certs.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        for (i = 0; this.certs != null && i < this.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            for (j = 0; j < that.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if (this.certs[i].equals(that.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (i = 0; that.certs != null && i < that.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            for (j = 0; j < this.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (that.certs[i].equals(this.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int hash = type.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            hash ^= name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (actions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            hash ^= actions.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Returns the canonical string representation of the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * which currently is the empty string "", since there are no actions for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * an UnresolvedPermission. That is, the actions for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * permission that will be created when this UnresolvedPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * is resolved may be non-null, but an UnresolvedPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * itself is never considered to have any actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public String getActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Get the type (class name) of the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * has not been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return the type (class name) of the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *  has not been resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public String getUnresolvedType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Get the target name of the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * has not been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @return the target name of the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *          has not been resolved, or <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *          if there is no targe name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public String getUnresolvedName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Get the actions for the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * has not been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return the actions for the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *          has not been resolved, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *          if there are no actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public String getUnresolvedActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Get the signer certificates (without any supporting chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * for the underlying permission that has not been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @return the signer certificates for the underlying permission that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * has not been resolved, or null, if there are no signer certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public java.security.cert.Certificate[] getUnresolvedCerts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return (certs == null) ? null : certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Returns a string describing this UnresolvedPermission.  The convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * is to specify the class name, the permission name, and the actions, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * the following format: '(unresolved "ClassName" "name" "actions")'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @return information about this UnresolvedPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return "(unresolved " + type + " " + name + " " + actions + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Returns a new PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * UnresolvedPermission  objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * storing UnresolvedPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return new UnresolvedPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Writes this object out to a stream (i.e., serializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @serialData An initial <code>String</code> denoting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <code>type</code> is followed by a <code>String</code> denoting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <code>name</code> is followed by a <code>String</code> denoting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <code>actions</code> is followed by an <code>int</code> indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * number of certificates to follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * (a value of "zero" denotes that there are no certificates associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * with this object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Each certificate is written out starting with a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * denoting the certificate type, followed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <code>int</code> specifying the length of the certificate encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * followed by the certificate encoding itself which is written out as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private void writeObject(java.io.ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        oos.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (certs==null || certs.length==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            oos.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            // write out the total number of certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            oos.writeInt(certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // write out each cert, including its type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            for (int i=0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                java.security.cert.Certificate cert = certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    oos.writeUTF(cert.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    oos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    oos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                } catch (CertificateEncodingException cee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    throw new IOException(cee.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
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
     * Restores this object from a stream (i.e., deserializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    private void readObject(java.io.ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        CertificateFactory cf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        Hashtable<String, CertificateFactory> cfs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        ois.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (type == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                throw new NullPointerException("type can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // process any new-style certs in the stream (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        int size = ois.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            // we know of 3 different cert types: X.509, PGP, SDSI, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            // could all be present in the stream at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            cfs = new Hashtable<String, CertificateFactory>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            this.certs = new java.security.cert.Certificate[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            // read the certificate type, and instantiate a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            // factory of that type (reuse existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            String certType = ois.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                // reuse certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                cf = cfs.get(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                // create new certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    cf = CertificateFactory.getInstance(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    throw new ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        ("Certificate factory for "+certType+" not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // store the certificate factory so we can reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // parse the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            byte[] encoded=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                encoded = new byte[ois.readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            } catch (OutOfMemoryError oome) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                throw new IOException("Certificate too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            ois.readFully(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                this.certs[i] = cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                throw new IOException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
}