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.implementations;
24 
25 import java.security.cert.Certificate;
26 import java.security.cert.X509Certificate;
27 import java.util.Iterator;
28 import java.util.NoSuchElementException;
29 
30 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverSpi;
31 
32 /**
33  * This {@link StorageResolverSpi} makes a single {@link X509Certificate}
34  * available to the {@link com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver}.
35  */
36 public class SingleCertificateResolver extends StorageResolverSpi {
37 
38     /** Field certificate */
39     private X509Certificate certificate;
40 
41     /**
42      * @param x509cert the single {@link X509Certificate}
43      */
SingleCertificateResolver(X509Certificate x509cert)44     public SingleCertificateResolver(X509Certificate x509cert) {
45         this.certificate = x509cert;
46     }
47 
48     /** {@inheritDoc} */
getIterator()49     public Iterator<Certificate> getIterator() {
50         return new InternalIterator(this.certificate);
51     }
52 
53     /**
54      * Class InternalIterator
55      */
56     static class InternalIterator implements Iterator<Certificate> {
57 
58         /** Field alreadyReturned */
59         boolean alreadyReturned = false;
60 
61         /** Field certificate */
62         X509Certificate certificate = null;
63 
64         /**
65          * Constructor InternalIterator
66          *
67          * @param x509cert
68          */
InternalIterator(X509Certificate x509cert)69         public InternalIterator(X509Certificate x509cert) {
70             this.certificate = x509cert;
71         }
72 
73         /** {@inheritDoc} */
hasNext()74         public boolean hasNext() {
75             return !this.alreadyReturned;
76         }
77 
78         /** {@inheritDoc} */
next()79         public Certificate next() {
80             if (this.alreadyReturned) {
81                 throw new NoSuchElementException();
82             }
83             this.alreadyReturned = true;
84             return this.certificate;
85         }
86 
87         /**
88          * Method remove
89          */
remove()90         public void remove() {
91             throw new UnsupportedOperationException("Can't remove keys from KeyStore");
92         }
93     }
94 }
95