1 /*
   2  * Copyright (c) 2000, 2013, 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 @jdk.Exported
  45 public class UnixNumericUserPrincipal implements
  46                                         Principal,
  47                                         java.io.Serializable {
  48     private static final long serialVersionUID = -4329764253802397821L;
  49 
  50     /**
  51      * @serial
  52      */
  53     private String name;
  54 
  55     /**
  56      * Create a <code>UnixNumericUserPrincipal</code> using a
  57      * <code>String</code> representation of the
  58      * user's identification number (UID).
  59      *
  60      * <p>
  61      *
  62      * @param name the user identification number (UID) for this user.
  63      *
  64      * @exception NullPointerException if the <code>name</code>
  65      *                  is <code>null</code>.
  66      */
  67     public UnixNumericUserPrincipal(String name) {
  68         if (name == null) {
  69             java.text.MessageFormat form = new java.text.MessageFormat
  70                 (sun.security.util.ResourcesMgr.getString
  71                         ("invalid.null.input.value",
  72                         "sun.security.util.AuthResources"));
  73             Object[] source = {"name"};
  74             throw new NullPointerException(form.format(source));
  75         }
  76 
  77         this.name = name;
  78     }
  79 
  80     /**
  81      * Create a <code>UnixNumericUserPrincipal</code> using a
  82      * long representation of the user's identification number (UID).
  83      *
  84      * <p>
  85      *
  86      * @param name the user identification number (UID) for this user
  87      *                  represented as a long.
  88      */
  89     public UnixNumericUserPrincipal(long name) {
  90         this.name = (new Long(name)).toString();
  91     }
  92 
  93     /**
  94      * Return the user identification number (UID) for this
  95      * <code>UnixNumericUserPrincipal</code>.
  96      *
  97      * <p>
  98      *
  99      * @return the user identification number (UID) for this
 100      *          <code>UnixNumericUserPrincipal</code>
 101      */
 102     public String getName() {
 103         return name;
 104     }
 105 
 106     /**
 107      * Return the user identification number (UID) for this
 108      * <code>UnixNumericUserPrincipal</code> as a long.
 109      *
 110      * <p>
 111      *
 112      * @return the user identification number (UID) for this
 113      *          <code>UnixNumericUserPrincipal</code> as a long.
 114      */
 115     public long longValue() {
 116         return ((new Long(name)).longValue());
 117     }
 118 
 119     /**
 120      * Return a string representation of this
 121      * <code>UnixNumericUserPrincipal</code>.
 122      *
 123      * <p>
 124      *
 125      * @return a string representation of this
 126      *          <code>UnixNumericUserPrincipal</code>.
 127      */
 128     public String toString() {
 129         java.text.MessageFormat form = new java.text.MessageFormat
 130                 (sun.security.util.ResourcesMgr.getString
 131                         ("UnixNumericUserPrincipal.name",
 132                         "sun.security.util.AuthResources"));
 133         Object[] source = {name};
 134         return form.format(source);
 135     }
 136 
 137     /**
 138      * Compares the specified Object with this
 139      * <code>UnixNumericUserPrincipal</code>
 140      * for equality.  Returns true if the given object is also a
 141      * <code>UnixNumericUserPrincipal</code> and the two
 142      * UnixNumericUserPrincipals
 143      * have the same user identification number (UID).
 144      *
 145      * <p>
 146      *
 147      * @param o Object to be compared for equality with this
 148      *          <code>UnixNumericUserPrincipal</code>.
 149      *
 150      * @return true if the specified Object is equal equal to this
 151      *          <code>UnixNumericUserPrincipal</code>.
 152      */
 153     public boolean equals(Object o) {
 154         if (o == null)
 155             return false;
 156 
 157         if (this == o)
 158             return true;
 159 
 160         if (!(o instanceof UnixNumericUserPrincipal))
 161             return false;
 162         UnixNumericUserPrincipal that = (UnixNumericUserPrincipal)o;
 163 
 164         if (this.getName().equals(that.getName()))
 165             return true;
 166         return false;
 167     }
 168 
 169     /**
 170      * Return a hash code for this <code>UnixNumericUserPrincipal</code>.
 171      *
 172      * <p>
 173      *
 174      * @return a hash code for this <code>UnixNumericUserPrincipal</code>.
 175      */
 176     public int hashCode() {
 177         return name.hashCode();
 178     }
 179 }