001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * @(#)UniqueValue.java 1.10 07/06/05
039: */
040:
041: package javax.mail.internet;
042:
043: import java.net.*;
044: import javax.mail.Session;
045:
046: /**
047: * This is a utility class that generates unique values. The generated
048: * String contains only US-ASCII characters and hence is safe for use
049: * in RFC822 headers. <p>
050: *
051: * This is a package private class.
052: *
053: * @author John Mani
054: * @author Max Spivak
055: * @author Bill Shannon
056: */
057:
058: class UniqueValue {
059: /**
060: * A global unique number, to ensure uniqueness of generated strings.
061: */
062: private static int id = 0;
063:
064: /**
065: * Get a unique value for use in a multipart boundary string.
066: *
067: * This implementation generates it by concatenating a global
068: * part number, a newly created object's <code>hashCode()</code>,
069: * and the current time (in milliseconds).
070: */
071: public static String getUniqueBoundaryValue() {
072: StringBuffer s = new StringBuffer();
073:
074: // Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
075: s.append("----=_Part_").append(getUniqueId()).append("_")
076: .append(s.hashCode()).append('.').append(
077: System.currentTimeMillis());
078: return s.toString();
079: }
080:
081: /**
082: * Get a unique value for use in a Message-ID.
083: *
084: * This implementation generates it by concatenating a newly
085: * created object's <code>hashCode()</code>, a global ID
086: * (incremented on every use), the current
087: * time (in milliseconds), the string "JavaMail", and
088: * this user's local address generated by
089: * <code>InternetAddress.getLocalAddress()</code>.
090: * (The address defaults to "javamailuser@localhost" if
091: * <code>getLocalAddress()</code> returns null.)
092: *
093: * @param ssn Session object used to get the local address
094: * @see javax.mail.internet.InternetAddress
095: */
096: public static String getUniqueMessageIDValue(Session ssn) {
097: String suffix = null;
098:
099: InternetAddress addr = InternetAddress.getLocalAddress(ssn);
100: if (addr != null)
101: suffix = addr.getAddress();
102: else {
103: suffix = "javamailuser@localhost"; // worst-case default
104: }
105:
106: StringBuffer s = new StringBuffer();
107:
108: // Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
109: s.append(s.hashCode()).append('.').append(getUniqueId())
110: .append('.').append(System.currentTimeMillis()).append(
111: '.').append("JavaMail.").append(suffix);
112: return s.toString();
113: }
114:
115: /**
116: * Ensure ID is unique by synchronizing access.
117: * XXX - Could use AtomicInteger.getAndIncrement() in J2SE 5.0.
118: */
119: private static synchronized int getUniqueId() {
120: return id++;
121: }
122: }
|