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