KeyAgreement / Mac : Change provider selection methods.

This mirrors changes made to Signature / Cipher.

Change-Id: I60b3447308165a1683d22e2152d73340d45adba8
diff --git a/ojluni/src/main/java/javax/crypto/KeyAgreement.java b/ojluni/src/main/java/javax/crypto/KeyAgreement.java
index 7bebc67..5468fa2 100755
--- a/ojluni/src/main/java/javax/crypto/KeyAgreement.java
+++ b/ojluni/src/main/java/javax/crypto/KeyAgreement.java
@@ -87,14 +87,6 @@
     // The name of the key agreement algorithm.
     private final String algorithm;
 
-    // next service to try in provider selection
-    // null once provider is selected
-    private Service firstService;
-
-    // remaining services to try in provider selection
-    // null once provider is selected
-    private Iterator serviceIterator;
-
     private final Object lock;
 
     /**
@@ -112,9 +104,7 @@
         lock = null;
     }
 
-    private KeyAgreement(Service s, Iterator t, String algorithm) {
-        firstService = s;
-        serviceIterator = t;
+    private KeyAgreement(String algorithm) {
         this.algorithm = algorithm;
         lock = new Object();
     }
@@ -173,7 +163,7 @@
             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                 continue;
             }
-            return new KeyAgreement(s, t, algorithm);
+            return new KeyAgreement(algorithm);
         }
         throw new NoSuchAlgorithmException
                                 ("Algorithm " + algorithm + " not available");
@@ -295,14 +285,7 @@
                 }
             }
             Exception lastException = null;
-            while ((firstService != null) || serviceIterator.hasNext()) {
-                Service s;
-                if (firstService != null) {
-                    s = firstService;
-                    firstService = null;
-                } else {
-                    s = (Service)serviceIterator.next();
-                }
+            for (Service s : GetInstance.getServices("KeyAgreement", algorithm)) {
                 if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                     continue;
                 }
@@ -314,8 +297,6 @@
                     spi = (KeyAgreementSpi)obj;
                     provider = s.getProvider();
                     // not needed any more
-                    firstService = null;
-                    serviceIterator = null;
                     return;
                 } catch (Exception e) {
                     lastException = e;
@@ -347,19 +328,12 @@
             AlgorithmParameterSpec params, SecureRandom random)
             throws InvalidKeyException, InvalidAlgorithmParameterException {
         synchronized (lock) {
-            if (spi != null) {
+            if (spi != null && key == null) {
                 implInit(spi, initType, key, params, random);
                 return;
             }
             Exception lastException = null;
-            while ((firstService != null) || serviceIterator.hasNext()) {
-                Service s;
-                if (firstService != null) {
-                    s = firstService;
-                    firstService = null;
-                } else {
-                    s = (Service)serviceIterator.next();
-                }
+            for (Service s : GetInstance.getServices("KeyAgreement", algorithm)) {
                 // if provider says it does not support this key, ignore it
                 if (s.supportsParameter(key) == false) {
                     continue;
@@ -372,8 +346,6 @@
                     implInit(spi, initType, key, params, random);
                     provider = s.getProvider();
                     this.spi = spi;
-                    firstService = null;
-                    serviceIterator = null;
                     return;
                 } catch (Exception e) {
                     // NoSuchAlgorithmException from newInstance()
@@ -457,7 +429,7 @@
      */
     public final void init(Key key, SecureRandom random)
             throws InvalidKeyException {
-        if (spi != null) {
+        if (spi != null && (key == null || lock == null)) {
             spi.engineInit(key, random);
         } else {
             try {
diff --git a/ojluni/src/main/java/javax/crypto/Mac.java b/ojluni/src/main/java/javax/crypto/Mac.java
index b178988..6781b59 100755
--- a/ojluni/src/main/java/javax/crypto/Mac.java
+++ b/ojluni/src/main/java/javax/crypto/Mac.java
@@ -89,14 +89,6 @@
     // Has this object been initialized?
     private boolean initialized = false;
 
-    // next service to try in provider selection
-    // null once provider is selected
-    private Service firstService;
-
-    // remaining services to try in provider selection
-    // null once provider is selected
-    private Iterator serviceIterator;
-
     private final Object lock;
 
     /**
@@ -110,13 +102,10 @@
         this.spi = macSpi;
         this.provider = provider;
         this.algorithm = algorithm;
-        serviceIterator = null;
         lock = null;
     }
 
-    private Mac(Service s, Iterator t, String algorithm) {
-        firstService = s;
-        serviceIterator = t;
+    private Mac(String algorithm) {
         this.algorithm = algorithm;
         lock = new Object();
     }
@@ -171,7 +160,7 @@
             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                 continue;
             }
-            return new Mac(s, t, algorithm);
+            return new Mac(algorithm);
         }
         throw new NoSuchAlgorithmException
                                 ("Algorithm " + algorithm + " not available");
@@ -262,7 +251,7 @@
      * is not the first method called.
      */
     void chooseFirstProvider() {
-        if ((spi != null) || (serviceIterator == null)) {
+        if (spi != null || lock == null) {
             return;
         }
         synchronized (lock) {
@@ -282,14 +271,7 @@
                 }
             }
             Exception lastException = null;
-            while ((firstService != null) || serviceIterator.hasNext()) {
-                Service s;
-                if (firstService != null) {
-                    s = firstService;
-                    firstService = null;
-                } else {
-                    s = (Service)serviceIterator.next();
-                }
+            for (Service s : GetInstance.getServices("Mac", algorithm)) {
                 if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                     continue;
                 }
@@ -300,9 +282,6 @@
                     }
                     spi = (MacSpi)obj;
                     provider = s.getProvider();
-                    // not needed any more
-                    firstService = null;
-                    serviceIterator = null;
                     return;
                 } catch (NoSuchAlgorithmException e) {
                     lastException = e;
@@ -320,19 +299,12 @@
     private void chooseProvider(Key key, AlgorithmParameterSpec params)
             throws InvalidKeyException, InvalidAlgorithmParameterException {
         synchronized (lock) {
-            if (spi != null) {
+            if (spi != null && (key == null || lock == null)) {
                 spi.engineInit(key, params);
                 return;
             }
             Exception lastException = null;
-            while ((firstService != null) || serviceIterator.hasNext()) {
-                Service s;
-                if (firstService != null) {
-                    s = firstService;
-                    firstService = null;
-                } else {
-                    s = (Service)serviceIterator.next();
-                }
+            for (Service s : GetInstance.getServices("Mac", algorithm)) {
                 // if provider says it does not support this key, ignore it
                 if (s.supportsParameter(key) == false) {
                     continue;
@@ -345,8 +317,6 @@
                     spi.engineInit(key, params);
                     provider = s.getProvider();
                     this.spi = spi;
-                    firstService = null;
-                    serviceIterator = null;
                     return;
                 } catch (Exception e) {
                     // NoSuchAlgorithmException from newInstance()
@@ -404,7 +374,7 @@
      */
     public final void init(Key key) throws InvalidKeyException {
         try {
-            if (spi != null) {
+            if (spi != null && (key == null || lock == null)) {
                 spi.engineInit(key, null);
             } else {
                 chooseProvider(key, null);
@@ -429,7 +399,7 @@
      */
     public final void init(Key key, AlgorithmParameterSpec params)
             throws InvalidKeyException, InvalidAlgorithmParameterException {
-        if (spi != null) {
+        if (spi != null && (key == null || lock == null)) {
             spi.engineInit(key, params);
         } else {
             chooseProvider(key, params);