jdk/src/share/classes/java/security/Provider.java
changeset 25542 05badfb785b2
parent 24685 215fa91e1b4c
equal deleted inserted replaced
25541:df83fb1a542e 25542:05badfb785b2
  1374      * (name, value) String pairs.
  1374      * (name, value) String pairs.
  1375      *
  1375      *
  1376      * <p>This class defines the methods {@link #supportsParameter
  1376      * <p>This class defines the methods {@link #supportsParameter
  1377      * supportsParameter()} and {@link #newInstance newInstance()}
  1377      * supportsParameter()} and {@link #newInstance newInstance()}
  1378      * which are used by the Java security framework when it searches for
  1378      * which are used by the Java security framework when it searches for
  1379      * suitable services and instantes them. The valid arguments to those
  1379      * suitable services and instantiates them. The valid arguments to those
  1380      * methods depend on the type of service. For the service types defined
  1380      * methods depend on the type of service. For the service types defined
  1381      * within Java SE, see the
  1381      * within Java SE, see the
  1382      * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
  1382      * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">
  1383      * Java Cryptography Architecture API Specification &amp; Reference </a>
  1383      * Java Cryptography Architecture API Specification &amp; Reference </a>
  1384      * for the valid values.
  1384      * for the valid values.
  1564          *
  1564          *
  1565          * @return a new implementation of this service
  1565          * @return a new implementation of this service
  1566          *
  1566          *
  1567          * @throws InvalidParameterException if the value of
  1567          * @throws InvalidParameterException if the value of
  1568          * constructorParameter is invalid for this type of service.
  1568          * constructorParameter is invalid for this type of service.
  1569          * @throws NoSuchAlgorithmException if instantation failed for
  1569          * @throws NoSuchAlgorithmException if instantiation failed for
  1570          * any other reason.
  1570          * any other reason.
  1571          */
  1571          */
  1572         public Object newInstance(Object constructorParameter)
  1572         public Object newInstance(Object constructorParameter)
  1573                 throws NoSuchAlgorithmException {
  1573                 throws NoSuchAlgorithmException {
  1574             if (registered == false) {
  1574             if (registered == false) {
  1592                         throw new InvalidParameterException
  1592                         throw new InvalidParameterException
  1593                             ("constructorParameter not used with " + type
  1593                             ("constructorParameter not used with " + type
  1594                             + " engines");
  1594                             + " engines");
  1595                     }
  1595                     }
  1596                     Class<?> clazz = getImplClass();
  1596                     Class<?> clazz = getImplClass();
  1597                     return clazz.newInstance();
  1597                     Class<?>[] empty = {};
       
  1598                     Constructor<?> con = clazz.getConstructor(empty);
       
  1599                     return con.newInstance();
  1598                 } else {
  1600                 } else {
  1599                     Class<?> paramClass = cap.getConstructorParameterClass();
  1601                     Class<?> paramClass = cap.getConstructorParameterClass();
  1600                     if (constructorParameter != null) {
  1602                     if (constructorParameter != null) {
  1601                         Class<?> argClass = constructorParameter.getClass();
  1603                         Class<?> argClass = constructorParameter.getClass();
  1602                         if (paramClass.isAssignableFrom(argClass) == false) {
  1604                         if (paramClass.isAssignableFrom(argClass) == false) {
  1635                     if (cl == null) {
  1637                     if (cl == null) {
  1636                         clazz = Class.forName(className);
  1638                         clazz = Class.forName(className);
  1637                     } else {
  1639                     } else {
  1638                         clazz = cl.loadClass(className);
  1640                         clazz = cl.loadClass(className);
  1639                     }
  1641                     }
       
  1642                     if (!Modifier.isPublic(clazz.getModifiers())) {
       
  1643                         throw new NoSuchAlgorithmException
       
  1644                             ("class configured for " + type + " (provider: " +
       
  1645                             provider.getName() + ") is not public.");
       
  1646                     }
  1640                     classRef = new WeakReference<Class<?>>(clazz);
  1647                     classRef = new WeakReference<Class<?>>(clazz);
  1641                 }
  1648                 }
  1642                 return clazz;
  1649                 return clazz;
  1643             } catch (ClassNotFoundException e) {
  1650             } catch (ClassNotFoundException e) {
  1644                 throw new NoSuchAlgorithmException
  1651                 throw new NoSuchAlgorithmException
  1645                     ("class configured for " + type + "(provider: " +
  1652                     ("class configured for " + type + " (provider: " +
  1646                     provider.getName() + ")" + "cannot be found.", e);
  1653                     provider.getName() + ") cannot be found.", e);
  1647             }
  1654             }
  1648         }
  1655         }
  1649 
  1656 
  1650         /**
  1657         /**
  1651          * Generic code path for unknown engine types. Call the
  1658          * Generic code path for unknown engine types. Call the
  1654          */
  1661          */
  1655         private Object newInstanceGeneric(Object constructorParameter)
  1662         private Object newInstanceGeneric(Object constructorParameter)
  1656                 throws Exception {
  1663                 throws Exception {
  1657             Class<?> clazz = getImplClass();
  1664             Class<?> clazz = getImplClass();
  1658             if (constructorParameter == null) {
  1665             if (constructorParameter == null) {
  1659                 Object o = clazz.newInstance();
  1666                 // create instance with public no-arg constructor if it exists
  1660                 return o;
  1667                 try {
       
  1668                     Class<?>[] empty = {};
       
  1669                     Constructor<?> con = clazz.getConstructor(empty);
       
  1670                     return con.newInstance();
       
  1671                 } catch (NoSuchMethodException e) {
       
  1672                     throw new NoSuchAlgorithmException("No public no-arg "
       
  1673                         + "constructor found in class " + className);
       
  1674                 }
  1661             }
  1675             }
  1662             Class<?> argClass = constructorParameter.getClass();
  1676             Class<?> argClass = constructorParameter.getClass();
  1663             Constructor<?>[] cons = clazz.getConstructors();
  1677             Constructor<?>[] cons = clazz.getConstructors();
  1664             // find first public constructor that can take the
  1678             // find first public constructor that can take the
  1665             // argument as parameter
  1679             // argument as parameter
  1666             for (int i = 0; i < cons.length; i++) {
  1680             for (Constructor<?> con : cons) {
  1667                 Constructor<?> con = cons[i];
       
  1668                 Class<?>[] paramTypes = con.getParameterTypes();
  1681                 Class<?>[] paramTypes = con.getParameterTypes();
  1669                 if (paramTypes.length != 1) {
  1682                 if (paramTypes.length != 1) {
  1670                     continue;
  1683                     continue;
  1671                 }
  1684                 }
  1672                 if (paramTypes[0].isAssignableFrom(argClass) == false) {
  1685                 if (paramTypes[0].isAssignableFrom(argClass) == false) {
  1673                     continue;
  1686                     continue;
  1674                 }
  1687                 }
  1675                 Object o = con.newInstance(new Object[] {constructorParameter});
  1688                 return con.newInstance(constructorParameter);
  1676                 return o;
  1689             }
  1677             }
  1690             throw new NoSuchAlgorithmException("No public constructor matching "
  1678             throw new NoSuchAlgorithmException("No constructor matching "
       
  1679                 + argClass.getName() + " found in class " + className);
  1691                 + argClass.getName() + " found in class " + className);
  1680         }
  1692         }
  1681 
  1693 
  1682         /**
  1694         /**
  1683          * Test whether this Service can use the specified parameter.
  1695          * Test whether this Service can use the specified parameter.