1 /*
   2  * Copyright (c) 2015, 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.net.URI;
  29 
  30 /**
  31  * Parameters used as input for {@code CertStore} algorithms which use
  32  * information contained in a URI to retrieve certificates and CRLs.
  33  * <p>
  34  * This class is used to provide necessary configuration parameters
  35  * through a URI as defined in RFC 5280 to implementations of
  36  * {@code CertStore} algorithms.
  37  * <p>
  38  * <b>Concurrent Access</b>
  39  * <p>
  40  * Unless otherwise specified, the methods defined in this class are not
  41  * thread-safe. Multiple threads that need to access a single
  42  * object concurrently should synchronize amongst themselves and
  43  * provide the necessary locking. Multiple threads each manipulating
  44  * separate objects need not synchronize.
  45  *
  46  * @since       1.9
  47  * @see         CertStore
  48  * @see         java.net.URI
  49  */
  50 public final class URICertStoreParameters implements CertStoreParameters {
  51 
  52     /**
  53      * The uri, cannot be null
  54      */
  55     private final URI uri;
  56 
  57     /*
  58      * Hash code for this parameters.
  59      */
  60     private int myhash = -1;
  61 
  62     /**
  63      * Creates an instance of {@code URICertStoreParameters} with the
  64      * specified URI.
  65      *
  66      * @param uri the URI which contains configuration information.
  67      * @throws NullPointerException if {@code uri} is null
  68      */
  69     public URICertStoreParameters(URI uri) {
  70         if (uri == null) {
  71             throw new NullPointerException();
  72         }
  73         this.uri = uri;
  74     }
  75 
  76     /**
  77      * Returns the URI used to construct this
  78      * {@code URICertStoreParameters} object.
  79      *
  80      * @return the URI.
  81      */
  82     public URI getURI() {
  83         return uri;
  84     }
  85 
  86     /**
  87      * Returns a copy of this object. Changes to the copy will not affect
  88      * the original and vice versa.
  89      *
  90      * @return the copy
  91      */
  92     @Override
  93     public URICertStoreParameters clone() {
  94         try {
  95             return new URICertStoreParameters(uri);
  96         } catch (NullPointerException e) {
  97             /* Cannot happen */
  98             throw new InternalError(e.toString(), e);
  99         }
 100     }
 101 
 102     /**
 103      * Returns a hash code value for this parameters object.
 104      * The hash code is generated using the URI supplied at construction.
 105      *
 106      * @return a hash code value for this parameters.
 107      */
 108     @Override
 109     public int hashCode() {
 110         if (myhash == -1) {
 111             myhash = uri.hashCode()*7;
 112         }
 113         return myhash;
 114     }
 115 
 116     /**
 117      * Compares the specified object with this parameters object for equality.
 118      * Two URICertStoreParameters are considered equal if the URIs used
 119      * to construct them are equal.
 120      *
 121      * @param p the object to test for equality with this parameters.
 122      *
 123      * @return true if the specified object is equal to this parameters object.
 124      */
 125     @Override
 126     public boolean equals(Object p) {
 127         if (p == null || (!(p instanceof URICertStoreParameters))) {
 128             return false;
 129         }
 130 
 131         if (p == this) {
 132             return true;
 133         }
 134 
 135         URICertStoreParameters other = (URICertStoreParameters)p;
 136         return uri.equals(other.getURI());
 137     }
 138 
 139     /**
 140      * Returns a formatted string describing the parameters
 141      * including the URI used to construct this object.
 142      *
 143      * @return a formatted string describing the parameters
 144      */
 145     @Override
 146     public String toString() {
 147         return "URICertStoreParameters: " + uri.toString();
 148     }
 149 }