Showing the changes to java/pkg/sun/security/provider/certpath/CollectionCertStore.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,190 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.security.provider.certpath; | |
| 27 | + | |
| 28 | +import java.security.InvalidAlgorithmParameterException; | |
| 29 | +import java.security.cert.Certificate; | |
| 30 | +import java.security.cert.CRL; | |
| 31 | +import java.util.Collection; | |
| 32 | +import java.util.ConcurrentModificationException; | |
| 33 | +import java.util.HashSet; | |
| 34 | +import java.util.Iterator; | |
| 35 | +import java.security.cert.CertSelector; | |
| 36 | +import java.security.cert.CertStore; | |
| 37 | +import java.security.cert.CertStoreException; | |
| 38 | +import java.security.cert.CertStoreParameters; | |
| 39 | +import java.security.cert.CollectionCertStoreParameters; | |
| 40 | +import java.security.cert.CRLSelector; | |
| 41 | +import java.security.cert.CertStoreSpi; | |
| 42 | + | |
| 43 | +/** | |
| 44 | + * A <code>CertStore</code> that retrieves <code>Certificates</code> and | |
| 45 | + * <code>CRL</code>s from a <code>Collection</code>. | |
| 46 | + * <p> | |
| 47 | + * Before calling the {@link #engineGetCertificates engineGetCertificates} or | |
| 48 | + * {@link #engineGetCRLs engineGetCRLs} methods, the | |
| 49 | + * {@link #CollectionCertStore(CertStoreParameters) | |
| 50 | + * CollectionCertStore(CertStoreParameters)} constructor is called to | |
| 51 | + * create the <code>CertStore</code> and establish the | |
| 52 | + * <code>Collection</code> from which <code>Certificate</code>s and | |
| 53 | + * <code>CRL</code>s will be retrieved. If the specified | |
| 54 | + * <code>Collection</code> contains an object that is not a | |
| 55 | + * <code>Certificate</code> or <code>CRL</code>, that object will be | |
| 56 | + * ignored. | |
| 57 | + * <p> | |
| 58 | + * <b>Concurrent Access</b> | |
| 59 | + * <p> | |
| 60 | + * As described in the javadoc for <code>CertStoreSpi</code>, the | |
| 61 | + * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods | |
| 62 | + * must be thread-safe. That is, multiple threads may concurrently | |
| 63 | + * invoke these methods on a single <code>CollectionCertStore</code> | |
| 64 | + * object (or more than one) with no ill effects. | |
| 65 | + * <p> | |
| 66 | + * This is achieved by requiring that the <code>Collection</code> passed to | |
| 67 | + * the {@link #CollectionCertStore(CertStoreParameters) | |
| 68 | + * CollectionCertStore(CertStoreParameters)} constructor (via the | |
| 69 | + * <code>CollectionCertStoreParameters</code> object) must have fail-fast | |
| 70 | + * iterators. Simultaneous modifications to the <code>Collection</code> can thus be | |
| 71 | + * detected and certificate or CRL retrieval can be retried. The fact that | |
| 72 | + * <code>Certificate</code>s and <code>CRL</code>s must be thread-safe is also | |
| 73 | + * essential. | |
| 74 | + * | |
| 75 | + * @see java.security.cert.CertStore | |
| 76 | + * | |
| 77 | + * @since 1.4 | |
| 78 | + * @author Steve Hanna | |
| 79 | + */ | |
| 80 | +public class CollectionCertStore extends CertStoreSpi { | |
| 81 | + | |
| 82 | + private Collection<?> coll; | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Creates a <code>CertStore</code> with the specified parameters. | |
| 86 | + * For this class, the parameters object must be an instance of | |
| 87 | + * <code>CollectionCertStoreParameters</code>. The <code>Collection</code> | |
| 88 | + * included in the <code>CollectionCertStoreParameters</code> object | |
| 89 | + * must be thread-safe. | |
| 90 | + * | |
| 91 | + * @param params the algorithm parameters | |
| 92 | + * @exception InvalidAlgorithmParameterException if params is not an | |
| 93 | + * instance of <code>CollectionCertStoreParameters</code> | |
| 94 | + */ | |
| 95 | + public CollectionCertStore(CertStoreParameters params) | |
| 96 | + throws InvalidAlgorithmParameterException | |
| 97 | + { | |
| 98 | + super(params); | |
| 99 | + if (!(params instanceof CollectionCertStoreParameters)) | |
| 100 | + throw new InvalidAlgorithmParameterException( | |
| 101 | + "parameters must be CollectionCertStoreParameters"); | |
| 102 | + coll = ((CollectionCertStoreParameters) params).getCollection(); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Returns a <code>Collection</code> of <code>Certificate</code>s that | |
| 107 | + * match the specified selector. If no <code>Certificate</code>s | |
| 108 | + * match the selector, an empty <code>Collection</code> will be returned. | |
| 109 | + * | |
| 110 | + * @param selector a <code>CertSelector</code> used to select which | |
| 111 | + * <code>Certificate</code>s should be returned. Specify <code>null</code> | |
| 112 | + * to return all <code>Certificate</code>s. | |
| 113 | + * @return a <code>Collection</code> of <code>Certificate</code>s that | |
| 114 | + * match the specified selector | |
| 115 | + * @throws CertStoreException if an exception occurs | |
| 116 | + */ | |
| 117 | + public Collection<Certificate> engineGetCertificates | |
| 118 | + (CertSelector selector) throws CertStoreException { | |
| 119 | + if (coll == null) { | |
| 120 | + throw new CertStoreException("Collection is null"); | |
| 121 | + } | |
| 122 | + // Tolerate a few ConcurrentModificationExceptions | |
| 123 | + for (int c = 0; c < 10; c++) { | |
| 124 | + try { | |
| 125 | + HashSet<Certificate> result = new HashSet<Certificate>(); | |
| 126 | + Iterator<?> i = coll.iterator(); | |
| 127 | + if (selector != null) { | |
| 128 | + while (i.hasNext()) { | |
| 129 | + Object o = i.next(); | |
| 130 | + if ((o instanceof Certificate) && | |
| 131 | + selector.match((Certificate) o)) | |
| 132 | + result.add((Certificate)o); | |
| 133 | + } | |
| 134 | + } else { | |
| 135 | + while (i.hasNext()) { | |
| 136 | + Object o = i.next(); | |
| 137 | + if (o instanceof Certificate) | |
| 138 | + result.add((Certificate)o); | |
| 139 | + } | |
| 140 | + } | |
| 141 | + return(result); | |
| 142 | + } catch (ConcurrentModificationException e) { } | |
| 143 | + } | |
| 144 | + throw new ConcurrentModificationException("Too many " | |
| 145 | + + "ConcurrentModificationExceptions"); | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Returns a <code>Collection</code> of <code>CRL</code>s that | |
| 150 | + * match the specified selector. If no <code>CRL</code>s | |
| 151 | + * match the selector, an empty <code>Collection</code> will be returned. | |
| 152 | + * | |
| 153 | + * @param selector a <code>CRLSelector</code> used to select which | |
| 154 | + * <code>CRL</code>s should be returned. Specify <code>null</code> | |
| 155 | + * to return all <code>CRL</code>s. | |
| 156 | + * @return a <code>Collection</code> of <code>CRL</code>s that | |
| 157 | + * match the specified selector | |
| 158 | + * @throws CertStoreException if an exception occurs | |
| 159 | + */ | |
| 160 | + public Collection<CRL> engineGetCRLs(CRLSelector selector) | |
| 161 | + throws CertStoreException | |
| 162 | + { | |
| 163 | + if (coll == null) | |
| 164 | + throw new CertStoreException("Collection is null"); | |
| 165 | + | |
| 166 | + // Tolerate a few ConcurrentModificationExceptions | |
| 167 | + for (int c = 0; c < 10; c++) { | |
| 168 | + try { | |
| 169 | + HashSet<CRL> result = new HashSet<CRL>(); | |
| 170 | + Iterator<?> i = coll.iterator(); | |
| 171 | + if (selector != null) { | |
| 172 | + while (i.hasNext()) { | |
| 173 | + Object o = i.next(); | |
| 174 | + if ((o instanceof CRL) && selector.match((CRL) o)) | |
| 175 | + result.add((CRL)o); | |
| 176 | + } | |
| 177 | + } else { | |
| 178 | + while (i.hasNext()) { | |
| 179 | + Object o = i.next(); | |
| 180 | + if (o instanceof CRL) | |
| 181 | + result.add((CRL)o); | |
| 182 | + } | |
| 183 | + } | |
| 184 | + return(result); | |
| 185 | + } catch (ConcurrentModificationException e) { } | |
| 186 | + } | |
| 187 | + throw new ConcurrentModificationException("Too many " | |
| 188 | + + "ConcurrentModificationExceptions"); | |
| 189 | + } | |
| 190 | +} | |