1. /*
  2. * @(#)X500Principal.java 1.12 04/05/18
  3. *
  4. * Copyright 2004 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.12, 05/18/04
  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. @Deprecated
  33. public class X500Principal implements Principal, java.io.Serializable {
  34. private static final long serialVersionUID = -8222422609431628648L;
  35. private static final java.util.ResourceBundle rb =
  36. (java.util.ResourceBundle)java.security.AccessController.doPrivileged
  37. (new java.security.PrivilegedAction() {
  38. public Object run() {
  39. return (java.util.ResourceBundle.getBundle
  40. ("sun.security.util.AuthResources"));
  41. }
  42. });
  43. /**
  44. * @serial
  45. */
  46. private String name;
  47. transient private X500Name thisX500Name;
  48. /**
  49. * Create a X500Principal with an X.500 Name,
  50. * such as "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"
  51. * (RFC 1779 style).
  52. *
  53. * <p>
  54. *
  55. * @param name the X.500 name
  56. *
  57. * @exception NullPointerException if the <code>name</code>
  58. * is <code>null</code>. <p>
  59. *
  60. * @exception IllegalArgumentException if the <code>name</code>
  61. * is improperly specified.
  62. */
  63. public X500Principal(String name) {
  64. if (name == null)
  65. throw new NullPointerException(rb.getString("provided null name"));
  66. try {
  67. thisX500Name = new X500Name(name);
  68. } catch (Exception e) {
  69. throw new IllegalArgumentException(e.toString());
  70. }
  71. this.name = name;
  72. }
  73. /**
  74. * Return the Unix username for this <code>X500Principal</code>.
  75. *
  76. * <p>
  77. *
  78. * @return the Unix username for this <code>X500Principal</code>
  79. */
  80. public String getName() {
  81. return thisX500Name.getName();
  82. }
  83. /**
  84. * Return a string representation of this <code>X500Principal</code>.
  85. *
  86. * <p>
  87. *
  88. * @return a string representation of this <code>X500Principal</code>.
  89. */
  90. public String toString() {
  91. return thisX500Name.toString();
  92. }
  93. /**
  94. * Compares the specified Object with this <code>X500Principal</code>
  95. * for equality.
  96. *
  97. * <p>
  98. *
  99. * @param o Object to be compared for equality with this
  100. * <code>X500Principal</code>.
  101. *
  102. * @return true if the specified Object is equal equal to this
  103. * <code>X500Principal</code>.
  104. */
  105. public boolean equals(Object o) {
  106. if (o == null)
  107. return false;
  108. if (this == o)
  109. return true;
  110. if (o instanceof X500Principal) {
  111. X500Principal that = (X500Principal)o;
  112. try {
  113. X500Name thatX500Name = new X500Name(that.getName());
  114. return thisX500Name.equals(thatX500Name);
  115. } catch (Exception e) {
  116. // any parsing exceptions, return false
  117. return false;
  118. }
  119. } else if (o instanceof Principal) {
  120. // this will return 'true' if 'o' is a sun.security.x509.X500Name
  121. // and the X500Names are equal
  122. return o.equals(thisX500Name);
  123. }
  124. return false;
  125. }
  126. /**
  127. * Return a hash code for this <code>X500Principal</code>.
  128. *
  129. * <p>
  130. *
  131. * @return a hash code for this <code>X500Principal</code>.
  132. */
  133. public int hashCode() {
  134. return thisX500Name.hashCode();
  135. }
  136. /**
  137. * Reads this object from a stream (i.e., deserializes it)
  138. */
  139. private void readObject(java.io.ObjectInputStream s) throws
  140. java.io.IOException,
  141. java.io.NotActiveException,
  142. ClassNotFoundException {
  143. s.defaultReadObject();
  144. // re-create thisX500Name
  145. thisX500Name = new X500Name(name);
  146. }
  147. }