1 /*
   2  * Copyright (c) 2000, 2013, 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 java.security.cert;
  27 
  28 import java.io.Serializable;
  29 import java.util.Collection;
  30 import java.util.Collections;
  31 
  32 /**
  33  * Parameters used as input for the Collection {@code CertStore}
  34  * algorithm.
  35  * <p>
  36  * This class is used to provide necessary configuration parameters
  37  * to implementations of the Collection {@code CertStore}
  38  * algorithm. The only parameter included in this class is the
  39  * {@code Collection} from which the {@code CertStore} will
  40  * retrieve certificates and CRLs.
  41  * <p>
  42  * <b>Concurrent Access</b>
  43  * <p>
  44  * Unless otherwise specified, the methods defined in this class are not
  45  * thread-safe. Multiple threads that need to access a single
  46  * object concurrently should synchronize amongst themselves and
  47  * provide the necessary locking. Multiple threads each manipulating
  48  * separate objects need not synchronize.
  49  *
  50  * @since       1.4
  51  * @author      Steve Hanna
  52  * @see         java.util.Collection
  53  * @see         CertStore
  54  */
  55 public class CollectionCertStoreParameters
  56     implements CertStoreParameters {
  57 
  58     private Collection<?> coll;
  59 
  60     /**
  61      * Creates an instance of {@code CollectionCertStoreParameters}
  62      * which will allow certificates and CRLs to be retrieved from the
  63      * specified {@code Collection}. If the specified
  64      * {@code Collection} contains an object that is not a
  65      * {@code Certificate} or {@code CRL}, that object will be
  66      * ignored by the Collection {@code CertStore}.
  67      * <p>
  68      * The {@code Collection} is <b>not</b> copied. Instead, a
  69      * reference is used. This allows the caller to subsequently add or
  70      * remove {@code Certificates} or {@code CRL}s from the
  71      * {@code Collection}, thus changing the set of
  72      * {@code Certificates} or {@code CRL}s available to the
  73      * Collection {@code CertStore}. The Collection {@code CertStore}
  74      * will not modify the contents of the {@code Collection}.
  75      * <p>
  76      * If the {@code Collection} will be modified by one thread while
  77      * another thread is calling a method of a Collection {@code CertStore}
  78      * that has been initialized with this {@code Collection}, the
  79      * {@code Collection} must have fail-fast iterators.
  80      *
  81      * @param collection a {@code Collection} of
  82      *        {@code Certificate}s and {@code CRL}s
  83      * @exception NullPointerException if {@code collection} is
  84      * {@code null}
  85      */
  86     public CollectionCertStoreParameters(Collection<?> collection) {
  87         if (collection == null)
  88             throw new NullPointerException();
  89         coll = collection;
  90     }
  91 
  92     /**
  93      * Creates an instance of {@code CollectionCertStoreParameters} with
  94      * the default parameter values (an empty and immutable
  95      * {@code Collection}).
  96      */
  97     public CollectionCertStoreParameters() {
  98         coll = Collections.EMPTY_SET;
  99     }
 100 
 101     /**
 102      * Returns the {@code Collection} from which {@code Certificate}s
 103      * and {@code CRL}s are retrieved. This is <b>not</b> a copy of the
 104      * {@code Collection}, it is a reference. This allows the caller to
 105      * subsequently add or remove {@code Certificates} or
 106      * {@code CRL}s from the {@code Collection}.
 107      *
 108      * @return the {@code Collection} (never null)
 109      */
 110     public Collection<?> getCollection() {
 111         return coll;
 112     }
 113 
 114     /**
 115      * Returns a copy of this object. Note that only a reference to the
 116      * {@code Collection} is copied, and not the contents.
 117      *
 118      * @return the copy
 119      */
 120     public Object clone() {
 121         try {
 122             return super.clone();
 123         } catch (CloneNotSupportedException e) {
 124             /* Cannot happen */
 125             throw new InternalError(e.toString(), e);
 126         }
 127     }
 128 
 129     /**
 130      * Returns a formatted string describing the parameters.
 131      *
 132      * @return a formatted string describing the parameters
 133      */
 134     public String toString() {
 135         StringBuilder sb = new StringBuilder();
 136         sb.append("CollectionCertStoreParameters: [\n");
 137         sb.append("  collection: ").append(coll).append('\n');
 138         sb.append(']');
 139         return sb.toString();
 140     }
 141 }