View Javadoc
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 }