KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)UserPrincipal.java 1.2 05/11/17
3  *
4  * Copyright 2006 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  * A user principal identified by a username or account name.
14  *
15  * <p>
16  * After successful authentication, a user {@link java.security.Principal}
17  * can be associated with a particular {@link javax.security.auth.Subject}
18  * to augment that <code>Subject</code> with an additional identity.
19  * Authorization decisions can then be based upon the
20  * <code>Principal</code>s that are associated with a <code>Subject</code>.
21  *
22  * <p>
23  * This class is immutable.
24  *
25  * @since 1.6
26  */

27 public final class UserPrincipal implements Principal JavaDoc, java.io.Serializable JavaDoc {
28
29     private static final long serialVersionUID = 892106070870210969L;
30
31     /**
32      * The principal's name
33      *
34      * @serial
35      */

36     private final String JavaDoc name;
37
38     /**
39      * Creates a principal.
40      *
41      * @param name The principal's string name.
42      * @exception NullPointerException If the <code>name</code> is
43      * <code>null</code>.
44      */

45     public UserPrincipal(String JavaDoc name) {
46     if (name == null) {
47         throw new NullPointerException JavaDoc("null name is illegal");
48     }
49     this.name = name;
50     }
51
52     /**
53      * Compares this principal to the specified object.
54      *
55      * @param object The object to compare this principal against.
56      * @return true if they are equal; false otherwise.
57      */

58     public boolean equals(Object JavaDoc object) {
59     if (this == object) {
60         return true;
61     }
62     if (object instanceof UserPrincipal) {
63         return name.equals(((UserPrincipal)object).getName());
64     }
65     return false;
66     }
67
68     /**
69      * Returns a hash code for this principal.
70      *
71      * @return The principal's hash code.
72      */

73     public int hashCode() {
74     return name.hashCode();
75     }
76
77     /**
78      * Returns the name of this principal.
79      *
80      * @return The principal's name.
81      */

82     public String JavaDoc getName() {
83     return name;
84     }
85
86     /**
87      * Returns a string representation of this principal.
88      *
89      * @return The principal's name.
90      */

91     public String JavaDoc toString() {
92     return name;
93     }
94 }
95
Popular Tags