001: /*
002: * Copyright 2000-2003 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: * <p> This class implements the <code>Principal</code> interface
032: * and represents a Unix user.
033: *
034: * <p> Principals such as this <code>UnixPrincipal</code>
035: * may be associated with a particular <code>Subject</code>
036: * to augment that <code>Subject</code> with an additional
037: * identity. Refer to the <code>Subject</code> class for more information
038: * on how to achieve this. Authorization decisions can then be based upon
039: * the Principals associated with a <code>Subject</code>.
040: *
041: * @version 1.8, 01/14/00
042: * @see java.security.Principal
043: * @see javax.security.auth.Subject
044: */
045: public class UnixPrincipal implements Principal, java.io.Serializable {
046:
047: private static final long serialVersionUID = -2951667807323493631L;
048:
049: /**
050: * @serial
051: */
052: private String name;
053:
054: /**
055: * Create a UnixPrincipal with a Unix username.
056: *
057: * <p>
058: *
059: * @param name the Unix username for this user.
060: *
061: * @exception NullPointerException if the <code>name</code>
062: * is <code>null</code>.
063: */
064: public UnixPrincipal(String name) {
065: if (name == null) {
066: java.text.MessageFormat form = new java.text.MessageFormat(
067: sun.security.util.ResourcesMgr.getString(
068: "invalid null input: value",
069: "sun.security.util.AuthResources"));
070: Object[] source = { "name" };
071: throw new NullPointerException(form.format(source));
072: }
073:
074: this .name = name;
075: }
076:
077: /**
078: * Return the Unix username for this <code>UnixPrincipal</code>.
079: *
080: * <p>
081: *
082: * @return the Unix username for this <code>UnixPrincipal</code>
083: */
084: public String getName() {
085: return name;
086: }
087:
088: /**
089: * Return a string representation of this <code>UnixPrincipal</code>.
090: *
091: * <p>
092: *
093: * @return a string representation of this <code>UnixPrincipal</code>.
094: */
095: public String toString() {
096: java.text.MessageFormat form = new java.text.MessageFormat(
097: sun.security.util.ResourcesMgr.getString(
098: "UnixPrincipal: name",
099: "sun.security.util.AuthResources"));
100: Object[] source = { name };
101: return form.format(source);
102: }
103:
104: /**
105: * Compares the specified Object with this <code>UnixPrincipal</code>
106: * for equality. Returns true if the given object is also a
107: * <code>UnixPrincipal</code> and the two UnixPrincipals
108: * have the same username.
109: *
110: * <p>
111: *
112: * @param o Object to be compared for equality with this
113: * <code>UnixPrincipal</code>.
114: *
115: * @return true if the specified Object is equal equal to this
116: * <code>UnixPrincipal</code>.
117: */
118: public boolean equals(Object o) {
119: if (o == null)
120: return false;
121:
122: if (this == o)
123: return true;
124:
125: if (!(o instanceof UnixPrincipal))
126: return false;
127: UnixPrincipal that = (UnixPrincipal) o;
128:
129: if (this .getName().equals(that.getName()))
130: return true;
131: return false;
132: }
133:
134: /**
135: * Return a hash code for this <code>UnixPrincipal</code>.
136: *
137: * <p>
138: *
139: * @return a hash code for this <code>UnixPrincipal</code>.
140: */
141: public int hashCode() {
142: return name.hashCode();
143: }
144: }
|