0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.jca;
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.Provider.Service;
0N/A
0N/A/**
0N/A * Collection of utility methods to facilitate implementing getInstance()
0N/A * methods in the JCA/JCE/JSSE/... framework.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.5
0N/A */
0N/Apublic class GetInstance {
0N/A
0N/A private GetInstance() {
0N/A // empty
0N/A }
0N/A
0N/A /**
0N/A * Static inner class representing a newly created instance.
0N/A */
0N/A public static final class Instance {
0N/A // public final fields, access directly without accessors
0N/A public final Provider provider;
0N/A public final Object impl;
0N/A private Instance(Provider provider, Object impl) {
0N/A this.provider = provider;
0N/A this.impl = impl;
0N/A }
0N/A // Return Provider and implementation as an array as used in the
0N/A // old Security.getImpl() methods.
0N/A public Object[] toArray() {
0N/A return new Object[] {impl, provider};
0N/A }
0N/A }
0N/A
0N/A public static Service getService(String type, String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A ProviderList list = Providers.getProviderList();
0N/A Service s = list.getService(type, algorithm);
0N/A if (s == null) {
0N/A throw new NoSuchAlgorithmException
0N/A (algorithm + " " + type + " not available");
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A public static Service getService(String type, String algorithm,
0N/A String provider) throws NoSuchAlgorithmException,
0N/A NoSuchProviderException {
0N/A if ((provider == null) || (provider.length() == 0)) {
0N/A throw new IllegalArgumentException("missing provider");
0N/A }
0N/A Provider p = Providers.getProviderList().getProvider(provider);
0N/A if (p == null) {
0N/A throw new NoSuchProviderException("no such provider: " + provider);
0N/A }
0N/A Service s = p.getService(type, algorithm);
0N/A if (s == null) {
0N/A throw new NoSuchAlgorithmException("no such algorithm: "
0N/A + algorithm + " for provider " + provider);
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A public static Service getService(String type, String algorithm,
0N/A Provider provider) throws NoSuchAlgorithmException {
0N/A if (provider == null) {
0N/A throw new IllegalArgumentException("missing provider");
0N/A }
0N/A Service s = provider.getService(type, algorithm);
0N/A if (s == null) {
0N/A throw new NoSuchAlgorithmException("no such algorithm: "
0N/A + algorithm + " for provider " + provider.getName());
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A /**
0N/A * Return a List of all the available Services that implement
0N/A * (type, algorithm). Note that the list is initialized lazily
0N/A * and Provider loading and lookup is only trigered when
0N/A * necessary.
0N/A */
0N/A public static List<Service> getServices(String type, String algorithm) {
0N/A ProviderList list = Providers.getProviderList();
0N/A return list.getServices(type, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * This method exists for compatibility with JCE only. It will be removed
0N/A * once JCE has been changed to use the replacement method.
0N/A * @deprecated use getServices(List<ServiceId>) instead
0N/A */
0N/A @Deprecated
0N/A public static List<Service> getServices(String type,
0N/A List<String> algorithms) {
0N/A ProviderList list = Providers.getProviderList();
0N/A return list.getServices(type, algorithms);
0N/A }
0N/A
0N/A /**
0N/A * Return a List of all the available Services that implement any of
0N/A * the specified algorithms. See getServices(String, String) for detals.
0N/A */
0N/A public static List<Service> getServices(List<ServiceId> ids) {
0N/A ProviderList list = Providers.getProviderList();
0N/A return list.getServices(ids);
0N/A }
0N/A
0N/A /*
0N/A * For all the getInstance() methods below:
0N/A * @param type the type of engine (e.g. MessageDigest)
0N/A * @param clazz the Spi class that the implementation must subclass
0N/A * (e.g. MessageDigestSpi.class) or null if no superclass check
0N/A * is required
0N/A * @param algorithm the name of the algorithm (or alias), e.g. MD5
0N/A * @param provider the provider (String or Provider object)
0N/A * @param param the parameter to pass to the Spi constructor
0N/A * (for CertStores)
0N/A *
0N/A * There are overloaded methods for all the permutations.
0N/A */
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm) throws NoSuchAlgorithmException {
0N/A // in the almost all cases, the first service will work
0N/A // avoid taking long path if so
0N/A ProviderList list = Providers.getProviderList();
0N/A Service firstService = list.getService(type, algorithm);
0N/A if (firstService == null) {
0N/A throw new NoSuchAlgorithmException
0N/A (algorithm + " " + type + " not available");
0N/A }
0N/A NoSuchAlgorithmException failure;
0N/A try {
0N/A return getInstance(firstService, clazz);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A failure = e;
0N/A }
0N/A // if we cannot get the service from the prefered provider,
0N/A // fail over to the next
0N/A for (Service s : list.getServices(type, algorithm)) {
0N/A if (s == firstService) {
0N/A // do not retry initial failed service
0N/A continue;
0N/A }
0N/A try {
0N/A return getInstance(s, clazz);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A failure = e;
0N/A }
0N/A }
0N/A throw failure;
0N/A }
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm, Object param) throws NoSuchAlgorithmException {
0N/A List<Service> services = getServices(type, algorithm);
0N/A NoSuchAlgorithmException failure = null;
0N/A for (Service s : services) {
0N/A try {
0N/A return getInstance(s, clazz, param);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A failure = e;
0N/A }
0N/A }
0N/A if (failure != null) {
0N/A throw failure;
0N/A } else {
0N/A throw new NoSuchAlgorithmException
0N/A (algorithm + " " + type + " not available");
0N/A }
0N/A }
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm, String provider) throws NoSuchAlgorithmException,
0N/A NoSuchProviderException {
0N/A return getInstance(getService(type, algorithm, provider), clazz);
0N/A }
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm, Object param, String provider)
0N/A throws NoSuchAlgorithmException, NoSuchProviderException {
0N/A return getInstance(getService(type, algorithm, provider), clazz, param);
0N/A }
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm, Provider provider)
0N/A throws NoSuchAlgorithmException {
0N/A return getInstance(getService(type, algorithm, provider), clazz);
0N/A }
0N/A
0N/A public static Instance getInstance(String type, Class clazz,
0N/A String algorithm, Object param, Provider provider)
0N/A throws NoSuchAlgorithmException {
0N/A return getInstance(getService(type, algorithm, provider), clazz, param);
0N/A }
0N/A
0N/A /*
0N/A * The two getInstance() methods below take a service. They are
0N/A * intended for classes that cannot use the standard methods, e.g.
0N/A * because they implement delayed provider selection like the
0N/A * Signature class.
0N/A */
0N/A
0N/A public static Instance getInstance(Service s, Class clazz)
0N/A throws NoSuchAlgorithmException {
0N/A Object instance = s.newInstance(null);
0N/A checkSuperClass(s, instance.getClass(), clazz);
0N/A return new Instance(s.getProvider(), instance);
0N/A }
0N/A
0N/A public static Instance getInstance(Service s, Class clazz,
0N/A Object param) throws NoSuchAlgorithmException {
0N/A Object instance = s.newInstance(param);
0N/A checkSuperClass(s, instance.getClass(), clazz);
0N/A return new Instance(s.getProvider(), instance);
0N/A }
0N/A
0N/A /**
0N/A * Check is subClass is a subclass of superClass. If not,
0N/A * throw a NoSuchAlgorithmException.
0N/A */
0N/A public static void checkSuperClass(Service s, Class subClass,
0N/A Class superClass) throws NoSuchAlgorithmException {
0N/A if (superClass == null) {
0N/A return;
0N/A }
0N/A if (superClass.isAssignableFrom(subClass) == false) {
0N/A throw new NoSuchAlgorithmException
0N/A ("class configured for " + s.getType() + ": "
0N/A + s.getClassName() + " not a " + s.getType());
0N/A }
0N/A }
0N/A
0N/A}