diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2006-09-05 20:47:38 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2006-09-05 20:47:38 +0000 |
commit | 228fc33f661314b6723b691635085ee4ff6a69b2 (patch) | |
tree | 50e78696593f8df21fe9330258dff98537d39f65 /javax/crypto/KeyAgreement.java | |
parent | f090c35d5776db64813ccf19d8327cf0ae756ac7 (diff) | |
download | classpath-228fc33f661314b6723b691635085ee4ff6a69b2.tar.gz |
2006-09-05 Andrew John Hughes <gnu_andrew@member.fsf.org>
* Merge of HEAD-->generics from 2006/08/12 to 2006/09/03.
Diffstat (limited to 'javax/crypto/KeyAgreement.java')
-rw-r--r-- | javax/crypto/KeyAgreement.java | 116 |
1 files changed, 61 insertions, 55 deletions
diff --git a/javax/crypto/KeyAgreement.java b/javax/crypto/KeyAgreement.java index d71743e3e..490031091 100644 --- a/javax/crypto/KeyAgreement.java +++ b/javax/crypto/KeyAgreement.java @@ -101,97 +101,103 @@ public class KeyAgreement virgin = true; } - // Class methods. - // ------------------------------------------------------------------------ - /** * Get an implementation of an algorithm from the first provider that * implements it. - * + * * @param algorithm The name of the algorithm to get. * @return The proper KeyAgreement instacne, if found. - * @throws java.security.NoSuchAlgorithmException If the specified - * algorithm is not implemented by any installed provider. + * @throws NoSuchAlgorithmException If the specified algorithm is not + * implemented by any installed provider. + * @throws IllegalArgumentException if <code>algorithm</code> is + * <code>null</code> or is an empty string. */ public static final KeyAgreement getInstance(String algorithm) - throws NoSuchAlgorithmException + throws NoSuchAlgorithmException { - Provider[] provs = Security.getProviders(); - String msg = algorithm; - for (int i = 0; i < provs.length; i++) - { - try - { - return getInstance(algorithm, provs[i]); - } - catch (NoSuchAlgorithmException nsae) - { - msg = nsae.getMessage(); - } - } - throw new NoSuchAlgorithmException(msg); + Provider[] p = Security.getProviders(); + NoSuchAlgorithmException lastException = null; + for (int i = 0; i < p.length; i++) + try + { + return getInstance(algorithm, p[i]); + } + catch (NoSuchAlgorithmException x) + { + lastException = x; + } + if (lastException != null) + throw lastException; + throw new NoSuchAlgorithmException(algorithm); } /** - * Get an implementation of an algorithm from a named provider. - * - * @param algorithm The name of the algorithm to get. - * @param provider The name of the provider from which to get the - * implementation. + * Return an implementation of an algorithm from a named provider. + * + * @param algorithm The name of the algorithm to create. + * @param provider The name of the provider from which to get the + * implementation. * @return The proper KeyAgreement instance, if found. - * @throws java.security.NoSuchAlgorithmException If the named provider - * does not implement the algorithm. - * @throws java.security.NoSuchProviderException If the named provider - * does not exist. + * @throws NoSuchAlgorithmException If the named provider does not implement + * the algorithm. + * @throws NoSuchProviderException If the named provider does not exist. + * @throws IllegalArgumentException if either <code>algorithm</code> or + * <code>provider</code> is <code>null</code>, or if + * <code>algorithm</code> is an empty string. */ - public static final KeyAgreement getInstance(String algorithm, - String provider) - throws NoSuchAlgorithmException, NoSuchProviderException + public static final KeyAgreement getInstance(String algorithm, String provider) + throws NoSuchAlgorithmException, NoSuchProviderException { + if (provider == null) + throw new IllegalArgumentException("provider MUST NOT be null"); Provider p = Security.getProvider(provider); if (p == null) - { - throw new NoSuchProviderException(provider); - } + throw new NoSuchProviderException(provider); return getInstance(algorithm, p); } /** - * Get an implementation of an algorithm from a specific provider. - * + * Return an implementation of an algorithm from a specific provider. + * * @param algorithm The name of the algorithm to get. - * @param provider The provider from which to get the implementation. + * @param provider The provider from which to get the implementation. * @return The proper KeyAgreement instance, if found. - * @throws java.security.NoSuchAlgorithmException If this provider - * does not implement the algorithm. + * @throws NoSuchAlgorithmException If this provider does not implement the + * algorithm. + * @throws IllegalArgumentException if either <code>algorithm</code> or + * <code>provider</code> is <code>null</code>, or if + * <code>algorithm</code> is an empty string. */ public static final KeyAgreement getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { + StringBuilder sb = new StringBuilder("KeyAgreement algorithm [") + .append(algorithm).append("] from provider[") + .append(provider).append("] could not be created"); + Throwable cause; try { - return new KeyAgreement((KeyAgreementSpi) - Engine.getInstance(SERVICE, algorithm, provider), - provider, algorithm); + Object spi = Engine.getInstance(SERVICE, algorithm, provider); + return new KeyAgreement((KeyAgreementSpi) spi, provider, algorithm); } - catch (InvocationTargetException ite) + catch (InvocationTargetException x) { - if (ite.getCause() == null) - throw new NoSuchAlgorithmException(algorithm); - if (ite.getCause() instanceof NoSuchAlgorithmException) - throw (NoSuchAlgorithmException) ite.getCause(); - throw new NoSuchAlgorithmException(algorithm); + cause = x.getCause(); + if (cause instanceof NoSuchAlgorithmException) + throw (NoSuchAlgorithmException) cause; + if (cause == null) + cause = x; } - catch (ClassCastException cce) + catch (ClassCastException x) { - throw new NoSuchAlgorithmException(algorithm); + cause = x; } + NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString()); + x.initCause(cause); + throw x; } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Do a phase in the key agreement. The number of times this method is * called depends upon the algorithm and the number of parties |