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