001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.security.auth;
027:
028: import java.security.Principal;
029: import javax.naming.InvalidNameException;
030: import javax.naming.ldap.LdapName;
031:
032: /**
033: * A principal identified by a distinguished name as specified by
034: * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>.
035: *
036: * <p>
037: * After successful authentication, a user {@link java.security.Principal}
038: * can be associated with a particular {@link javax.security.auth.Subject}
039: * to augment that <code>Subject</code> with an additional identity.
040: * Authorization decisions can then be based upon the
041: * <code>Principal</code>s that are associated with a <code>Subject</code>.
042: *
043: * <p>
044: * This class is immutable.
045: *
046: * @since 1.6
047: */
048: public final class LdapPrincipal implements Principal,
049: java.io.Serializable {
050:
051: private static final long serialVersionUID = 6820120005580754861L;
052:
053: /**
054: * The principal's string name
055: *
056: * @serial
057: */
058: private final String nameString;
059:
060: /**
061: * The principal's name
062: *
063: * @serial
064: */
065: private final LdapName name;
066:
067: /**
068: * Creates an LDAP principal.
069: *
070: * @param name The principal's string distinguished name.
071: * @throws InvalidNameException If a syntax violation is detected.
072: * @exception NullPointerException If the <code>name</code> is
073: * <code>null</code>.
074: */
075: public LdapPrincipal(String name) throws InvalidNameException {
076: if (name == null) {
077: throw new NullPointerException("null name is illegal");
078: }
079: this .name = getLdapName(name);
080: nameString = name;
081: }
082:
083: /**
084: * Compares this principal to the specified object.
085: *
086: * @param object The object to compare this principal against.
087: * @return true if they are equal; false otherwise.
088: */
089: public boolean equals(Object object) {
090: if (this == object) {
091: return true;
092: }
093: if (object instanceof LdapPrincipal) {
094: try {
095:
096: return name.equals(getLdapName(((LdapPrincipal) object)
097: .getName()));
098:
099: } catch (InvalidNameException e) {
100: return false;
101: }
102: }
103: return false;
104: }
105:
106: /**
107: * Computes the hash code for this principal.
108: *
109: * @return The principal's hash code.
110: */
111: public int hashCode() {
112: return name.hashCode();
113: }
114:
115: /**
116: * Returns the name originally used to create this principal.
117: *
118: * @return The principal's string name.
119: */
120: public String getName() {
121: return nameString;
122: }
123:
124: /**
125: * Creates a string representation of this principal's name in the format
126: * defined by <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
127: * If the name has zero components an empty string is returned.
128: *
129: * @return The principal's string name.
130: */
131: public String toString() {
132: return name.toString();
133: }
134:
135: // Create an LdapName object from a string distinguished name.
136: private LdapName getLdapName(String name)
137: throws InvalidNameException {
138: return new LdapName(name);
139: }
140: }
|