KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)UnixNumericUserPrincipal.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 identification number (UID).
15  *
16  * <p> Principals such as this <code>UnixNumericUserPrincipal</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 UnixNumericUserPrincipal implements
28                     Principal JavaDoc,
29                     java.io.Serializable JavaDoc {
30     private static final long serialVersionUID = -4329764253802397821L;
31
32     /**
33      * @serial
34      */

35     private String JavaDoc name;
36
37     /**
38      * Create a <code>UnixNumericUserPrincipal</code> using a
39      * <code>String</code> representation of the
40      * user's identification number (UID).
41      *
42      * <p>
43      *
44      * @param name the user identification number (UID) for this user.
45      *
46      * @exception NullPointerException if the <code>name</code>
47      * is <code>null</code>.
48      */

49     public UnixNumericUserPrincipal(String JavaDoc name) {
50     if (name == null) {
51         java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
52         (sun.security.util.ResourcesMgr.getString
53             ("invalid null input: value",
54             "sun.security.util.AuthResources"));
55         Object JavaDoc[] source = {"name"};
56         throw new NullPointerException JavaDoc(form.format(source));
57     }
58
59     this.name = name;
60     }
61
62     /**
63      * Create a <code>UnixNumericUserPrincipal</code> using a
64      * long representation of the user's identification number (UID).
65      *
66      * <p>
67      *
68      * @param name the user identification number (UID) for this user
69      * represented as a long.
70      */

71     public UnixNumericUserPrincipal(long name) {
72     this.name = (new Long JavaDoc(name)).toString();
73     }
74
75     /**
76      * Return the user identification number (UID) for this
77      * <code>UnixNumericUserPrincipal</code>.
78      *
79      * <p>
80      *
81      * @return the user identification number (UID) for this
82      * <code>UnixNumericUserPrincipal</code>
83      */

84     public String JavaDoc getName() {
85     return name;
86     }
87
88     /**
89      * Return the user identification number (UID) for this
90      * <code>UnixNumericUserPrincipal</code> as a long.
91      *
92      * <p>
93      *
94      * @return the user identification number (UID) for this
95      * <code>UnixNumericUserPrincipal</code> as a long.
96      */

97     public long longValue() {
98     return ((new Long JavaDoc(name)).longValue());
99     }
100
101     /**
102      * Return a string representation of this
103      * <code>UnixNumericUserPrincipal</code>.
104      *
105      * <p>
106      *
107      * @return a string representation of this
108      * <code>UnixNumericUserPrincipal</code>.
109      */

110     public String JavaDoc toString() {
111     java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
112         (sun.security.util.ResourcesMgr.getString
113             ("UnixNumericUserPrincipal: name",
114             "sun.security.util.AuthResources"));
115     Object JavaDoc[] source = {name};
116     return form.format(source);
117     }
118
119     /**
120      * Compares the specified Object with this
121      * <code>UnixNumericUserPrincipal</code>
122      * for equality. Returns true if the given object is also a
123      * <code>UnixNumericUserPrincipal</code> and the two
124      * UnixNumericUserPrincipals
125      * have the same user identification number (UID).
126      *
127      * <p>
128      *
129      * @param o Object to be compared for equality with this
130      * <code>UnixNumericUserPrincipal</code>.
131      *
132      * @return true if the specified Object is equal equal to this
133      * <code>UnixNumericUserPrincipal</code>.
134      */

135     public boolean equals(Object JavaDoc o) {
136     if (o == null)
137         return false;
138
139         if (this == o)
140             return true;
141  
142         if (!(o instanceof UnixNumericUserPrincipal))
143             return false;
144         UnixNumericUserPrincipal that = (UnixNumericUserPrincipal)o;
145
146     if (this.getName().equals(that.getName()))
147         return true;
148     return false;
149     }
150  
151     /**
152      * Return a hash code for this <code>UnixNumericUserPrincipal</code>.
153      *
154      * <p>
155      *
156      * @return a hash code for this <code>UnixNumericUserPrincipal</code>.
157      */

158     public int hashCode() {
159     return name.hashCode();
160     }
161 }
162
Popular Tags