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