KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > CodeSigner


1 /*
2  * @(#)CodeSigner.java 1.4 04/04/26
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package java.security;
9
10 import java.io.Serializable JavaDoc;
11 import java.security.cert.CertPath JavaDoc;
12
13 /**
14  * This class encapsulates information about a code signer.
15  * It is immutable.
16  *
17  * @since 1.5
18  * @version 1.4, 04/26/04
19  * @author Vincent Ryan
20  */

21
22 public final class CodeSigner implements Serializable JavaDoc {
23
24     private static final long serialVersionUID = 6819288105193937581L;
25
26     /**
27      * The signer's certificate path.
28      *
29      * @serial
30      */

31     private CertPath JavaDoc signerCertPath;
32
33     /*
34      * The signature timestamp.
35      *
36      * @serial
37      */

38     private Timestamp JavaDoc timestamp;
39
40     /*
41      * Hash code for this code signer.
42      */

43     private transient int myhash = -1;
44
45     /**
46      * Constructs a CodeSigner object.
47      *
48      * @param signerCertPath The signer's certificate path.
49      * It must not be <code>null</code>.
50      * @param timestamp A signature timestamp.
51      * If <code>null</code> then no timestamp was generated
52      * for the signature.
53      * @throws NullPointerException if <code>signerCertPath</code> is
54      * <code>null</code>.
55      */

56     public CodeSigner(CertPath JavaDoc signerCertPath, Timestamp JavaDoc timestamp) {
57     if (signerCertPath == null) {
58         throw new NullPointerException JavaDoc();
59     }
60     this.signerCertPath = signerCertPath;
61     this.timestamp = timestamp;
62     }
63
64     /**
65      * Returns the signer's certificate path.
66      *
67      * @return A certificate path.
68      */

69     public CertPath JavaDoc getSignerCertPath() {
70     return signerCertPath;
71     }
72
73     /**
74      * Returns the signature timestamp.
75      *
76      * @return The timestamp or <code>null</code> if none is present.
77      */

78     public Timestamp JavaDoc getTimestamp() {
79     return timestamp;
80     }
81
82     /**
83      * Returns the hash code value for this code signer.
84      * The hash code is generated using the signer's certificate path and the
85      * timestamp, if present.
86      *
87      * @return a hash code value for this code signer.
88      */

89     public int hashCode() {
90         if (myhash == -1) {
91         if (timestamp == null) {
92         myhash = signerCertPath.hashCode();
93         } else {
94         myhash = signerCertPath.hashCode() + timestamp.hashCode();
95         }
96         }
97         return myhash;
98     }
99
100     /**
101      * Tests for equality between the specified object and this
102      * code signer. Two code signers are considered equal if their
103      * signer certificate paths are equal and if their timestamps are equal,
104      * if present in both.
105      *
106      * @param obj the object to test for equality with this object.
107      *
108      * @return true if the objects are considered equal, false otherwise.
109      */

110     public boolean equals(Object JavaDoc obj) {
111         if (obj == null || (!(obj instanceof CodeSigner JavaDoc))) {
112             return false;
113         }
114         CodeSigner JavaDoc that = (CodeSigner JavaDoc)obj;
115
116         if (this == that) {
117             return true;
118         }
119     Timestamp JavaDoc thatTimestamp = that.getTimestamp();
120     if (timestamp == null) {
121         if (thatTimestamp != null) {
122         return false;
123         }
124     } else {
125         if (thatTimestamp == null ||
126             (! timestamp.equals(thatTimestamp))) {
127         return false;
128         }
129     }
130         return signerCertPath.equals(that.getSignerCertPath());
131     }
132
133     /**
134      * Returns a string describing this code signer.
135      *
136      * @return A string comprising the signer's certificate and a timestamp,
137      * if present.
138      */

139     public String JavaDoc toString() {
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         sb.append("(");
142         sb.append("Signer: " + signerCertPath.getCertificates().get(0));
143     if (timestamp != null) {
144             sb.append("timestamp: " + timestamp);
145     }
146         sb.append(")");
147         return sb.toString();
148     }
149 }
150
Popular Tags