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