--- src/org/apache/xml/security/keys/storage/StorageResolver.java (revision 955619) +++ src/org/apache/xml/security/keys/storage/StorageResolver.java (working copy) @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import org.apache.xml.security.keys.storage.implementations.KeyStoreResolver; import org.apache.xml.security.keys.storage.implementations.SingleCertificateResolver; @@ -116,8 +117,7 @@ /** * Method getIterator * @return the iterator for the resolvers. - * - */ + */ public Iterator getIterator() { if (this._iterator == null) { @@ -132,7 +132,7 @@ /** * Method hasNext * - * @return true if there is more elements. + * @return true if there are more elements. */ public boolean hasNext() { @@ -156,6 +156,7 @@ /** * Class StorageResolverIterator + * This iterates over all the Certificates found in all the resolvers. * * @author $Author$ * @version $Revision$ @@ -165,23 +166,40 @@ /** Field _resolvers */ Iterator _resolvers = null; + /** Field _currentResolver */ + Iterator _currentResolver = null; + /** - * Constructor FilesystemIterator + * Constructor StorageResolverIterator * * @param resolvers */ public StorageResolverIterator(Iterator resolvers) { this._resolvers = resolvers; + _currentResolver = findNextResolver(); } /** @inheritDoc */ public boolean hasNext() { - return _resolvers.hasNext(); + if (_currentResolver == null) { + return false; + } + + if (_currentResolver.hasNext()) { + return true; + } + + _currentResolver = findNextResolver(); + return (_currentResolver != null); } /** @inheritDoc */ public Object next() { - return _resolvers.next(); + if (hasNext()) { + return _currentResolver.next(); + } + + throw new NoSuchElementException(); } /** @@ -191,5 +209,19 @@ throw new UnsupportedOperationException( "Can't remove keys from KeyStore"); } + + // Find the next storage with at least one element and return its Iterator + private Iterator findNextResolver() { + + while (_resolvers.hasNext()) { + StorageResolverSpi resolverSpi = (StorageResolverSpi)_resolvers.next(); + Iterator iter = resolverSpi.getIterator(); + if (iter.hasNext()) { + return iter; + } + } + + return null; + } } } --- src_unitTests/org/apache/xml/security/test/ModuleTest.java (revision 955619) +++ src_unitTests/org/apache/xml/security/test/ModuleTest.java (working copy) @@ -61,6 +61,7 @@ suite.addTest(org.apache.xml.security.test.keys.content.x509.XMLX509IssuerSerialTest.suite()); suite.addTest(org.apache.xml.security.test.keys.content.x509.XMLX509CertificateTest.suite()); suite.addTest(org.apache.xml.security.test.keys.storage.KeyStoreResolverTest.suite()); + suite.addTest(org.apache.xml.security.test.keys.storage.StorageResolverTest.suite()); // suite.addTest(org.apache.xml.security.test.algorithms.implementations.KeyWrapTest.suite()); // suite.addTest(org.apache.xml.security.test.algorithms.implementations.BlockEncryptionTest.suite()); //J+ --- src_unitTests/org/apache/xml/security/test/keys/storage/StorageResolverTest.java (revision 0) +++ src_unitTests/org/apache/xml/security/test/keys/storage/StorageResolverTest.java (revision 0) @@ -0,0 +1,82 @@ +/* + * Copyright 2008-2010 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.xml.security.test.keys.storage; + +import java.io.FileInputStream; +import java.security.KeyStore; +import java.security.cert.X509Certificate; +import java.util.NoSuchElementException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.xml.security.keys.storage.StorageResolver; + +/** + * KeyStore StorageResolver test. + */ +public class StorageResolverTest extends TestCase { + + private static final String BASEDIR = System.getProperty("basedir"); + private static final String SEP = System.getProperty("file.separator"); + + public StorageResolverTest() { + super("KeyStoreResolverTest"); + } + + public StorageResolverTest(String name) { + super(name); + } + + public static Test suite() { + return new TestSuite(StorageResolverTest.class); + } + + public void testStorageResolver() throws Exception { + + String inputDir = BASEDIR + SEP + "data" + SEP + + "org" + SEP + "apache" + SEP + "xml" + SEP + "security" + SEP + + "samples" + SEP + "input"; + + FileInputStream inStream = new FileInputStream(inputDir + SEP + "keystore.jks"); + KeyStore ks = KeyStore.getInstance("JKS"); + ks.load(inStream, "xmlsecurity".toCharArray()); + + FileInputStream inStream2 = new FileInputStream(inputDir + SEP + "keystore2.jks"); + KeyStore ks2 = KeyStore.getInstance("JCEKS"); + ks2.load(inStream2, "xmlsecurity".toCharArray()); + + StorageResolver storage = new StorageResolver(ks); + storage.add(ks2); + + int count = 0; + while (storage.hasNext()) { + X509Certificate cert = storage.next(); + assertNotNull(cert); + count++; + } + + assertEquals(4, count); + + try { + storage.next(); + fail("Expecting NoSuchElementException"); + } catch (NoSuchElementException e) { + } + } +}