1. /*
  2. * @(#)UnixPrincipal.java 1.7 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. /**
  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. /**
  26. * @serial
  27. */
  28. private String name;
  29. /**
  30. * Create a UnixPrincipal with a Unix username.
  31. *
  32. * <p>
  33. *
  34. * @param name the Unix username for this user.
  35. *
  36. * @exception NullPointerException if the <code>name</code>
  37. * is <code>null</code>.
  38. */
  39. public UnixPrincipal(String name) {
  40. if (name == null) {
  41. java.text.MessageFormat form = new java.text.MessageFormat
  42. (sun.security.util.ResourcesMgr.getString
  43. ("invalid null input: value",
  44. "sun.security.util.AuthResources"));
  45. Object[] source = {"name"};
  46. throw new NullPointerException(form.format(source));
  47. }
  48. this.name = name;
  49. }
  50. /**
  51. * Return the Unix username for this <code>UnixPrincipal</code>.
  52. *
  53. * <p>
  54. *
  55. * @return the Unix username for this <code>UnixPrincipal</code>
  56. */
  57. public String getName() {
  58. return name;
  59. }
  60. /**
  61. * Return a string representation of this <code>UnixPrincipal</code>.
  62. *
  63. * <p>
  64. *
  65. * @return a string representation of this <code>UnixPrincipal</code>.
  66. */
  67. public String toString() {
  68. java.text.MessageFormat form = new java.text.MessageFormat
  69. (sun.security.util.ResourcesMgr.getString
  70. ("UnixPrincipal: name",
  71. "sun.security.util.AuthResources"));
  72. Object[] source = {name};
  73. return form.format(source);
  74. }
  75. /**
  76. * Compares the specified Object with this <code>UnixPrincipal</code>
  77. * for equality. Returns true if the given object is also a
  78. * <code>UnixPrincipal</code> and the two UnixPrincipals
  79. * have the same username.
  80. *
  81. * <p>
  82. *
  83. * @param o Object to be compared for equality with this
  84. * <code>UnixPrincipal</code>.
  85. *
  86. * @return true if the specified Object is equal equal to this
  87. * <code>UnixPrincipal</code>.
  88. */
  89. public boolean equals(Object o) {
  90. if (o == null)
  91. return false;
  92. if (this == o)
  93. return true;
  94. if (!(o instanceof UnixPrincipal))
  95. return false;
  96. UnixPrincipal that = (UnixPrincipal)o;
  97. if (this.getName().equals(that.getName()))
  98. return true;
  99. return false;
  100. }
  101. /**
  102. * Return a hash code for this <code>UnixPrincipal</code>.
  103. *
  104. * <p>
  105. *
  106. * @return a hash code for this <code>UnixPrincipal</code>.
  107. */
  108. public int hashCode() {
  109. return name.hashCode();
  110. }
  111. }