src/java.base/share/classes/sun/security/jca/GetInstance.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2003, 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 sun.security.jca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Collection of utility methods to facilitate implementing getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * methods in the JCA/JCE/JSSE/... framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class GetInstance {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private GetInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Static inner class representing a newly created instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final class Instance {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // public final fields, access directly without accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public final Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public final Object impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        private Instance(Provider provider, Object impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Return Provider and implementation as an array as used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // old Security.getImpl() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return new Object[] {impl, provider};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static Service getService(String type, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Service s = list.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    (algorithm + " " + type + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static Service getService(String type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            NoSuchProviderException {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
    78
        if (provider == null || provider.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Provider p = Providers.getProviderList().getProvider(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new NoSuchProviderException("no such provider: " + provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Service s = p.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new NoSuchAlgorithmException("no such algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                + algorithm + " for provider " + provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static Service getService(String type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Service s = provider.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new NoSuchAlgorithmException("no such algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                + algorithm + " for provider " + provider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Return a List of all the available Services that implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * (type, algorithm). Note that the list is initialized lazily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * and Provider loading and lookup is only trigered when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static List<Service> getServices(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return list.getServices(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * This method exists for compatibility with JCE only. It will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * once JCE has been changed to use the replacement method.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   120
     * @deprecated use {@code getServices(List<ServiceId>)} instead
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static List<Service> getServices(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            List<String> algorithms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return list.getServices(type, algorithms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Return a List of all the available Services that implement any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the specified algorithms. See getServices(String, String) for detals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static List<Service> getServices(List<ServiceId> ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return list.getServices(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * For all the getInstance() methods below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param type the type of engine (e.g. MessageDigest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param clazz the Spi class that the implementation must subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *   (e.g. MessageDigestSpi.class) or null if no superclass check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *   is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param algorithm the name of the algorithm (or alias), e.g. MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param provider the provider (String or Provider object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param param the parameter to pass to the Spi constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *   (for CertStores)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * There are overloaded methods for all the permutations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   152
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // in the almost all cases, the first service will work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // avoid taking long path if so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Service firstService = list.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (firstService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    (algorithm + " " + type + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        NoSuchAlgorithmException failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return getInstance(firstService, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10336
diff changeset
   168
        // if we cannot get the service from the preferred provider,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // fail over to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (Service s : list.getServices(type, algorithm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (s == firstService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // do not retry initial failed service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   184
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            String algorithm, Object param) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        List<Service> services = getServices(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        NoSuchAlgorithmException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (Service s : services) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return getInstance(s, clazz, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (failure != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    (algorithm + " " + type + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   203
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            String algorithm, String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return getInstance(getService(type, algorithm, provider), clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   209
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            String algorithm, Object param, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return getInstance(getService(type, algorithm, provider), clazz, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   215
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return getInstance(getService(type, algorithm, provider), clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   221
    public static Instance getInstance(String type, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            String algorithm, Object param, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return getInstance(getService(type, algorithm, provider), clazz, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * The two getInstance() methods below take a service. They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * intended for classes that cannot use the standard methods, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * because they implement delayed provider selection like the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Signature class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   234
    public static Instance getInstance(Service s, Class<?> clazz)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Object instance = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        checkSuperClass(s, instance.getClass(), clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return new Instance(s.getProvider(), instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   241
    public static Instance getInstance(Service s, Class<?> clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            Object param) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        Object instance = s.newInstance(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        checkSuperClass(s, instance.getClass(), clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return new Instance(s.getProvider(), instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Check is subClass is a subclass of superClass. If not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * throw a NoSuchAlgorithmException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   252
    public static void checkSuperClass(Service s, Class<?> subClass,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   253
            Class<?> superClass) throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (superClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (superClass.isAssignableFrom(subClass) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                ("class configured for " + s.getType() + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                + s.getClassName() + " not a " + s.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}