KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)LdapPrincipal.java 1.1 06/03/27
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 import javax.naming.InvalidNameException JavaDoc;
12 import javax.naming.ldap.LdapName JavaDoc;
13
14 /**
15  * A principal identified by a distinguished name as specified by
16  * <a HREF="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>.
17  *
18  * <p>
19  * After successful authentication, a user {@link java.security.Principal}
20  * can be associated with a particular {@link javax.security.auth.Subject}
21  * to augment that <code>Subject</code> with an additional identity.
22  * Authorization decisions can then be based upon the
23  * <code>Principal</code>s that are associated with a <code>Subject</code>.
24  *
25  * <p>
26  * This class is immutable.
27  *
28  * @since 1.6
29  */

30 public final class LdapPrincipal implements Principal JavaDoc, java.io.Serializable JavaDoc {
31
32     private static final long serialVersionUID = 6820120005580754861L;
33
34     /**
35      * The principal's string name
36      *
37      * @serial
38      */

39     private final String JavaDoc nameString;
40
41     /**
42      * The principal's name
43      *
44      * @serial
45      */

46     private final LdapName JavaDoc name;
47
48     /**
49      * Creates an LDAP principal.
50      *
51      * @param name The principal's string distinguished name.
52      * @throws InvalidNameException If a syntax violation is detected.
53      * @exception NullPointerException If the <code>name</code> is
54      * <code>null</code>.
55      */

56     public LdapPrincipal(String JavaDoc name) throws InvalidNameException JavaDoc {
57         if (name == null) {
58             throw new NullPointerException JavaDoc("null name is illegal");
59         }
60     this.name = getLdapName(name);
61     nameString = name;
62     }
63
64     /**
65      * Compares this principal to the specified object.
66      *
67      * @param object The object to compare this principal against.
68      * @return true if they are equal; false otherwise.
69      */

70     public boolean equals(Object JavaDoc object) {
71     if (this == object) {
72         return true;
73     }
74     if (object instanceof LdapPrincipal) {
75         try {
76
77         return
78             name.equals(getLdapName(((LdapPrincipal)object).getName()));
79
80         } catch (InvalidNameException JavaDoc e) {
81         return false;
82         }
83     }
84     return false;
85     }
86
87     /**
88      * Computes the hash code for this principal.
89      *
90      * @return The principal's hash code.
91      */

92     public int hashCode() {
93     return name.hashCode();
94     }
95
96     /**
97      * Returns the name originally used to create this principal.
98      *
99      * @return The principal's string name.
100      */

101     public String JavaDoc getName() {
102     return nameString;
103     }
104
105     /**
106      * Creates a string representation of this principal's name in the format
107      * defined by <a HREF="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
108      * If the name has zero components an empty string is returned.
109      *
110      * @return The principal's string name.
111      */

112     public String JavaDoc toString() {
113     return name.toString();
114     }
115
116     // Create an LdapName object from a string distinguished name.
117
private LdapName JavaDoc getLdapName(String JavaDoc name) throws InvalidNameException JavaDoc {
118     return new LdapName JavaDoc(name);
119     }
120 }
121
Popular Tags