jdk/src/share/classes/java/security/Provider.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 9035 1255eb81cc2f
child 18579 b678846778ad
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@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) 1996, 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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import static java.util.Locale.ENGLISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class represents a "provider" for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Java Security API, where a provider implements some or all parts of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Java Security. Services that a provider may implement include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <li>Algorithms (such as DSA, RSA, MD5 or SHA-1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>Key generation, conversion, and management facilities (such as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * algorithm-specific keys).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Each provider has a name and a version number, and is configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * in each runtime it is installed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>See <a href =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * "../../../technotes/guides/security/crypto/CryptoSpec.html#Provider">The Provider Class</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * in the "Java Cryptography Architecture API Specification &amp; Reference"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * for information about how a particular type of provider, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * cryptographic service provider, works and is installed. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * please note that a provider can be used to implement any security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * service in Java that uses a pluggable architecture with a choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * of implementations that fit underneath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>Some provider implementations may encounter unrecoverable internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * errors during their operation, for example a failure to communicate with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * security token. A {@link ProviderException} should be used to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * such errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>The service type <code>Provider</code> is reserved for use by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * security framework. Services of this type cannot be added, removed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * or modified by applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * The following attributes are automatically placed in each Provider object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <table cellspacing=4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <tr><th>Name</th><th>Value</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <tr><td><code>Provider.id name</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  *    <td><code>String.valueOf(provider.getName())</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <tr><td><code>Provider.id version</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *     <td><code>String.valueOf(provider.getVersion())</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <tr><td><code>Provider.id info</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       <td><code>String.valueOf(provider.getInfo())</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <tr><td><code>Provider.id className</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     <td><code>provider.getClass().getName()</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public abstract class Provider extends Properties {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Declare serialVersionUID to be compatible with JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final long serialVersionUID = -4298000515446427739L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        sun.security.util.Debug.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ("provider", "Provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The provider name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * A description of the provider and its services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private String info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The provider version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private double version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private transient Set<Map.Entry<Object,Object>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private transient int entrySetCallCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private transient boolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructs a provider with the specified name, version number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * and information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param name the provider name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param version the provider version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param info a description of the provider and its services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected Provider(String name, double version, String info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.version = version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.info = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        putId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns the name of this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the name of this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the version number for this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the version number for this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public double getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns a human-readable description of the provider and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * services.  This may return an HTML page, with relevant links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return a description of the provider and its services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public String getInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns a string with the name and the version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * of this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return the string with the name and the version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * for this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return name + " version " + version;
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
     * override the following methods to ensure that provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * information can only be changed if the caller has the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Clears this provider so that it no longer contains the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * used to look up facilities implemented by the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <code>checkSecurityAccess</code> method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>"clearProviderProperties."+name</code> (where <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * is the provider name) to see if it's ok to clear this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * is used (that is, that method is not overriden), then this results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * a call to the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * with a <code>SecurityPermission("clearProviderProperties."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *          denies access to clear this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public synchronized void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        check("clearProviderProperties."+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            debug.println("Remove " + name + " provider properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        implClear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Reads a property list (key and element pairs) from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param inStream   the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception  IOException  if an error occurred when reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *               input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see java.util.Properties#load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public synchronized void load(InputStream inStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        check("putProviderProperty."+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            debug.println("Load " + name + " provider properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Properties tempProperties = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        tempProperties.load(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        implPutAll(tempProperties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Copies all of the mappings from the specified Map to this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * These mappings will replace any properties that this provider had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * for any of the keys currently in the specified Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public synchronized void putAll(Map<?,?> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        check("putProviderProperty."+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            debug.println("Put all " + name + " provider properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        implPutAll(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns an unmodifiable Set view of the property entries contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * in this Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see   java.util.Map.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized Set<Map.Entry<Object,Object>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (entrySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (entrySetCallCount++ == 0)  // Initial call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                entrySet = Collections.unmodifiableMap(this).entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return super.entrySet();   // Recursive call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // This exception will be thrown if the implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Collections.unmodifiableMap.entrySet() is changed such that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // no longer calls entrySet() on the backing Map.  (Provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // entrySet implementation depends on this "implementation detail",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // which is unlikely to change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (entrySetCallCount != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new RuntimeException("Internal error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns an unmodifiable Set view of the property keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public Set<Object> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return Collections.unmodifiableSet(super.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns an unmodifiable Collection view of the property values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * contained in this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public Collection<Object> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return Collections.unmodifiableCollection(super.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Sets the <code>key</code> property to have the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <code>checkSecurityAccess</code> method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <code>"putProviderProperty."+name</code>, where <code>name</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * provider name, to see if it's ok to set this provider's property values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * is used (that is, that method is not overriden), then this results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * a call to the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * with a <code>SecurityPermission("putProviderProperty."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param key the property key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param value the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return the previous value of the specified property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * (<code>key</code>), or null if it did not have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          denies access to set property values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public synchronized Object put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        check("putProviderProperty."+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            debug.println("Set " + name + " provider property [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                          key + "/" + value +"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return implPut(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Removes the <code>key</code> property (and its corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>value</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <code>checkSecurityAccess</code> method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <code>"removeProviderProperty."+name</code>, where <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * the provider name, to see if it's ok to remove this provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * properties. If the default implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <code>checkSecurityAccess</code> is used (that is, that method is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * overriden), then this results in a call to the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>checkPermission</code> method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <code>SecurityPermission("removeProviderProperty."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param key the key for the property to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the value to which the key had been mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * or null if the key did not have a mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *          denies access to remove this provider's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public synchronized Object remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        check("removeProviderProperty."+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            debug.println("Remove " + name + " provider property " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return implRemove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // let javadoc show doc from superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // let javadoc show doc from superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public Enumeration<Object> keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return super.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // let javadoc show doc from superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public Enumeration<Object> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return super.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    // let javadoc show doc from superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public String getProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return super.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private void checkInitialized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private void check(String directive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            security.checkSecurityAccess(directive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    // legacy properties changed since last call to any services method?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private transient boolean legacyChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // serviceMap changed since last call to getServices()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    private transient boolean servicesChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // Map<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private transient Map<String,String> legacyStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    // Map<ServiceKey,Service>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // used for services added via putService(), initialized on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private transient Map<ServiceKey,Service> serviceMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    // Map<ServiceKey,Service>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    // used for services added via legacy methods, init on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private transient Map<ServiceKey,Service> legacyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    // Set<Service>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    // Unmodifiable set of all services. Initialized on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private transient Set<Service> serviceSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    // register the id attributes for this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // this is to ensure that equals() and hashCode() do not incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // report to different provider objects as the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private void putId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // note: name and info may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        super.put("Provider.id name", String.valueOf(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        super.put("Provider.id version", String.valueOf(version));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        super.put("Provider.id info", String.valueOf(info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        super.put("Provider.id className", this.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                throws IOException, ClassNotFoundException {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   437
        Map<Object,Object> copy = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        for (Map.Entry<Object,Object> entry : super.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            copy.put(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        defaults = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        implClear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        putAll(copy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Copies all of the mappings from the specified Map to this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Internal method to be called AFTER the security check has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   453
    private void implPutAll(Map<?,?> t) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   454
        for (Map.Entry<?,?> e : t.entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            implPut(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private Object implRemove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (key instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            String keyString = (String)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (keyString.startsWith("Provider.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            legacyChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (legacyStrings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                legacyStrings = new LinkedHashMap<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            legacyStrings.remove(keyString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return super.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private Object implPut(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if ((key instanceof String) && (value instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            String keyString = (String)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (keyString.startsWith("Provider.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            legacyChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (legacyStrings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                legacyStrings = new LinkedHashMap<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            legacyStrings.put(keyString, (String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return super.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private void implClear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (legacyStrings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            legacyStrings.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (legacyMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            legacyMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (serviceMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            serviceMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        legacyChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        servicesChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        putId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    // used as key in the serviceMap and legacyMap HashMaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private static class ServiceKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        private final String originalAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        private ServiceKey(String type, String algorithm, boolean intern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            this.originalAlgorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            algorithm = algorithm.toUpperCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            this.algorithm = intern ? algorithm.intern() : algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return type.hashCode() + algorithm.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (obj instanceof ServiceKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            ServiceKey other = (ServiceKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return this.type.equals(other.type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                && this.algorithm.equals(other.algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        boolean matches(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            return (this.type == type) && (this.originalAlgorithm == algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Ensure all the legacy String properties are fully parsed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * service objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    private void ensureLegacyParsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if ((legacyChanged == false) || (legacyStrings == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (legacyMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            legacyMap = new LinkedHashMap<ServiceKey,Service>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            legacyMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        for (Map.Entry<String,String> entry : legacyStrings.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            parseLegacyPut(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        removeInvalidServices(legacyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        legacyChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Remove all invalid services from the Map. Invalid services can only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * occur if the legacy properties are inconsistent or incomplete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private void removeInvalidServices(Map<ServiceKey,Service> map) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   562
        for (Iterator<Map.Entry<ServiceKey, Service>> t =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   563
                map.entrySet().iterator(); t.hasNext(); ) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   564
            Service s = t.next().getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (s.isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                t.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private String[] getTypeAndAlgorithm(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int i = key.indexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (i < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                debug.println("Ignoring invalid entry in provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        + name + ":" + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        String type = key.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        String alg = key.substring(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return new String[] {type, alg};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    private final static String ALIAS_PREFIX = "Alg.Alias.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    private final static String ALIAS_PREFIX_LOWER = "alg.alias.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    private final static int ALIAS_LENGTH = ALIAS_PREFIX.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private void parseLegacyPut(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (name.toLowerCase(ENGLISH).startsWith(ALIAS_PREFIX_LOWER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            // e.g. put("Alg.Alias.MessageDigest.SHA", "SHA-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // aliasKey ~ MessageDigest.SHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            String stdAlg = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            String aliasKey = name.substring(ALIAS_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            String[] typeAndAlg = getTypeAndAlgorithm(aliasKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (typeAndAlg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            String type = getEngineName(typeAndAlg[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            String aliasAlg = typeAndAlg[1].intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            ServiceKey key = new ServiceKey(type, stdAlg, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            Service s = legacyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                s = new Service(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                s.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                s.algorithm = stdAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                legacyMap.put(key, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            legacyMap.put(new ServiceKey(type, aliasAlg, true), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            s.addAlias(aliasAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            String[] typeAndAlg = getTypeAndAlgorithm(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (typeAndAlg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            int i = typeAndAlg[1].indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                // e.g. put("MessageDigest.SHA-1", "sun.security.provider.SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                String type = getEngineName(typeAndAlg[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                String stdAlg = typeAndAlg[1].intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                String className = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                ServiceKey key = new ServiceKey(type, stdAlg, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                Service s = legacyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    s = new Service(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    s.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    s.algorithm = stdAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    legacyMap.put(key, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                s.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else { // attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                // e.g. put("MessageDigest.SHA-1 ImplementedIn", "Software");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                String attributeValue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                String type = getEngineName(typeAndAlg[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                String attributeString = typeAndAlg[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                String stdAlg = attributeString.substring(0, i).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                String attributeName = attributeString.substring(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                // kill additional spaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                while (attributeName.startsWith(" ")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    attributeName = attributeName.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                attributeName = attributeName.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                ServiceKey key = new ServiceKey(type, stdAlg, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                Service s = legacyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    s = new Service(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    s.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    s.algorithm = stdAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    legacyMap.put(key, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                s.addAttribute(attributeName, attributeValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Get the service describing this Provider's implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * specified type of this algorithm or alias. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * implementation exists, this method returns null. If there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * matching services, one added to this provider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * {@link #putService putService()} and one added via {@link #put put()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * the service added via {@link #putService putService()} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @param type the type of {@link Service service} requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * (for example, <code>MessageDigest</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param algorithm the case insensitive algorithm name (or alternate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * alias) of the service requested (for example, <code>SHA-1</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @return the service describing this Provider's matching service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * or null if no such service exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @throws NullPointerException if type or algorithm is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public synchronized Service getService(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // avoid allocating a new key object if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        ServiceKey key = previousKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (key.matches(type, algorithm) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            key = new ServiceKey(type, algorithm, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            previousKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (serviceMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            Service service = serviceMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (service != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        ensureLegacyParsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return (legacyMap != null) ? legacyMap.get(key) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    // ServiceKey from previous getService() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    // by re-using it if possible we avoid allocating a new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    // and the toUpperCase() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    // re-use will occur e.g. as the framework traverses the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    // list and queries each provider with the same values until it finds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    // a matching service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    private static volatile ServiceKey previousKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                            new ServiceKey("", "", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Get an unmodifiable Set of all services supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * this Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @return an unmodifiable Set of all services supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * this Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public synchronized Set<Service> getServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        checkInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (legacyChanged || servicesChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (serviceSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            ensureLegacyParsed();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   719
            Set<Service> set = new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (serviceMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                set.addAll(serviceMap.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            if (legacyMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                set.addAll(legacyMap.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            serviceSet = Collections.unmodifiableSet(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            servicesChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return serviceSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Add a service. If a service of the same type with the same algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * name exists and it was added using {@link #putService putService()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * it is replaced by the new service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * This method also places information about this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * in the provider's Hashtable values in the format described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Java Cryptography Architecture API Specification &amp; Reference </a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <p>Also, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * <code>checkSecurityAccess</code> method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * <code>"putProviderProperty."+name</code>, where <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * the provider name, to see if it's ok to set this provider's property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * values. If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * is used (that is, that method is not overriden), then this results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * a call to the security manager's <code>checkPermission</code> method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * a <code>SecurityPermission("putProviderProperty."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @param s the Service to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *      if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *      java.lang.SecurityManager#checkSecurityAccess}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *      access to set property values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @throws NullPointerException if s is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    protected synchronized void putService(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        check("putProviderProperty." + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            debug.println(name + ".putService(): " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (s.getProvider() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    ("service.getProvider() must match this Provider object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (serviceMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            serviceMap = new LinkedHashMap<ServiceKey,Service>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        servicesChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        String type = s.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        String algorithm = s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        ServiceKey key = new ServiceKey(type, algorithm, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // remove existing service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        implRemoveService(serviceMap.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        serviceMap.put(key, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        for (String alias : s.getAliases()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            serviceMap.put(new ServiceKey(type, alias, true), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        putPropertyStrings(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Put the string properties for this Service in this Provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    private void putPropertyStrings(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        String type = s.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        String algorithm = s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // use super() to avoid permission check and other processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        super.put(type + "." + algorithm, s.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        for (String alias : s.getAliases()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            super.put(ALIAS_PREFIX + type + "." + alias, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        for (Map.Entry<UString,String> entry : s.attributes.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            String key = type + "." + algorithm + " " + entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            super.put(key, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Remove the string properties for this Service from this Provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    private void removePropertyStrings(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        String type = s.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        String algorithm = s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // use super() to avoid permission check and other processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        super.remove(type + "." + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        for (String alias : s.getAliases()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            super.remove(ALIAS_PREFIX + type + "." + alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        for (Map.Entry<UString,String> entry : s.attributes.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            String key = type + "." + algorithm + " " + entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            super.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Remove a service previously added using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * {@link #putService putService()}. The specified service is removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * this provider. It will no longer be returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * {@link #getService getService()} and its information will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * from this provider's Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <p>Also, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <code>checkSecurityAccess</code> method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * <code>"removeProviderProperty."+name</code>, where <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * the provider name, to see if it's ok to remove this provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * properties. If the default implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * <code>checkSecurityAccess</code> is used (that is, that method is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * overriden), then this results in a call to the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <code>checkPermission</code> method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <code>SecurityPermission("removeProviderProperty."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param s the Service to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *          access to remove this provider's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @throws NullPointerException if s is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    protected synchronized void removeService(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        check("removeProviderProperty." + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            debug.println(name + ".removeService(): " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        implRemoveService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    private void implRemoveService(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if ((s == null) || (serviceMap == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        String type = s.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        String algorithm = s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        ServiceKey key = new ServiceKey(type, algorithm, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        Service oldService = serviceMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (s != oldService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        servicesChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        serviceMap.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        for (String alias : s.getAliases()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            serviceMap.remove(new ServiceKey(type, alias, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        removePropertyStrings(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    // Wrapped String that behaves in a case insensitive way for equals/hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    private static class UString {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        final String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        final String lowerString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        UString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            this.string = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            this.lowerString = s.toLowerCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            return lowerString.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            if (obj instanceof UString == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            UString other = (UString)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return lowerString.equals(other.lowerString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    // describe relevant properties of a type of engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    private static class EngineDescription {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        final boolean supportsParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        final String constructorParameterClassName;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   918
        private volatile Class<?> constructorParameterClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        EngineDescription(String name, boolean sp, String paramName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            this.supportsParameter = sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            this.constructorParameterClassName = paramName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   925
        Class<?> getConstructorParameterClass() throws ClassNotFoundException {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   926
            Class<?> clazz = constructorParameterClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                clazz = Class.forName(constructorParameterClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                constructorParameterClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    // built in knowledge of the engine types shipped as part of the JDK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    private static final Map<String,EngineDescription> knownEngines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    private static void addEngine(String name, boolean sp, String paramName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        EngineDescription ed = new EngineDescription(name, sp, paramName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        // also index by canonical name to avoid toLowerCase() for some lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        knownEngines.put(name.toLowerCase(ENGLISH), ed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        knownEngines.put(name, ed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        knownEngines = new HashMap<String,EngineDescription>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        // JCA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        addEngine("AlgorithmParameterGenerator",        false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        addEngine("AlgorithmParameters",                false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        addEngine("KeyFactory",                         false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        addEngine("KeyPairGenerator",                   false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        addEngine("KeyStore",                           false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        addEngine("MessageDigest",                      false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        addEngine("SecureRandom",                       false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        addEngine("Signature",                          true,  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        addEngine("CertificateFactory",                 false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        addEngine("CertPathBuilder",                    false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        addEngine("CertPathValidator",                  false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        addEngine("CertStore",                          false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                            "java.security.cert.CertStoreParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        // JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        addEngine("Cipher",                             true,  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        addEngine("ExemptionMechanism",                 false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        addEngine("Mac",                                true,  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        addEngine("KeyAgreement",                       true,  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        addEngine("KeyGenerator",                       false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        addEngine("SecretKeyFactory",                   false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        // JSSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        addEngine("KeyManagerFactory",                  false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        addEngine("SSLContext",                         false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        addEngine("TrustManagerFactory",                false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        // JGSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        addEngine("GssApiMechanism",                    false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // SASL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        addEngine("SaslClientFactory",                  false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        addEngine("SaslServerFactory",                  false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // POLICY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        addEngine("Policy",                             false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                            "java.security.Policy$Parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // CONFIGURATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        addEngine("Configuration",                      false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                            "javax.security.auth.login.Configuration$Parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        // XML DSig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        addEngine("XMLSignatureFactory",                false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        addEngine("KeyInfoFactory",                     false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        addEngine("TransformService",                   false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        // Smart Card I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        addEngine("TerminalFactory",                    false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                            "java.lang.Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    // get the "standard" (mixed-case) engine name for arbitary case engine name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    // if there is no known engine by that name, return s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    private static String getEngineName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        // try original case first, usually correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        EngineDescription e = knownEngines.get(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            e = knownEngines.get(s.toLowerCase(ENGLISH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        return (e == null) ? s : e.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * The description of a security service. It encapsulates the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * of a service and contains a factory method to obtain new implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * instances of this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * <p>Each service has a provider that offers the service, a type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * an algorithm name, and the name of the class that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * service. Optionally, it also includes a list of alternate algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * names for this service (aliases) and attributes, which are a map of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * (name, value) String pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * <p>This class defines the methods {@link #supportsParameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * supportsParameter()} and {@link #newInstance newInstance()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * which are used by the Java security framework when it searches for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * suitable services and instantes them. The valid arguments to those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * methods depend on the type of service. For the service types defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * within Java SE, see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * for the valid values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * Note that components outside of Java SE can define additional types of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * services and their behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <p>Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    public static class Service {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        private String type, algorithm, className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        private final Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        private List<String> aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        private Map<UString,String> attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // Reference to the cached implementation Class object
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1038
        private volatile Reference<Class<?>> classRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        // flag indicating whether this service has its attributes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        // supportedKeyFormats or supportedKeyClasses set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        // if null, the values have not been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        // if TRUE, at least one of supportedFormats/Classes is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        private volatile Boolean hasKeyAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        // supported encoding formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        private String[] supportedFormats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // names of the supported key (super) classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        private Class[] supportedClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        // whether this service has been registered with the Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        private boolean registered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1055
        private static final Class<?>[] CLASS0 = new Class<?>[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // this constructor and these methods are used for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        // the legacy string properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        private Service(Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            aliases = Collections.<String>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            attributes = Collections.<UString,String>emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        private boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            return (type != null) && (algorithm != null) && (className != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        private void addAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            if (aliases.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                aliases = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            aliases.add(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        void addAttribute(String type, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            if (attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                attributes = new HashMap<UString,String>(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            attributes.put(new UString(type), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         * Construct a new service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * @param provider the provider that offers this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
         * @param type the type of this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
         * @param className the name of the class implementing this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
         * @param aliases List of aliases or null if algorithm has no aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         * @param attributes Map of attributes or null if this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         *                   has no attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         * @throws NullPointerException if provider, type, algorithm, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         * className is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        public Service(Provider provider, String type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                String className, List<String> aliases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                Map<String,String> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if ((provider == null) || (type == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    (algorithm == null) || (className == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            this.type = getEngineName(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if (aliases == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                this.aliases = Collections.<String>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                this.aliases = new ArrayList<String>(aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                this.attributes = Collections.<UString,String>emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                this.attributes = new HashMap<UString,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                for (Map.Entry<String,String> entry : attributes.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    this.attributes.put(new UString(entry.getKey()), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         * Get the type of this service. For example, <code>MessageDigest</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         * @return the type of this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        public final String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * Return the name of the algorithm of this service. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * <code>SHA-1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * @return the algorithm of this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            return algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
         * Return the Provider of this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
         * @return the Provider of this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         * Return the name of the class implementing this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * @return the name of the class implementing this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        public final String getClassName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            return className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // internal only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        private final List<String> getAliases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         * Return the value of the specified attribute or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * attribute is not set for this Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         * @param name the name of the requested attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         * @return the value of the specified attribute or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         *         attribute is not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * @throws NullPointerException if name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        public final String getAttribute(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return attributes.get(new UString(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
         * Return a new instance of the implementation described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
         * service. The security provider framework uses this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
         * construct implementations. Applications will typically not need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
         * to call it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         * <p>The default implementation uses reflection to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         * standard constructor for this type of service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         * Security providers can override this method to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
         * instantiation in a different way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         * For details and the values of constructorParameter that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
         * valid for the various types of services see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
         * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
         * Java Cryptography Architecture API Specification &amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
         * Reference</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         * @param constructorParameter the value to pass to the constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * or null if this type of service does not use a constructorParameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * @return a new implementation of this service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         * @throws InvalidParameterException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
         * constructorParameter is invalid for this type of service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         * @throws NoSuchAlgorithmException if instantation failed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         * any other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        public Object newInstance(Object constructorParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (registered == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                if (provider.getService(type, algorithm) != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                        ("Service not registered with Provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                        + provider.getName() + ": " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                registered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                EngineDescription cap = knownEngines.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                if (cap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    // unknown engine type, use generic code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    // this is the code path future for non-core
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    // optional packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    return newInstanceGeneric(constructorParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                if (cap.constructorParameterClassName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    if (constructorParameter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                        throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                            ("constructorParameter not used with " + type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                            + " engines");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1234
                    Class<?> clazz = getImplClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    return clazz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1237
                    Class<?> paramClass = cap.getConstructorParameterClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    if (constructorParameter != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1239
                        Class<?> argClass = constructorParameter.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                        if (paramClass.isAssignableFrom(argClass) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                            throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                            ("constructorParameter must be instanceof "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                            + cap.constructorParameterClassName.replace('$', '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                            + " for engine type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                    }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1247
                    Class<?> clazz = getImplClass();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1248
                    Constructor<?> cons = clazz.getConstructor(paramClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    return cons.newInstance(constructorParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                    ("Error constructing implementation (algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    + algorithm + ", provider: " + provider.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    + ", class: " + className + ")", e.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    ("Error constructing implementation (algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                    + algorithm + ", provider: " + provider.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    + ", class: " + className + ")", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        // return the implementation Class object for this service
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1267
        private Class<?> getImplClass() throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1269
                Reference<Class<?>> ref = classRef;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1270
                Class<?> clazz = (ref == null) ? null : ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    ClassLoader cl = provider.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    if (cl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                        clazz = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                        clazz = cl.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1278
                    classRef = new WeakReference<Class<?>>(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    ("class configured for " + type + "(provider: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    provider.getName() + ")" + "cannot be found.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
         * Generic code path for unknown engine types. Call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
         * no-args constructor if constructorParameter is null, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
         * use the first matching constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        private Object newInstanceGeneric(Object constructorParameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                throws Exception {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1295
            Class<?> clazz = getImplClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            if (constructorParameter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                Object o = clazz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1300
            Class<?> argClass = constructorParameter.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            Constructor[] cons = clazz.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            // find first public constructor that can take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            // argument as parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            for (int i = 0; i < cons.length; i++) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1305
                Constructor<?> con = cons[i];
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1306
                Class<?>[] paramTypes = con.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                if (paramTypes.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                if (paramTypes[0].isAssignableFrom(argClass) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                Object o = con.newInstance(new Object[] {constructorParameter});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            throw new NoSuchAlgorithmException("No constructor matching "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                + argClass.getName() + " found in class " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
         * Test whether this Service can use the specified parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         * Returns false if this service cannot use the parameter. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         * true if this service can use the parameter, if a fast test is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * infeasible, or if the status is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         * <p>The security provider framework uses this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * some types of services to quickly exclude non-matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * implementations for consideration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         * Applications will typically not need to call it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
         * <p>For details and the values of parameter that are valid for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         * various types of services see the top of this class and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         * Java Cryptography Architecture API Specification &amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         * Reference</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         * Security providers can override it to implement their own test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
         * @param parameter the parameter to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         * @return false if this this service cannot use the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * parameter; true if it can possibly use the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         * @throws InvalidParameterException if the value of parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         * invalid for this type of service or if this method cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         * used with this type of service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        public boolean supportsParameter(Object parameter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            EngineDescription cap = knownEngines.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            if (cap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                // unknown engine type, return true by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            if (cap.supportsParameter == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                throw new InvalidParameterException("supportsParameter() not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                    + "used with " + type + " engines");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            // allow null for keys without attributes for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            if ((parameter != null) && (parameter instanceof Key == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    ("Parameter must be instanceof Key for engine " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            if (hasKeyAttributes() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            if (parameter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            Key key = (Key)parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            if (supportsKeyFormat(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (supportsKeyClass(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * Return whether this service has its Supported* properties for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         * keys defined. Parses the attributes if not yet initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        private boolean hasKeyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            Boolean b = hasKeyAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    s = getAttribute("SupportedKeyFormats");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                        supportedFormats = s.split("\\|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    s = getAttribute("SupportedKeyClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                        String[] classNames = s.split("\\|");
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1394
                        List<Class<?>> classList =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
  1395
                            new ArrayList<>(classNames.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                        for (String className : classNames) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1397
                            Class<?> clazz = getKeyClass(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                            if (clazz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                                classList.add(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                        supportedClasses = classList.toArray(CLASS0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    boolean bool = (supportedFormats != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                        || (supportedClasses != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    b = Boolean.valueOf(bool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    hasKeyAttributes = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            return b.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        // get the key class object of the specified name
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1414
        private Class<?> getKeyClass(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                return Class.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                ClassLoader cl = provider.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    return cl.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        private boolean supportsKeyFormat(Key key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            if (supportedFormats == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            String format = key.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            for (String supportedFormat : supportedFormats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                if (supportedFormat.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        private boolean supportsKeyClass(Key key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            if (supportedClasses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1451
            Class<?> keyClass = key.getClass();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1452
            for (Class<?> clazz : supportedClasses) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                if (clazz.isAssignableFrom(keyClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         * Return a String representation of this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
         * @return a String representation of this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            String aString = aliases.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                ? "" : "\r\n  aliases: " + aliases.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            String attrs = attributes.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                ? "" : "\r\n  attributes: " + attributes.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            return provider.getName() + ": " + type + "." + algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                + " -> " + className + aString + attrs + "\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
}