KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > UniqueValue


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)UniqueValue.java 1.8 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import java.net.*;
31 import javax.mail.Session JavaDoc;
32
33 /**
34  * This is a utility class that generates unique values. The generated
35  * String contains only US-ASCII characters and hence is safe for use
36  * in RFC822 headers. <p>
37  *
38  * This is a package private class.
39  *
40  * @author John Mani
41  * @author Max Spivak
42  * @author Bill Shannon
43  */

44
45 class UniqueValue {
46     /**
47      * A global part number. Access is not synchronized because the
48      * value is only one part of the unique value and so doesn't need
49      * to be accurate.
50      */

51     private static int part = 0;
52
53     /**
54      * Likewise, a global id for Message-ID generation.
55      */

56     private static int id = 0;
57
58     /**
59      * Get a unique value for use in a multipart boundary string.
60      *
61      * This implementation generates it by concatenating a global
62      * part number, a newly created object's <code>hashCode()</code>,
63      * and the current time (in milliseconds).
64      */

65     public static String JavaDoc getUniqueBoundaryValue() {
66     StringBuffer JavaDoc s = new StringBuffer JavaDoc();
67
68     // Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
69
s.append("----=_Part_").append(part++).append("_").
70       append(s.hashCode()).append('.').
71       append(System.currentTimeMillis());
72     return s.toString();
73     }
74
75     /**
76      * Get a unique value for use in a Message-ID.
77      *
78      * This implementation generates it by concatenating a newly
79      * created object's <code>hashCode()</code>, a global ID
80      * (incremented on every use), the current
81      * time (in milliseconds), the string "JavaMail", and
82      * this user's local address generated by
83      * <code>InternetAddress.getLocalAddress()</code>.
84      * (The address defaults to "javamailuser@localhost" if
85      * <code>getLocalAddress()</code> returns null.)
86      *
87      * @param ssn Session object used to get the local address
88      * @see javax.mail.internet.InternetAddress
89      */

90     public static String JavaDoc getUniqueMessageIDValue(Session JavaDoc ssn) {
91     String JavaDoc suffix = null;
92
93     InternetAddress JavaDoc addr = InternetAddress.getLocalAddress(ssn);
94     if (addr != null)
95         suffix = addr.getAddress();
96     else {
97         suffix = "javamailuser@localhost"; // worst-case default
98
}
99
100     StringBuffer JavaDoc s = new StringBuffer JavaDoc();
101
102     // Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
103
s.append(s.hashCode()).append('.').append(id++).
104       append(System.currentTimeMillis()).append('.').
105       append("JavaMail.").
106       append(suffix);
107     return s.toString();
108     }
109 }
110
Popular Tags