1. /*
  2. * @(#)UnixNumericGroupPrincipal.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 group identification number (GID).
  12. *
  13. * <p> Principals such as this <code>UnixNumericGroupPrincipal</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 UnixNumericGroupPrincipal implements
  25. Principal,
  26. java.io.Serializable {
  27. private static final long serialVersionUID = 3941535899328403223L;
  28. /**
  29. * @serial
  30. */
  31. private String name;
  32. /**
  33. * @serial
  34. */
  35. private boolean primaryGroup;
  36. /**
  37. * Create a <code>UnixNumericGroupPrincipal</code> using a
  38. * <code>String</code> representation of the user's
  39. * group identification number (GID).
  40. *
  41. * <p>
  42. *
  43. * @param name the user's group identification number (GID)
  44. * for this user. <p>
  45. *
  46. * @param primaryGroup true if the specified GID represents the
  47. * primary group to which this user belongs.
  48. *
  49. * @exception NullPointerException if the <code>name</code>
  50. * is <code>null</code>.
  51. */
  52. public UnixNumericGroupPrincipal(String name, boolean primaryGroup) {
  53. if (name == null) {
  54. java.text.MessageFormat form = new java.text.MessageFormat
  55. (sun.security.util.ResourcesMgr.getString
  56. ("invalid null input: value",
  57. "sun.security.util.AuthResources"));
  58. Object[] source = {"name"};
  59. throw new NullPointerException(form.format(source));
  60. }
  61. this.name = name;
  62. this.primaryGroup = primaryGroup;
  63. }
  64. /**
  65. * Create a <code>UnixNumericGroupPrincipal</code> using a
  66. * long representation of the user's group identification number (GID).
  67. *
  68. * <p>
  69. *
  70. * @param name the user's group identification number (GID) for this user
  71. * represented as a long. <p>
  72. *
  73. * @param primaryGroup true if the specified GID represents the
  74. * primary group to which this user belongs.
  75. *
  76. */
  77. public UnixNumericGroupPrincipal(long name, boolean primaryGroup) {
  78. this.name = (new Long(name)).toString();
  79. this.primaryGroup = primaryGroup;
  80. }
  81. /**
  82. * Return the user's group identification number (GID) for this
  83. * <code>UnixNumericGroupPrincipal</code>.
  84. *
  85. * <p>
  86. *
  87. * @return the user's group identification number (GID) for this
  88. * <code>UnixNumericGroupPrincipal</code>
  89. */
  90. public String getName() {
  91. return name;
  92. }
  93. /**
  94. * Return the user's group identification number (GID) for this
  95. * <code>UnixNumericGroupPrincipal</code> as a long.
  96. *
  97. * <p>
  98. *
  99. * @return the user's group identification number (GID) for this
  100. * <code>UnixNumericGroupPrincipal</code> as a long.
  101. */
  102. public long longValue() {
  103. return ((new Long(name)).longValue());
  104. }
  105. /**
  106. * Return whether this group identification number (GID) represents
  107. * the primary group to which this user belongs.
  108. *
  109. * <p>
  110. *
  111. * @return true if this group identification number (GID) represents
  112. * the primary group to which this user belongs,
  113. * or false otherwise.
  114. */
  115. public boolean isPrimaryGroup() {
  116. return primaryGroup;
  117. }
  118. /**
  119. * Return a string representation of this
  120. * <code>UnixNumericGroupPrincipal</code>.
  121. *
  122. * <p>
  123. *
  124. * @return a string representation of this
  125. * <code>UnixNumericGroupPrincipal</code>.
  126. */
  127. public String toString() {
  128. if (primaryGroup) {
  129. java.text.MessageFormat form = new java.text.MessageFormat
  130. (sun.security.util.ResourcesMgr.getString
  131. ("UnixNumericGroupPrincipal [Primary Group]: name",
  132. "sun.security.util.AuthResources"));
  133. Object[] source = {name};
  134. return form.format(source);
  135. } else {
  136. java.text.MessageFormat form = new java.text.MessageFormat
  137. (sun.security.util.ResourcesMgr.getString
  138. ("UnixNumericGroupPrincipal [Supplementary Group]: name",
  139. "sun.security.util.AuthResources"));
  140. Object[] source = {name};
  141. return form.format(source);
  142. }
  143. }
  144. /**
  145. * Compares the specified Object with this
  146. * <code>UnixNumericGroupPrincipal</code>
  147. * for equality. Returns true if the given object is also a
  148. * <code>UnixNumericGroupPrincipal</code> and the two
  149. * UnixNumericGroupPrincipals
  150. * have the same group identification number (GID).
  151. *
  152. * <p>
  153. *
  154. * @param o Object to be compared for equality with this
  155. * <code>UnixNumericGroupPrincipal</code>.
  156. *
  157. * @return true if the specified Object is equal equal to this
  158. * <code>UnixNumericGroupPrincipal</code>.
  159. */
  160. public boolean equals(Object o) {
  161. if (o == null)
  162. return false;
  163. if (this == o)
  164. return true;
  165. if (!(o instanceof UnixNumericGroupPrincipal))
  166. return false;
  167. UnixNumericGroupPrincipal that = (UnixNumericGroupPrincipal)o;
  168. if (this.getName().equals(that.getName()) &&
  169. this.isPrimaryGroup() == that.isPrimaryGroup())
  170. return true;
  171. return false;
  172. }
  173. /**
  174. * Return a hash code for this <code>UnixNumericGroupPrincipal</code>.
  175. *
  176. * <p>
  177. *
  178. * @return a hash code for this <code>UnixNumericGroupPrincipal</code>.
  179. */
  180. public int hashCode() {
  181. return toString().hashCode();
  182. }
  183. }