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 user's Unix identification number (UID).
033: *
034: * <p> Principals such as this <code>UnixNumericUserPrincipal</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 UnixNumericUserPrincipal implements Principal,
046: java.io.Serializable {
047: private static final long serialVersionUID = -4329764253802397821L;
048:
049: /**
050: * @serial
051: */
052: private String name;
053:
054: /**
055: * Create a <code>UnixNumericUserPrincipal</code> using a
056: * <code>String</code> representation of the
057: * user's identification number (UID).
058: *
059: * <p>
060: *
061: * @param name the user identification number (UID) for this user.
062: *
063: * @exception NullPointerException if the <code>name</code>
064: * is <code>null</code>.
065: */
066: public UnixNumericUserPrincipal(String name) {
067: if (name == null) {
068: java.text.MessageFormat form = new java.text.MessageFormat(
069: sun.security.util.ResourcesMgr.getString(
070: "invalid null input: value",
071: "sun.security.util.AuthResources"));
072: Object[] source = { "name" };
073: throw new NullPointerException(form.format(source));
074: }
075:
076: this .name = name;
077: }
078:
079: /**
080: * Create a <code>UnixNumericUserPrincipal</code> using a
081: * long representation of the user's identification number (UID).
082: *
083: * <p>
084: *
085: * @param name the user identification number (UID) for this user
086: * represented as a long.
087: */
088: public UnixNumericUserPrincipal(long name) {
089: this .name = (new Long(name)).toString();
090: }
091:
092: /**
093: * Return the user identification number (UID) for this
094: * <code>UnixNumericUserPrincipal</code>.
095: *
096: * <p>
097: *
098: * @return the user identification number (UID) for this
099: * <code>UnixNumericUserPrincipal</code>
100: */
101: public String getName() {
102: return name;
103: }
104:
105: /**
106: * Return the user identification number (UID) for this
107: * <code>UnixNumericUserPrincipal</code> as a long.
108: *
109: * <p>
110: *
111: * @return the user identification number (UID) for this
112: * <code>UnixNumericUserPrincipal</code> as a long.
113: */
114: public long longValue() {
115: return ((new Long(name)).longValue());
116: }
117:
118: /**
119: * Return a string representation of this
120: * <code>UnixNumericUserPrincipal</code>.
121: *
122: * <p>
123: *
124: * @return a string representation of this
125: * <code>UnixNumericUserPrincipal</code>.
126: */
127: public String toString() {
128: java.text.MessageFormat form = new java.text.MessageFormat(
129: sun.security.util.ResourcesMgr.getString(
130: "UnixNumericUserPrincipal: name",
131: "sun.security.util.AuthResources"));
132: Object[] source = { name };
133: return form.format(source);
134: }
135:
136: /**
137: * Compares the specified Object with this
138: * <code>UnixNumericUserPrincipal</code>
139: * for equality. Returns true if the given object is also a
140: * <code>UnixNumericUserPrincipal</code> and the two
141: * UnixNumericUserPrincipals
142: * have the same user identification number (UID).
143: *
144: * <p>
145: *
146: * @param o Object to be compared for equality with this
147: * <code>UnixNumericUserPrincipal</code>.
148: *
149: * @return true if the specified Object is equal equal to this
150: * <code>UnixNumericUserPrincipal</code>.
151: */
152: public boolean equals(Object o) {
153: if (o == null)
154: return false;
155:
156: if (this == o)
157: return true;
158:
159: if (!(o instanceof UnixNumericUserPrincipal))
160: return false;
161: UnixNumericUserPrincipal that = (UnixNumericUserPrincipal) o;
162:
163: if (this .getName().equals(that.getName()))
164: return true;
165: return false;
166: }
167:
168: /**
169: * Return a hash code for this <code>UnixNumericUserPrincipal</code>.
170: *
171: * <p>
172: *
173: * @return a hash code for this <code>UnixNumericUserPrincipal</code>.
174: */
175: public int hashCode() {
176: return name.hashCode();
177: }
178: }
|