1 /*
   2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.security.auth;
  27 
  28 import java.security.Principal;
  29 
  30 /**
  31  * <p> This class implements the <code>Principal</code> interface
  32  * and represents a user's Unix identification number (UID).
  33  *
  34  * <p> Principals such as this <code>UnixNumericUserPrincipal</code>
  35  * may be associated with a particular <code>Subject</code>
  36  * to augment that <code>Subject</code> with an additional
  37  * identity.  Refer to the <code>Subject</code> class for more information
  38  * on how to achieve this.  Authorization decisions can then be based upon
  39  * the Principals associated with a <code>Subject</code>.
  40  *
  41  * @see java.security.Principal
  42  * @see javax.security.auth.Subject
  43  */
  44 public class UnixNumericUserPrincipal implements
  45                                         Principal,
  46                                         java.io.Serializable {
  47     private static final long serialVersionUID = -4329764253802397821L;
  48 
  49     /**
  50      * @serial
  51      */
  52     private String name;
  53 
  54     /**
  55      * Create a <code>UnixNumericUserPrincipal</code> using a
  56      * <code>String</code> representation of the
  57      * user's identification number (UID).
  58      *
  59      * <p>
  60      *
  61      * @param name the user identification number (UID) for this user.
  62      *
  63      * @exception NullPointerException if the <code>name</code>
  64      *                  is <code>null</code>.
  65      */
  66     public UnixNumericUserPrincipal(String name) {
  67         if (name == null) {
  68             java.text.MessageFormat form = new java.text.MessageFormat
  69                 (sun.security.util.ResourcesMgr.getString
  70                         ("invalid.null.input.value",
  71                         "sun.security.util.AuthResources"));
  72             Object[] source = {"name"};
  73             throw new NullPointerException(form.format(source));
  74         }
  75 
  76         this.name = name;
  77     }
  78 
  79     /**
  80      * Create a <code>UnixNumericUserPrincipal</code> using a
  81      * long representation of the user's identification number (UID).
  82      *
  83      * <p>
  84      *
  85      * @param name the user identification number (UID) for this user
  86      *                  represented as a long.
  87      */
  88     public UnixNumericUserPrincipal(long name) {
  89         this.name = (new Long(name)).toString();
  90     }
  91 
  92     /**
  93      * Return the user identification number (UID) for this
  94      * <code>UnixNumericUserPrincipal</code>.
  95      *
  96      * <p>
  97      *
  98      * @return the user identification number (UID) for this
  99      *          <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 }