KickJava   Java API By Example, From Geeks To Geeks.

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


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

34     private String JavaDoc name;
35
36     /**
37      * Create a UnixPrincipal with a Unix username.
38      *
39      * <p>
40      *
41      * @param name the Unix username for this user.
42      *
43      * @exception NullPointerException if the <code>name</code>
44      * is <code>null</code>.
45      */

46     public UnixPrincipal(String JavaDoc name) {
47     if (name == null) {
48         java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
49         (sun.security.util.ResourcesMgr.getString
50             ("invalid null input: value",
51             "sun.security.util.AuthResources"));
52         Object JavaDoc[] source = {"name"};
53         throw new NullPointerException JavaDoc(form.format(source));
54     }
55
56     this.name = name;
57     }
58
59     /**
60      * Return the Unix username for this <code>UnixPrincipal</code>.
61      *
62      * <p>
63      *
64      * @return the Unix username for this <code>UnixPrincipal</code>
65      */

66     public String JavaDoc getName() {
67     return name;
68     }
69
70     /**
71      * Return a string representation of this <code>UnixPrincipal</code>.
72      *
73      * <p>
74      *
75      * @return a string representation of this <code>UnixPrincipal</code>.
76      */

77     public String JavaDoc toString() {
78     java.text.MessageFormat JavaDoc form = new java.text.MessageFormat JavaDoc
79         (sun.security.util.ResourcesMgr.getString
80             ("UnixPrincipal: name",
81             "sun.security.util.AuthResources"));
82     Object JavaDoc[] source = {name};
83     return form.format(source);
84     }
85
86     /**
87      * Compares the specified Object with this <code>UnixPrincipal</code>
88      * for equality. Returns true if the given object is also a
89      * <code>UnixPrincipal</code> and the two UnixPrincipals
90      * have the same username.
91      *
92      * <p>
93      *
94      * @param o Object to be compared for equality with this
95      * <code>UnixPrincipal</code>.
96      *
97      * @return true if the specified Object is equal equal to this
98      * <code>UnixPrincipal</code>.
99      */

100     public boolean equals(Object JavaDoc o) {
101     if (o == null)
102         return false;
103
104         if (this == o)
105             return true;
106  
107         if (!(o instanceof UnixPrincipal))
108             return false;
109         UnixPrincipal that = (UnixPrincipal)o;
110
111     if (this.getName().equals(that.getName()))
112         return true;
113     return false;
114     }
115  
116     /**
117      * Return a hash code for this <code>UnixPrincipal</code>.
118      *
119      * <p>
120      *
121      * @return a hash code for this <code>UnixPrincipal</code>.
122      */

123     public int hashCode() {
124     return name.hashCode();
125     }
126 }
127
Popular Tags