1. /*
  2. * @(#)UnixNumericUserPrincipal.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 user's Unix identification number (UID).
  12. *
  13. * <p> Principals such as this <code>UnixNumericUserPrincipal</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 UnixNumericUserPrincipal implements
  25. Principal,
  26. java.io.Serializable {
  27. private static final long serialVersionUID = -4329764253802397821L;
  28. /**
  29. * @serial
  30. */
  31. private String name;
  32. /**
  33. * Create a <code>UnixNumericUserPrincipal</code> using a
  34. * <code>String</code> representation of the
  35. * user's identification number (UID).
  36. *
  37. * <p>
  38. *
  39. * @param name the user identification number (UID) for this user.
  40. *
  41. * @exception NullPointerException if the <code>name</code>
  42. * is <code>null</code>.
  43. */
  44. public UnixNumericUserPrincipal(String name) {
  45. if (name == null) {
  46. java.text.MessageFormat form = new java.text.MessageFormat
  47. (sun.security.util.ResourcesMgr.getString
  48. ("invalid null input: value",
  49. "sun.security.util.AuthResources"));
  50. Object[] source = {"name"};
  51. throw new NullPointerException(form.format(source));
  52. }
  53. this.name = name;
  54. }
  55. /**
  56. * Create a <code>UnixNumericUserPrincipal</code> using a
  57. * long representation of the user's identification number (UID).
  58. *
  59. * <p>
  60. *
  61. * @param name the user identification number (UID) for this user
  62. * represented as a long.
  63. */
  64. public UnixNumericUserPrincipal(long name) {
  65. this.name = (new Long(name)).toString();
  66. }
  67. /**
  68. * Return the user identification number (UID) for this
  69. * <code>UnixNumericUserPrincipal</code>.
  70. *
  71. * <p>
  72. *
  73. * @return the user identification number (UID) for this
  74. * <code>UnixNumericUserPrincipal</code>
  75. */
  76. public String getName() {
  77. return name;
  78. }
  79. /**
  80. * Return the user identification number (UID) for this
  81. * <code>UnixNumericUserPrincipal</code> as a long.
  82. *
  83. * <p>
  84. *
  85. * @return the user identification number (UID) for this
  86. * <code>UnixNumericUserPrincipal</code> as a long.
  87. */
  88. public long longValue() {
  89. return ((new Long(name)).longValue());
  90. }
  91. /**
  92. * Return a string representation of this
  93. * <code>UnixNumericUserPrincipal</code>.
  94. *
  95. * <p>
  96. *
  97. * @return a string representation of this
  98. * <code>UnixNumericUserPrincipal</code>.
  99. */
  100. public String toString() {
  101. java.text.MessageFormat form = new java.text.MessageFormat
  102. (sun.security.util.ResourcesMgr.getString
  103. ("UnixNumericUserPrincipal: name",
  104. "sun.security.util.AuthResources"));
  105. Object[] source = {name};
  106. return form.format(source);
  107. }
  108. /**
  109. * Compares the specified Object with this
  110. * <code>UnixNumericUserPrincipal</code>
  111. * for equality. Returns true if the given object is also a
  112. * <code>UnixNumericUserPrincipal</code> and the two
  113. * UnixNumericUserPrincipals
  114. * have the same user identification number (UID).
  115. *
  116. * <p>
  117. *
  118. * @param o Object to be compared for equality with this
  119. * <code>UnixNumericUserPrincipal</code>.
  120. *
  121. * @return true if the specified Object is equal equal to this
  122. * <code>UnixNumericUserPrincipal</code>.
  123. */
  124. public boolean equals(Object o) {
  125. if (o == null)
  126. return false;
  127. if (this == o)
  128. return true;
  129. if (!(o instanceof UnixNumericUserPrincipal))
  130. return false;
  131. UnixNumericUserPrincipal that = (UnixNumericUserPrincipal)o;
  132. if (this.getName().equals(that.getName()))
  133. return true;
  134. return false;
  135. }
  136. /**
  137. * Return a hash code for this <code>UnixNumericUserPrincipal</code>.
  138. *
  139. * <p>
  140. *
  141. * @return a hash code for this <code>UnixNumericUserPrincipal</code>.
  142. */
  143. public int hashCode() {
  144. return name.hashCode();
  145. }
  146. }