1. /*
  2. * @(#)X500Principal.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.security.auth;
  8. import java.security.Principal;
  9. import sun.security.x509.X500Name;
  10. /**
  11. * <p> This class represents an X.500 <code>Principal</code>.
  12. * X500Principals have names such as,
  13. * "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"
  14. * (RFC 1779 style).
  15. *
  16. * <p> Principals such as this <code>X500Principal</code>
  17. * may be associated with a particular <code>Subject</code>
  18. * to augment that <code>Subject</code> with an additional
  19. * identity. Refer to the <code>Subject</code> class for more information
  20. * on how to achieve this. Authorization decisions can then be based upon
  21. * the Principals associated with a <code>Subject</code>.
  22. *
  23. * @version 1.9, 01/23/03
  24. * @see java.security.Principal
  25. * @see javax.security.auth.Subject
  26. * @deprecated A new X500Principal class is available in the Java 2 platform.
  27. * This X500Principal classs is entirely deprecated and
  28. * is here to allow for a smooth transition to the new
  29. * class.
  30. * @see javax.security.auth.x500.X500Principal
  31. */
  32. public class X500Principal implements Principal, java.io.Serializable {
  33. private static final java.util.ResourceBundle rb =
  34. (java.util.ResourceBundle)java.security.AccessController.doPrivileged
  35. (new java.security.PrivilegedAction() {
  36. public Object run() {
  37. return (java.util.ResourceBundle.getBundle
  38. ("sun.security.util.AuthResources"));
  39. }
  40. });
  41. /**
  42. * @serial
  43. */
  44. private String name;
  45. transient private X500Name thisX500Name;
  46. /**
  47. * Create a X500Principal with an X.500 Name,
  48. * such as "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"
  49. * (RFC 1779 style).
  50. *
  51. * <p>
  52. *
  53. * @param name the X.500 name
  54. *
  55. * @exception NullPointerException if the <code>name</code>
  56. * is <code>null</code>. <p>
  57. *
  58. * @exception IllegalArgumentException if the <code>name</code>
  59. * is improperly specified.
  60. */
  61. public X500Principal(String name) {
  62. if (name == null)
  63. throw new NullPointerException(rb.getString("provided null name"));
  64. try {
  65. thisX500Name = new X500Name(name);
  66. } catch (Exception e) {
  67. throw new IllegalArgumentException(e.toString());
  68. }
  69. this.name = name;
  70. }
  71. /**
  72. * Return the Unix username for this <code>X500Principal</code>.
  73. *
  74. * <p>
  75. *
  76. * @return the Unix username for this <code>X500Principal</code>
  77. */
  78. public String getName() {
  79. return thisX500Name.getName();
  80. }
  81. /**
  82. * Return a string representation of this <code>X500Principal</code>.
  83. *
  84. * <p>
  85. *
  86. * @return a string representation of this <code>X500Principal</code>.
  87. */
  88. public String toString() {
  89. return thisX500Name.toString();
  90. }
  91. /**
  92. * Compares the specified Object with this <code>X500Principal</code>
  93. * for equality.
  94. *
  95. * <p>
  96. *
  97. * @param o Object to be compared for equality with this
  98. * <code>X500Principal</code>.
  99. *
  100. * @return true if the specified Object is equal equal to this
  101. * <code>X500Principal</code>.
  102. */
  103. public boolean equals(Object o) {
  104. if (o == null)
  105. return false;
  106. if (this == o)
  107. return true;
  108. if (o instanceof X500Principal) {
  109. X500Principal that = (X500Principal)o;
  110. try {
  111. X500Name thatX500Name = new X500Name(that.getName());
  112. return thisX500Name.equals(thatX500Name);
  113. } catch (Exception e) {
  114. // any parsing exceptions, return false
  115. return false;
  116. }
  117. } else if (o instanceof Principal) {
  118. // this will return 'true' if 'o' is a sun.security.x509.X500Name
  119. // and the X500Names are equal
  120. return o.equals(thisX500Name);
  121. }
  122. return false;
  123. }
  124. /**
  125. * Return a hash code for this <code>X500Principal</code>.
  126. *
  127. * <p>
  128. *
  129. * @return a hash code for this <code>X500Principal</code>.
  130. */
  131. public int hashCode() {
  132. return thisX500Name.hashCode();
  133. }
  134. /**
  135. * Reads this object from a stream (i.e., deserializes it)
  136. */
  137. private void readObject(java.io.ObjectInputStream s) throws
  138. java.io.IOException,
  139. java.io.NotActiveException,
  140. ClassNotFoundException {
  141. s.defaultReadObject();
  142. // re-create thisX500Name
  143. thisX500Name = new X500Name(name);
  144. }
  145. }