1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 package com.sun.org.apache.xml.internal.security.keys.storage; 22 23 import java.security.KeyStore; 24 import java.security.cert.X509Certificate; 25 import java.util.ArrayList; 26 import java.util.Iterator; 27 import java.util.List; 28 29 import com.sun.org.apache.xml.internal.security.keys.storage.implementations.KeyStoreResolver; 30 import com.sun.org.apache.xml.internal.security.keys.storage.implementations.SingleCertificateResolver; 31 32 33 /** 34 * This class collects customized resolvers for Certificates. 35 * 36 * @author $Author: mullan $ 37 */ 38 public class StorageResolver { 39 40 /** {@link java.util.logging} logging facility */ 41 static java.util.logging.Logger log = 42 java.util.logging.Logger.getLogger(StorageResolver.class.getName()); 43 44 /** Field _storageResolvers */ 45 List<Object> _storageResolvers = null; 46 47 /** Field _iterator */ 48 Iterator<Object> _iterator = null; 49 50 /** 51 * Constructor StorageResolver 52 * 53 */ 54 public StorageResolver() {} 55 56 /** 57 * Constructor StorageResolver 58 * 59 * @param resolver 60 */ 61 public StorageResolver(StorageResolverSpi resolver) { 62 this.add(resolver); 63 } 64 65 /** 66 * Method addResolver 67 * 68 * @param resolver 69 */ 70 public void add(StorageResolverSpi resolver) { 71 if (_storageResolvers==null) 72 _storageResolvers=new ArrayList<Object>(); 73 this._storageResolvers.add(resolver); 74 75 this._iterator = null; 76 } 77 78 /** 79 * Constructor StorageResolver 80 * 81 * @param keyStore 82 */ 83 public StorageResolver(KeyStore keyStore) { 84 this.add(keyStore); 85 } 86 87 /** 88 * Method addKeyStore 89 * 90 * @param keyStore 91 */ 92 public void add(KeyStore keyStore) { 93 94 try { 95 this.add(new KeyStoreResolver(keyStore)); 96 } catch (StorageResolverException ex) { 97 log.log(java.util.logging.Level.SEVERE, "Could not add KeyStore because of: ", ex); 98 } 99 } 100 101 /** 102 * Constructor StorageResolver 103 * 104 * @param x509certificate 105 */ 106 public StorageResolver(X509Certificate x509certificate) { 107 this.add(x509certificate); 108 } 109 110 /** 111 * Method addCertificate 112 * 113 * @param x509certificate 114 */ 115 public void add(X509Certificate x509certificate) { 116 this.add(new SingleCertificateResolver(x509certificate)); 117 } 118 119 /** 120 * Method getIterator 121 * @return the iterator for the resolvers. 122 * 123 */ 124 public Iterator<Object> getIterator() { 125 126 if (this._iterator == null) { 127 if (_storageResolvers==null) 128 _storageResolvers=new ArrayList<Object>(); 129 this._iterator = new StorageResolverIterator(this._storageResolvers.iterator()); 130 } 131 132 return this._iterator; 133 } 134 135 /** 136 * Method hasNext 137 * 138 * @return true if there is more elements. 139 */ 140 public boolean hasNext() { 141 142 if (this._iterator == null) { 143 if (_storageResolvers==null) 144 _storageResolvers=new ArrayList<Object>(); 145 this._iterator = new StorageResolverIterator(this._storageResolvers.iterator()); 146 } 147 148 return this._iterator.hasNext(); 149 } 150 151 /** 152 * Method next 153 * 154 * @return the next element 155 */ 156 public X509Certificate next() { 157 return (X509Certificate) this._iterator.next(); 158 } 159 160 /** 161 * Class StorageResolverIterator 162 * 163 * @author $Author: mullan $ 164 * @version $Revision: 1.5 $ 165 */ 166 static class StorageResolverIterator implements Iterator<Object> { 167 168 /** Field _resolvers */ 169 Iterator<Object> _resolvers = null; 170 171 /** 172 * Constructor FilesystemIterator 173 * 174 * @param resolvers 175 */ 176 public StorageResolverIterator(Iterator<Object> resolvers) { 177 this._resolvers = resolvers; 178 } 179 180 /** @inheritDoc */ 181 public boolean hasNext() { 182 return _resolvers.hasNext(); 183 } 184 185 /** @inheritDoc */ 186 public Object next() { 187 return _resolvers.next(); 188 } 189 190 /** 191 * Method remove 192 */ 193 public void remove() { 194 throw new UnsupportedOperationException( 195 "Can't remove keys from KeyStore"); 196 } 197 } 198 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.keys.storage; 24 25 import java.security.KeyStore; 26 import java.security.cert.Certificate; 27 import java.security.cert.X509Certificate; 28 import java.util.ArrayList; 29 import java.util.Iterator; 30 import java.util.List; 31 import java.util.NoSuchElementException; 32 33 import com.sun.org.apache.xml.internal.security.keys.storage.implementations.KeyStoreResolver; 34 import com.sun.org.apache.xml.internal.security.keys.storage.implementations.SingleCertificateResolver; 35 36 /** 37 * This class collects customized resolvers for Certificates. 38 */ 39 public class StorageResolver { 40 41 /** {@link org.apache.commons.logging} logging facility */ 42 private static java.util.logging.Logger log = 43 java.util.logging.Logger.getLogger(StorageResolver.class.getName()); 44 45 /** Field storageResolvers */ 46 private List<StorageResolverSpi> storageResolvers = null; 47 48 /** 49 * Constructor StorageResolver 50 * 51 */ 52 public StorageResolver() {} 53 54 /** 55 * Constructor StorageResolver 56 * 57 * @param resolver 58 */ 59 public StorageResolver(StorageResolverSpi resolver) { 60 this.add(resolver); 61 } 62 63 /** 64 * Method addResolver 65 * 66 * @param resolver 67 */ 68 public void add(StorageResolverSpi resolver) { 69 if (storageResolvers == null) { 70 storageResolvers = new ArrayList<StorageResolverSpi>(); 71 } 72 this.storageResolvers.add(resolver); 73 } 74 75 /** 76 * Constructor StorageResolver 77 * 78 * @param keyStore 79 */ 80 public StorageResolver(KeyStore keyStore) { 81 this.add(keyStore); 82 } 83 84 /** 85 * Method addKeyStore 86 * 87 * @param keyStore 88 */ 89 public void add(KeyStore keyStore) { 90 try { 91 this.add(new KeyStoreResolver(keyStore)); 92 } catch (StorageResolverException ex) { 93 log.log(java.util.logging.Level.SEVERE, "Could not add KeyStore because of: ", ex); 94 } 95 } 96 97 /** 98 * Constructor StorageResolver 99 * 100 * @param x509certificate 101 */ 102 public StorageResolver(X509Certificate x509certificate) { 103 this.add(x509certificate); 104 } 105 106 /** 107 * Method addCertificate 108 * 109 * @param x509certificate 110 */ 111 public void add(X509Certificate x509certificate) { 112 this.add(new SingleCertificateResolver(x509certificate)); 113 } 114 115 /** 116 * Method getIterator 117 * @return the iterator for the resolvers. 118 */ 119 public Iterator<Certificate> getIterator() { 120 return new StorageResolverIterator(this.storageResolvers.iterator()); 121 } 122 123 /** 124 * Class StorageResolverIterator 125 * This iterates over all the Certificates found in all the resolvers. 126 */ 127 static class StorageResolverIterator implements Iterator<Certificate> { 128 129 /** Field resolvers */ 130 Iterator<StorageResolverSpi> resolvers = null; 131 132 /** Field currentResolver */ 133 Iterator<Certificate> currentResolver = null; 134 135 /** 136 * Constructor StorageResolverIterator 137 * 138 * @param resolvers 139 */ 140 public StorageResolverIterator(Iterator<StorageResolverSpi> resolvers) { 141 this.resolvers = resolvers; 142 currentResolver = findNextResolver(); 143 } 144 145 /** @inheritDoc */ 146 public boolean hasNext() { 147 if (currentResolver == null) { 148 return false; 149 } 150 151 if (currentResolver.hasNext()) { 152 return true; 153 } 154 155 currentResolver = findNextResolver(); 156 return (currentResolver != null); 157 } 158 159 /** @inheritDoc */ 160 public Certificate next() { 161 if (hasNext()) { 162 return currentResolver.next(); 163 } 164 165 throw new NoSuchElementException(); 166 } 167 168 /** 169 * Method remove 170 */ 171 public void remove() { 172 throw new UnsupportedOperationException("Can't remove keys from KeyStore"); 173 } 174 175 // Find the next storage with at least one element and return its Iterator 176 private Iterator<Certificate> findNextResolver() { 177 while (resolvers.hasNext()) { 178 StorageResolverSpi resolverSpi = resolvers.next(); 179 Iterator<Certificate> iter = resolverSpi.getIterator(); 180 if (iter.hasNext()) { 181 return iter; 182 } 183 } 184 185 return null; 186 } 187 } 188 } |