1. /*
  2. * @(#)UnixPrincipal.java 1.9 03/12/19
  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. /**
  10. * <p> This class implements the <code>Principal</code> interface
  11. * and represents a Unix user.
  12. *
  13. * <p> Principals such as this <code>UnixPrincipal</code>
  14. * may be associated with a particular <code>Subject</code>
  15. * to augment that <code>Subject</code> with an additional
  16. * identity. Refer to the <code>Subject</code> class for more information
  17. * on how to achieve this. Authorization decisions can then be based upon
  18. * the Principals associated with a <code>Subject</code>.
  19. *
  20. * @version 1.8, 01/14/00
  21. * @see java.security.Principal
  22. * @see javax.security.auth.Subject
  23. */
  24. public class UnixPrincipal implements Principal, java.io.Serializable {
  25. private static final long serialVersionUID = -2951667807323493631L;
  26. /**
  27. * @serial
  28. */
  29. private String name;
  30. /**
  31. * Create a UnixPrincipal with a Unix username.
  32. *
  33. * <p>
  34. *
  35. * @param name the Unix username for this user.
  36. *
  37. * @exception NullPointerException if the <code>name</code>
  38. * is <code>null</code>.
  39. */
  40. public UnixPrincipal(String name) {
  41. if (name == null) {
  42. java.text.MessageFormat form = new java.text.MessageFormat
  43. (sun.security.util.ResourcesMgr.getString
  44. ("invalid null input: value",
  45. "sun.security.util.AuthResources"));
  46. Object[] source = {"name"};
  47. throw new NullPointerException(form.format(source));
  48. }
  49. this.name = name;
  50. }
  51. /**
  52. * Return the Unix username for this <code>UnixPrincipal</code>.
  53. *
  54. * <p>
  55. *
  56. * @return the Unix username for this <code>UnixPrincipal</code>
  57. */
  58. public String getName() {
  59. return name;
  60. }
  61. /**
  62. * Return a string representation of this <code>UnixPrincipal</code>.
  63. *
  64. * <p>
  65. *
  66. * @return a string representation of this <code>UnixPrincipal</code>.
  67. */
  68. public String toString() {
  69. java.text.MessageFormat form = new java.text.MessageFormat
  70. (sun.security.util.ResourcesMgr.getString
  71. ("UnixPrincipal: name",
  72. "sun.security.util.AuthResources"));
  73. Object[] source = {name};
  74. return form.format(source);
  75. }
  76. /**
  77. * Compares the specified Object with this <code>UnixPrincipal</code>
  78. * for equality. Returns true if the given object is also a
  79. * <code>UnixPrincipal</code> and the two UnixPrincipals
  80. * have the same username.
  81. *
  82. * <p>
  83. *
  84. * @param o Object to be compared for equality with this
  85. * <code>UnixPrincipal</code>.
  86. *
  87. * @return true if the specified Object is equal equal to this
  88. * <code>UnixPrincipal</code>.
  89. */
  90. public boolean equals(Object o) {
  91. if (o == null)
  92. return false;
  93. if (this == o)
  94. return true;
  95. if (!(o instanceof UnixPrincipal))
  96. return false;
  97. UnixPrincipal that = (UnixPrincipal)o;
  98. if (this.getName().equals(that.getName()))
  99. return true;
  100. return false;
  101. }
  102. /**
  103. * Return a hash code for this <code>UnixPrincipal</code>.
  104. *
  105. * <p>
  106. *
  107. * @return a hash code for this <code>UnixPrincipal</code>.
  108. */
  109. public int hashCode() {
  110. return name.hashCode();
  111. }
  112. }