KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > security > auth > UnixNumericGroupPrincipal


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
8 package com.sun.security.auth;
9
10 import java.security.Principal JavaDoc;
11
12 /**
13  * <p> This class implements the <code>Principal</code> interface
14  * and represents a user's Unix group identification number (GID).
15  *
16  * <p> Principals such as this <code>UnixNumericGroupPrincipal</code>
17  * may be associated with a particular <code>Subject</code>
18  * to augment that <code>Subject</code> with an additional
19  * identity. Refer to the <code>Subject</code> class for more information
20  * on how to achieve this. Authorization decisions can then be based upon
21  * the Principals associated with a <code>Subject</code>.
22  *
23  * @version 1.8, 01/14/00
24  * @see java.security.Principal
25  * @see javax.security.auth.Subject
26  */

27 public class UnixNumericGroupPrincipal implements
28                     Principal JavaDoc,
29                     java.io.Serializable JavaDoc {
30
31     private static final long serialVersionUID = 3941535899328403223L;
32
33     /**
34      * @serial
35      */

36     private String JavaDoc name;
37
38     /**
39      * @serial
40      */

41     private boolean primaryGroup;
42
43     /**
44      * Create a <code>UnixNumericGroupPrincipal</code> using a
45      * <code>String</code> representation of the user's
46      * group identification number (GID).
47      *
48      * <p>
49      *
50      * @param name the user's group identification number (GID)
51      * for this user. <p>
52      *
53      * @param primaryGroup true if the specified GID represents the
54      * primary group to which this user belongs.
55      *
56      * @exception NullPointerException if the <code>name</code>
57      * is <code>null</code>.
58      */

59     public UnixNumericGroupPrincipal(String JavaDoc name, boolean primaryGroup) {
60     if (name == null) {
61         java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
62         (sun.security.util.ResourcesMgr.getString
63             ("invalid null input: value",
64             "sun.security.util.AuthResources"));
65         Object JavaDoc[] source = {"name"};
66         throw new NullPointerException JavaDoc(form.format(source));
67     }
68
69     this.name = name;
70     this.primaryGroup = primaryGroup;
71     }
72
73     /**
74      * Create a <code>UnixNumericGroupPrincipal</code> using a
75      * long representation of the user's group identification number (GID).
76      *
77      * <p>
78      *
79      * @param name the user's group identification number (GID) for this user
80      * represented as a long. <p>
81      *
82      * @param primaryGroup true if the specified GID represents the
83      * primary group to which this user belongs.
84      *
85      */

86     public UnixNumericGroupPrincipal(long name, boolean primaryGroup) {
87     this.name = (new Long JavaDoc(name)).toString();
88     this.primaryGroup = primaryGroup;
89     }
90
91     /**
92      * Return the user's group identification number (GID) for this
93      * <code>UnixNumericGroupPrincipal</code>.
94      *
95      * <p>
96      *
97      * @return the user's group identification number (GID) for this
98      * <code>UnixNumericGroupPrincipal</code>
99      */

100     public String JavaDoc getName() {
101     return name;
102     }
103
104     /**
105      * Return the user's group identification number (GID) for this
106      * <code>UnixNumericGroupPrincipal</code> as a long.
107      *
108      * <p>
109      *
110      * @return the user's group identification number (GID) for this
111      * <code>UnixNumericGroupPrincipal</code> as a long.
112      */

113     public long longValue() {
114     return ((new Long JavaDoc(name)).longValue());
115     }
116
117     /**
118      * Return whether this group identification number (GID) represents
119      * the primary group to which this user belongs.
120      *
121      * <p>
122      *
123      * @return true if this group identification number (GID) represents
124      * the primary group to which this user belongs,
125      * or false otherwise.
126      */

127     public boolean isPrimaryGroup() {
128     return primaryGroup;
129     }
130
131     /**
132      * Return a string representation of this
133      * <code>UnixNumericGroupPrincipal</code>.
134      *
135      * <p>
136      *
137      * @return a string representation of this
138      * <code>UnixNumericGroupPrincipal</code>.
139      */

140     public String JavaDoc toString() {
141
142     if (primaryGroup) {
143         java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
144         (sun.security.util.ResourcesMgr.getString
145             ("UnixNumericGroupPrincipal [Primary Group]: name",
146             "sun.security.util.AuthResources"));
147         Object JavaDoc[] source = {name};
148         return form.format(source);
149     } else {
150         java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
151         (sun.security.util.ResourcesMgr.getString
152             ("UnixNumericGroupPrincipal [Supplementary Group]: name",
153             "sun.security.util.AuthResources"));
154         Object JavaDoc[] source = {name};
155         return form.format(source);
156     }
157     }
158
159     /**
160      * Compares the specified Object with this
161      * <code>UnixNumericGroupPrincipal</code>
162      * for equality. Returns true if the given object is also a
163      * <code>UnixNumericGroupPrincipal</code> and the two
164      * UnixNumericGroupPrincipals
165      * have the same group identification number (GID).
166      *
167      * <p>
168      *
169      * @param o Object to be compared for equality with this
170      * <code>UnixNumericGroupPrincipal</code>.
171      *
172      * @return true if the specified Object is equal equal to this
173      * <code>UnixNumericGroupPrincipal</code>.
174      */

175     public boolean equals(Object JavaDoc o) {
176     if (o == null)
177         return false;
178
179         if (this == o)
180             return true;
181  
182         if (!(o instanceof UnixNumericGroupPrincipal))
183             return false;
184         UnixNumericGroupPrincipal that = (UnixNumericGroupPrincipal)o;
185
186     if (this.getName().equals(that.getName()) &&
187         this.isPrimaryGroup() == that.isPrimaryGroup())
188         return true;
189     return false;
190     }
191  
192     /**
193      * Return a hash code for this <code>UnixNumericGroupPrincipal</code>.
194      *
195      * <p>
196      *
197      * @return a hash code for this <code>UnixNumericGroupPrincipal</code>.
198      */

199     public int hashCode() {
200     return toString().hashCode();
201     }
202 }
203
Popular Tags