1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.internet;
  6. import java.net.*;
  7. import javax.mail.Session;
  8. /**
  9. * This is a utility class that generates unique values. The generated
  10. * String contains only US-ASCII characters and hence is safe for use
  11. * in RFC822 headers. <p>
  12. *
  13. * This is a package private class.
  14. *
  15. * @author John Mani
  16. * @author Max Spivak
  17. * @author Bill Shannon
  18. */
  19. class UniqueValue {
  20. /**
  21. * A global part number. Access is not synchronized because the
  22. * value is only one part of the unique value and so doesn't need
  23. * to be accurate.
  24. */
  25. private static int part = 0;
  26. /**
  27. * Get a unique value for use in a multipart boundary string.
  28. *
  29. * This implementation generates it by concatenating a global
  30. * part number, a newly created object's <code>hashCode()</code>,
  31. * and the current time (in milliseconds).
  32. */
  33. public static String getUniqueBoundaryValue() {
  34. StringBuffer s = new StringBuffer();
  35. // Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
  36. s.append("----=_Part_").append(part++).append("_").
  37. append(s.hashCode()).append('.').
  38. append(System.currentTimeMillis());
  39. return s.toString();
  40. }
  41. /**
  42. * Get a unique value for use in a Message-ID.
  43. *
  44. * This implementation generates it by concatenating a newly
  45. * created object's <code>hashCode()</code>, the current
  46. * time (in milliseconds), the string "JavaMail", and
  47. * this user's local address generated by
  48. * <code>InternetAddress.getLocalAddress()</code>.
  49. * (The address defaults to "javamailuser@localhost" if
  50. * <code>getLocalAddress()</code> returns null.)
  51. *
  52. * @param ssn Session object used to get the local address
  53. * @see javax.mail.internet.InternetAddress
  54. */
  55. public static String getUniqueMessageIDValue(Session ssn) {
  56. String suffix = null;
  57. InternetAddress addr = InternetAddress.getLocalAddress(ssn);
  58. if (addr != null)
  59. suffix = addr.getAddress();
  60. else {
  61. suffix = "javamailuser@localhost"; // worst-case default
  62. }
  63. StringBuffer s = new StringBuffer();
  64. // Unique string is <hashcode>.<currentTime>.JavaMail.<suffix>
  65. s.append(s.hashCode()).append('.').
  66. append(System.currentTimeMillis()).append('.').
  67. append("JavaMail.").
  68. append(suffix);
  69. return s.toString();
  70. }
  71. }