001: /*
002: * Copyright 2005 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:
030: /**
031: * A user principal identified by a username or account name.
032: *
033: * <p>
034: * After successful authentication, a user {@link java.security.Principal}
035: * can be associated with a particular {@link javax.security.auth.Subject}
036: * to augment that <code>Subject</code> with an additional identity.
037: * Authorization decisions can then be based upon the
038: * <code>Principal</code>s that are associated with a <code>Subject</code>.
039: *
040: * <p>
041: * This class is immutable.
042: *
043: * @since 1.6
044: */
045: public final class UserPrincipal implements Principal,
046: java.io.Serializable {
047:
048: private static final long serialVersionUID = 892106070870210969L;
049:
050: /**
051: * The principal's name
052: *
053: * @serial
054: */
055: private final String name;
056:
057: /**
058: * Creates a principal.
059: *
060: * @param name The principal's string name.
061: * @exception NullPointerException If the <code>name</code> is
062: * <code>null</code>.
063: */
064: public UserPrincipal(String name) {
065: if (name == null) {
066: throw new NullPointerException("null name is illegal");
067: }
068: this .name = name;
069: }
070:
071: /**
072: * Compares this principal to the specified object.
073: *
074: * @param object The object to compare this principal against.
075: * @return true if they are equal; false otherwise.
076: */
077: public boolean equals(Object object) {
078: if (this == object) {
079: return true;
080: }
081: if (object instanceof UserPrincipal) {
082: return name.equals(((UserPrincipal) object).getName());
083: }
084: return false;
085: }
086:
087: /**
088: * Returns a hash code for this principal.
089: *
090: * @return The principal's hash code.
091: */
092: public int hashCode() {
093: return name.hashCode();
094: }
095:
096: /**
097: * Returns the name of this principal.
098: *
099: * @return The principal's name.
100: */
101: public String getName() {
102: return name;
103: }
104:
105: /**
106: * Returns a string representation of this principal.
107: *
108: * @return The principal's name.
109: */
110: public String toString() {
111: return name;
112: }
113: }
|