blob: e9b21184402890f0862addf33c64f17e9f211383 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.security.auth;
27
28import java.security.Principal;
29
30/**
31 * <p> This class implements the <code>Principal</code> interface
32 * and represents a user's Unix group identification number (GID).
33 *
34 * <p> Principals such as this <code>UnixNumericGroupPrincipal</code>
35 * may be associated with a particular <code>Subject</code>
36 * to augment that <code>Subject</code> with an additional
37 * identity. Refer to the <code>Subject</code> class for more information
38 * on how to achieve this. Authorization decisions can then be based upon
39 * the Principals associated with a <code>Subject</code>.
40 *
41 * @see java.security.Principal
42 * @see javax.security.auth.Subject
43 */
44public class UnixNumericGroupPrincipal implements
45 Principal,
46 java.io.Serializable {
47
48 private static final long serialVersionUID = 3941535899328403223L;
49
50 /**
51 * @serial
52 */
53 private String name;
54
55 /**
56 * @serial
57 */
58 private boolean primaryGroup;
59
60 /**
61 * Create a <code>UnixNumericGroupPrincipal</code> using a
62 * <code>String</code> representation of the user's
63 * group identification number (GID).
64 *
65 * <p>
66 *
67 * @param name the user's group identification number (GID)
68 * for this user. <p>
69 *
70 * @param primaryGroup true if the specified GID represents the
71 * primary group to which this user belongs.
72 *
73 * @exception NullPointerException if the <code>name</code>
74 * is <code>null</code>.
75 */
76 public UnixNumericGroupPrincipal(String name, boolean primaryGroup) {
77 if (name == null) {
78 java.text.MessageFormat form = new java.text.MessageFormat
79 (sun.security.util.ResourcesMgr.getString
80 ("invalid null input: value",
81 "sun.security.util.AuthResources"));
82 Object[] source = {"name"};
83 throw new NullPointerException(form.format(source));
84 }
85
86 this.name = name;
87 this.primaryGroup = primaryGroup;
88 }
89
90 /**
91 * Create a <code>UnixNumericGroupPrincipal</code> using a
92 * long representation of the user's group identification number (GID).
93 *
94 * <p>
95 *
96 * @param name the user's group identification number (GID) for this user
97 * represented as a long. <p>
98 *
99 * @param primaryGroup true if the specified GID represents the
100 * primary group to which this user belongs.
101 *
102 */
103 public UnixNumericGroupPrincipal(long name, boolean primaryGroup) {
104 this.name = (new Long(name)).toString();
105 this.primaryGroup = primaryGroup;
106 }
107
108 /**
109 * Return the user's group identification number (GID) for this
110 * <code>UnixNumericGroupPrincipal</code>.
111 *
112 * <p>
113 *
114 * @return the user's group identification number (GID) for this
115 * <code>UnixNumericGroupPrincipal</code>
116 */
117 public String getName() {
118 return name;
119 }
120
121 /**
122 * Return the user's group identification number (GID) for this
123 * <code>UnixNumericGroupPrincipal</code> as a long.
124 *
125 * <p>
126 *
127 * @return the user's group identification number (GID) for this
128 * <code>UnixNumericGroupPrincipal</code> as a long.
129 */
130 public long longValue() {
131 return ((new Long(name)).longValue());
132 }
133
134 /**
135 * Return whether this group identification number (GID) represents
136 * the primary group to which this user belongs.
137 *
138 * <p>
139 *
140 * @return true if this group identification number (GID) represents
141 * the primary group to which this user belongs,
142 * or false otherwise.
143 */
144 public boolean isPrimaryGroup() {
145 return primaryGroup;
146 }
147
148 /**
149 * Return a string representation of this
150 * <code>UnixNumericGroupPrincipal</code>.
151 *
152 * <p>
153 *
154 * @return a string representation of this
155 * <code>UnixNumericGroupPrincipal</code>.
156 */
157 public String toString() {
158
159 if (primaryGroup) {
160 java.text.MessageFormat form = new java.text.MessageFormat
161 (sun.security.util.ResourcesMgr.getString
162 ("UnixNumericGroupPrincipal [Primary Group]: name",
163 "sun.security.util.AuthResources"));
164 Object[] source = {name};
165 return form.format(source);
166 } else {
167 java.text.MessageFormat form = new java.text.MessageFormat
168 (sun.security.util.ResourcesMgr.getString
169 ("UnixNumericGroupPrincipal [Supplementary Group]: name",
170 "sun.security.util.AuthResources"));
171 Object[] source = {name};
172 return form.format(source);
173 }
174 }
175
176 /**
177 * Compares the specified Object with this
178 * <code>UnixNumericGroupPrincipal</code>
179 * for equality. Returns true if the given object is also a
180 * <code>UnixNumericGroupPrincipal</code> and the two
181 * UnixNumericGroupPrincipals
182 * have the same group identification number (GID).
183 *
184 * <p>
185 *
186 * @param o Object to be compared for equality with this
187 * <code>UnixNumericGroupPrincipal</code>.
188 *
189 * @return true if the specified Object is equal equal to this
190 * <code>UnixNumericGroupPrincipal</code>.
191 */
192 public boolean equals(Object o) {
193 if (o == null)
194 return false;
195
196 if (this == o)
197 return true;
198
199 if (!(o instanceof UnixNumericGroupPrincipal))
200 return false;
201 UnixNumericGroupPrincipal that = (UnixNumericGroupPrincipal)o;
202
203 if (this.getName().equals(that.getName()) &&
204 this.isPrimaryGroup() == that.isPrimaryGroup())
205 return true;
206 return false;
207 }
208
209 /**
210 * Return a hash code for this <code>UnixNumericGroupPrincipal</code>.
211 *
212 * <p>
213 *
214 * @return a hash code for this <code>UnixNumericGroupPrincipal</code>.
215 */
216 public int hashCode() {
217 return toString().hashCode();
218 }
219}