jdk/src/share/classes/sun/security/jca/GetInstance.java
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
equal deleted inserted replaced
10335:3c7eda3ab2f5 10336:0bb1999251f8
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   147      *   (for CertStores)
   147      *   (for CertStores)
   148      *
   148      *
   149      * There are overloaded methods for all the permutations.
   149      * There are overloaded methods for all the permutations.
   150      */
   150      */
   151 
   151 
   152     public static Instance getInstance(String type, Class clazz,
   152     public static Instance getInstance(String type, Class<?> clazz,
   153             String algorithm) throws NoSuchAlgorithmException {
   153             String algorithm) throws NoSuchAlgorithmException {
   154         // in the almost all cases, the first service will work
   154         // in the almost all cases, the first service will work
   155         // avoid taking long path if so
   155         // avoid taking long path if so
   156         ProviderList list = Providers.getProviderList();
   156         ProviderList list = Providers.getProviderList();
   157         Service firstService = list.getService(type, algorithm);
   157         Service firstService = list.getService(type, algorithm);
   179             }
   179             }
   180         }
   180         }
   181         throw failure;
   181         throw failure;
   182     }
   182     }
   183 
   183 
   184     public static Instance getInstance(String type, Class clazz,
   184     public static Instance getInstance(String type, Class<?> clazz,
   185             String algorithm, Object param) throws NoSuchAlgorithmException {
   185             String algorithm, Object param) throws NoSuchAlgorithmException {
   186         List<Service> services = getServices(type, algorithm);
   186         List<Service> services = getServices(type, algorithm);
   187         NoSuchAlgorithmException failure = null;
   187         NoSuchAlgorithmException failure = null;
   188         for (Service s : services) {
   188         for (Service s : services) {
   189             try {
   189             try {
   198             throw new NoSuchAlgorithmException
   198             throw new NoSuchAlgorithmException
   199                     (algorithm + " " + type + " not available");
   199                     (algorithm + " " + type + " not available");
   200         }
   200         }
   201     }
   201     }
   202 
   202 
   203     public static Instance getInstance(String type, Class clazz,
   203     public static Instance getInstance(String type, Class<?> clazz,
   204             String algorithm, String provider) throws NoSuchAlgorithmException,
   204             String algorithm, String provider) throws NoSuchAlgorithmException,
   205             NoSuchProviderException {
   205             NoSuchProviderException {
   206         return getInstance(getService(type, algorithm, provider), clazz);
   206         return getInstance(getService(type, algorithm, provider), clazz);
   207     }
   207     }
   208 
   208 
   209     public static Instance getInstance(String type, Class clazz,
   209     public static Instance getInstance(String type, Class<?> clazz,
   210             String algorithm, Object param, String provider)
   210             String algorithm, Object param, String provider)
   211             throws NoSuchAlgorithmException, NoSuchProviderException {
   211             throws NoSuchAlgorithmException, NoSuchProviderException {
   212         return getInstance(getService(type, algorithm, provider), clazz, param);
   212         return getInstance(getService(type, algorithm, provider), clazz, param);
   213     }
   213     }
   214 
   214 
   215     public static Instance getInstance(String type, Class clazz,
   215     public static Instance getInstance(String type, Class<?> clazz,
   216             String algorithm, Provider provider)
   216             String algorithm, Provider provider)
   217             throws NoSuchAlgorithmException {
   217             throws NoSuchAlgorithmException {
   218         return getInstance(getService(type, algorithm, provider), clazz);
   218         return getInstance(getService(type, algorithm, provider), clazz);
   219     }
   219     }
   220 
   220 
   221     public static Instance getInstance(String type, Class clazz,
   221     public static Instance getInstance(String type, Class<?> clazz,
   222             String algorithm, Object param, Provider provider)
   222             String algorithm, Object param, Provider provider)
   223             throws NoSuchAlgorithmException {
   223             throws NoSuchAlgorithmException {
   224         return getInstance(getService(type, algorithm, provider), clazz, param);
   224         return getInstance(getService(type, algorithm, provider), clazz, param);
   225     }
   225     }
   226 
   226 
   229      * intended for classes that cannot use the standard methods, e.g.
   229      * intended for classes that cannot use the standard methods, e.g.
   230      * because they implement delayed provider selection like the
   230      * because they implement delayed provider selection like the
   231      * Signature class.
   231      * Signature class.
   232      */
   232      */
   233 
   233 
   234     public static Instance getInstance(Service s, Class clazz)
   234     public static Instance getInstance(Service s, Class<?> clazz)
   235             throws NoSuchAlgorithmException {
   235             throws NoSuchAlgorithmException {
   236         Object instance = s.newInstance(null);
   236         Object instance = s.newInstance(null);
   237         checkSuperClass(s, instance.getClass(), clazz);
   237         checkSuperClass(s, instance.getClass(), clazz);
   238         return new Instance(s.getProvider(), instance);
   238         return new Instance(s.getProvider(), instance);
   239     }
   239     }
   240 
   240 
   241     public static Instance getInstance(Service s, Class clazz,
   241     public static Instance getInstance(Service s, Class<?> clazz,
   242             Object param) throws NoSuchAlgorithmException {
   242             Object param) throws NoSuchAlgorithmException {
   243         Object instance = s.newInstance(param);
   243         Object instance = s.newInstance(param);
   244         checkSuperClass(s, instance.getClass(), clazz);
   244         checkSuperClass(s, instance.getClass(), clazz);
   245         return new Instance(s.getProvider(), instance);
   245         return new Instance(s.getProvider(), instance);
   246     }
   246     }
   247 
   247 
   248     /**
   248     /**
   249      * Check is subClass is a subclass of superClass. If not,
   249      * Check is subClass is a subclass of superClass. If not,
   250      * throw a NoSuchAlgorithmException.
   250      * throw a NoSuchAlgorithmException.
   251      */
   251      */
   252     public static void checkSuperClass(Service s, Class subClass,
   252     public static void checkSuperClass(Service s, Class<?> subClass,
   253             Class superClass) throws NoSuchAlgorithmException {
   253             Class<?> superClass) throws NoSuchAlgorithmException {
   254         if (superClass == null) {
   254         if (superClass == null) {
   255             return;
   255             return;
   256         }
   256         }
   257         if (superClass.isAssignableFrom(subClass) == false) {
   257         if (superClass.isAssignableFrom(subClass) == false) {
   258             throw new NoSuchAlgorithmException
   258             throw new NoSuchAlgorithmException