Home | History | Annotate | Download | only in cms
      1 package org.bouncycastle.cms;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Collection;
      5 import java.util.HashMap;
      6 import java.util.Iterator;
      7 import java.util.List;
      8 import java.util.Map;
      9 
     10 import org.bouncycastle.util.Iterable;
     11 
     12 public class SignerInformationStore
     13     implements Iterable<SignerInformation>
     14 {
     15     private List all = new ArrayList();
     16     private Map table = new HashMap();
     17 
     18     /**
     19      * Create a store containing a single SignerInformation object.
     20      *
     21      * @param signerInfo the signer information to contain.
     22      */
     23     public SignerInformationStore(
     24         SignerInformation  signerInfo)
     25     {
     26         this.all = new ArrayList(1);
     27         this.all.add(signerInfo);
     28 
     29         SignerId sid = signerInfo.getSID();
     30 
     31         table.put(sid, all);
     32     }
     33 
     34     /**
     35      * Create a store containing a collection of SignerInformation objects.
     36      *
     37      * @param signerInfos a collection signer information objects to contain.
     38      */
     39     public SignerInformationStore(
     40         Collection<SignerInformation>  signerInfos)
     41     {
     42         Iterator    it = signerInfos.iterator();
     43 
     44         while (it.hasNext())
     45         {
     46             SignerInformation   signer = (SignerInformation)it.next();
     47             SignerId            sid = signer.getSID();
     48 
     49             List list = (ArrayList)table.get(sid);
     50             if (list == null)
     51             {
     52                 list = new ArrayList(1);
     53                 table.put(sid, list);
     54             }
     55 
     56             list.add(signer);
     57         }
     58 
     59         this.all = new ArrayList(signerInfos);
     60     }
     61 
     62     /**
     63      * Return the first SignerInformation object that matches the
     64      * passed in selector. Null if there are no matches.
     65      *
     66      * @param selector to identify a signer
     67      * @return a single SignerInformation object. Null if none matches.
     68      */
     69     public SignerInformation get(
     70         SignerId        selector)
     71     {
     72         Collection list = getSigners(selector);
     73 
     74         return list.size() == 0 ? null : (SignerInformation) list.iterator().next();
     75     }
     76 
     77     /**
     78      * Return the number of signers in the collection.
     79      *
     80      * @return number of signers identified.
     81      */
     82     public int size()
     83     {
     84         return all.size();
     85     }
     86 
     87     /**
     88      * Return all signers in the collection
     89      *
     90      * @return a collection of signers.
     91      */
     92     public Collection<SignerInformation> getSigners()
     93     {
     94         return new ArrayList(all);
     95     }
     96 
     97     /**
     98      * Return possible empty collection with signers matching the passed in SignerId
     99      *
    100      * @param selector a signer id to select against.
    101      * @return a collection of SignerInformation objects.
    102      */
    103     public Collection<SignerInformation> getSigners(
    104         SignerId selector)
    105     {
    106         if (selector.getIssuer() != null && selector.getSubjectKeyIdentifier() != null)
    107         {
    108             List results = new ArrayList();
    109 
    110             Collection match1 = getSigners(new SignerId(selector.getIssuer(), selector.getSerialNumber()));
    111 
    112             if (match1 != null)
    113             {
    114                 results.addAll(match1);
    115             }
    116 
    117             Collection match2 = getSigners(new SignerId(selector.getSubjectKeyIdentifier()));
    118 
    119             if (match2 != null)
    120             {
    121                 results.addAll(match2);
    122             }
    123 
    124             return results;
    125         }
    126         else
    127         {
    128             List list = (ArrayList)table.get(selector);
    129 
    130             return list == null ? new ArrayList() : new ArrayList(list);
    131         }
    132     }
    133 
    134     /**
    135      * Support method for Iterable where available.
    136      */
    137     public Iterator<SignerInformation> iterator()
    138     {
    139         return getSigners().iterator();
    140     }
    141 }
    142