0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider.certpath;
0N/A
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.security.cert.CRL;
0N/Aimport java.util.Collection;
0N/Aimport java.util.ConcurrentModificationException;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.security.cert.CertSelector;
0N/Aimport java.security.cert.CertStore;
0N/Aimport java.security.cert.CertStoreException;
0N/Aimport java.security.cert.CertStoreParameters;
0N/Aimport java.security.cert.CollectionCertStoreParameters;
0N/Aimport java.security.cert.CRLSelector;
0N/Aimport java.security.cert.CertStoreSpi;
0N/A
0N/A/**
0N/A * A <code>CertStore</code> that retrieves <code>Certificates</code> and
0N/A * <code>CRL</code>s from a <code>Collection</code>.
0N/A * <p>
0N/A * Before calling the {@link #engineGetCertificates engineGetCertificates} or
0N/A * {@link #engineGetCRLs engineGetCRLs} methods, the
0N/A * {@link #CollectionCertStore(CertStoreParameters)
0N/A * CollectionCertStore(CertStoreParameters)} constructor is called to
0N/A * create the <code>CertStore</code> and establish the
0N/A * <code>Collection</code> from which <code>Certificate</code>s and
0N/A * <code>CRL</code>s will be retrieved. If the specified
0N/A * <code>Collection</code> contains an object that is not a
0N/A * <code>Certificate</code> or <code>CRL</code>, that object will be
0N/A * ignored.
0N/A * <p>
0N/A * <b>Concurrent Access</b>
0N/A * <p>
0N/A * As described in the javadoc for <code>CertStoreSpi</code>, the
0N/A * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods
0N/A * must be thread-safe. That is, multiple threads may concurrently
0N/A * invoke these methods on a single <code>CollectionCertStore</code>
0N/A * object (or more than one) with no ill effects.
0N/A * <p>
0N/A * This is achieved by requiring that the <code>Collection</code> passed to
0N/A * the {@link #CollectionCertStore(CertStoreParameters)
0N/A * CollectionCertStore(CertStoreParameters)} constructor (via the
0N/A * <code>CollectionCertStoreParameters</code> object) must have fail-fast
0N/A * iterators. Simultaneous modifications to the <code>Collection</code> can thus be
0N/A * detected and certificate or CRL retrieval can be retried. The fact that
0N/A * <code>Certificate</code>s and <code>CRL</code>s must be thread-safe is also
0N/A * essential.
0N/A *
0N/A * @see java.security.cert.CertStore
0N/A *
0N/A * @since 1.4
0N/A * @author Steve Hanna
0N/A */
0N/Apublic class CollectionCertStore extends CertStoreSpi {
0N/A
0N/A private Collection<?> coll;
0N/A
0N/A /**
0N/A * Creates a <code>CertStore</code> with the specified parameters.
0N/A * For this class, the parameters object must be an instance of
0N/A * <code>CollectionCertStoreParameters</code>. The <code>Collection</code>
0N/A * included in the <code>CollectionCertStoreParameters</code> object
0N/A * must be thread-safe.
0N/A *
0N/A * @param params the algorithm parameters
0N/A * @exception InvalidAlgorithmParameterException if params is not an
0N/A * instance of <code>CollectionCertStoreParameters</code>
0N/A */
0N/A public CollectionCertStore(CertStoreParameters params)
0N/A throws InvalidAlgorithmParameterException
0N/A {
0N/A super(params);
0N/A if (!(params instanceof CollectionCertStoreParameters))
0N/A throw new InvalidAlgorithmParameterException(
0N/A "parameters must be CollectionCertStoreParameters");
0N/A coll = ((CollectionCertStoreParameters) params).getCollection();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>Collection</code> of <code>Certificate</code>s that
0N/A * match the specified selector. If no <code>Certificate</code>s
0N/A * match the selector, an empty <code>Collection</code> will be returned.
0N/A *
0N/A * @param selector a <code>CertSelector</code> used to select which
0N/A * <code>Certificate</code>s should be returned. Specify <code>null</code>
0N/A * to return all <code>Certificate</code>s.
0N/A * @return a <code>Collection</code> of <code>Certificate</code>s that
0N/A * match the specified selector
0N/A * @throws CertStoreException if an exception occurs
0N/A */
0N/A public Collection<Certificate> engineGetCertificates
0N/A (CertSelector selector) throws CertStoreException {
0N/A if (coll == null) {
0N/A throw new CertStoreException("Collection is null");
0N/A }
0N/A // Tolerate a few ConcurrentModificationExceptions
0N/A for (int c = 0; c < 10; c++) {
0N/A try {
0N/A HashSet<Certificate> result = new HashSet<Certificate>();
0N/A Iterator<?> i = coll.iterator();
0N/A if (selector != null) {
0N/A while (i.hasNext()) {
0N/A Object o = i.next();
0N/A if ((o instanceof Certificate) &&
0N/A selector.match((Certificate) o))
0N/A result.add((Certificate)o);
0N/A }
0N/A } else {
0N/A while (i.hasNext()) {
0N/A Object o = i.next();
0N/A if (o instanceof Certificate)
0N/A result.add((Certificate)o);
0N/A }
0N/A }
0N/A return(result);
0N/A } catch (ConcurrentModificationException e) { }
0N/A }
0N/A throw new ConcurrentModificationException("Too many "
0N/A + "ConcurrentModificationExceptions");
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>Collection</code> of <code>CRL</code>s that
0N/A * match the specified selector. If no <code>CRL</code>s
0N/A * match the selector, an empty <code>Collection</code> will be returned.
0N/A *
0N/A * @param selector a <code>CRLSelector</code> used to select which
0N/A * <code>CRL</code>s should be returned. Specify <code>null</code>
0N/A * to return all <code>CRL</code>s.
0N/A * @return a <code>Collection</code> of <code>CRL</code>s that
0N/A * match the specified selector
0N/A * @throws CertStoreException if an exception occurs
0N/A */
0N/A public Collection<CRL> engineGetCRLs(CRLSelector selector)
0N/A throws CertStoreException
0N/A {
0N/A if (coll == null)
0N/A throw new CertStoreException("Collection is null");
0N/A
0N/A // Tolerate a few ConcurrentModificationExceptions
0N/A for (int c = 0; c < 10; c++) {
0N/A try {
0N/A HashSet<CRL> result = new HashSet<CRL>();
0N/A Iterator<?> i = coll.iterator();
0N/A if (selector != null) {
0N/A while (i.hasNext()) {
0N/A Object o = i.next();
0N/A if ((o instanceof CRL) && selector.match((CRL) o))
0N/A result.add((CRL)o);
0N/A }
0N/A } else {
0N/A while (i.hasNext()) {
0N/A Object o = i.next();
0N/A if (o instanceof CRL)
0N/A result.add((CRL)o);
0N/A }
0N/A }
0N/A return(result);
0N/A } catch (ConcurrentModificationException e) { }
0N/A }
0N/A throw new ConcurrentModificationException("Too many "
0N/A + "ConcurrentModificationExceptions");
0N/A }
0N/A}