1 /*
   2  * Copyright (c) 1999, 2006, 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 import sun.security.x509.X500Name;
  30 
  31 /**
  32  * <p> This class represents an X.500 <code>Principal</code>.
  33  * X500Principals have names such as,
  34  * "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"
  35  * (RFC 1779 style).
  36  *
  37  * <p> Principals such as this <code>X500Principal</code>
  38  * may be associated with a particular <code>Subject</code>
  39  * to augment that <code>Subject</code> with an additional
  40  * identity.  Refer to the <code>Subject</code> class for more information
  41  * on how to achieve this.  Authorization decisions can then be based upon
  42  * the Principals associated with a <code>Subject</code>.
  43  *
  44  * @see java.security.Principal
  45  * @see javax.security.auth.Subject
  46  * @deprecated A new X500Principal class is available in the Java platform.
  47  *             This X500Principal classs is entirely deprecated and
  48  *             is here to allow for a smooth transition to the new
  49  *             class.
  50  * @see javax.security.auth.x500.X500Principal
  51 */
  52 @Deprecated
  53 public class X500Principal implements Principal, java.io.Serializable {
  54 
  55     private static final long serialVersionUID = -8222422609431628648L;
  56 
  57     private static final java.util.ResourceBundle rb =
  58         java.security.AccessController.doPrivileged
  59         (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
  60               public java.util.ResourceBundle run() {
  61                   return (java.util.ResourceBundle.getBundle
  62                                 ("sun.security.util.AuthResources"));
  63               }
  64         });
  65 
  66     /**
  67      * @serial
  68      */
  69     private String name;
  70 
  71     transient private X500Name thisX500Name;
  72 
  73     /**
  74      * Create a X500Principal with an X.500 Name,
  75      * such as "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"
  76      * (RFC 1779 style).
  77      *
  78      * <p>
  79      *
  80      * @param name the X.500 name
  81      *
  82      * @exception NullPointerException if the <code>name</code>
  83      *                  is <code>null</code>. <p>
  84      *
  85      * @exception IllegalArgumentException if the <code>name</code>
  86      *                  is improperly specified.
  87      */
  88     public X500Principal(String name) {
  89         if (name == null)
  90             throw new NullPointerException(rb.getString("provided null name"));
  91 
  92         try {
  93             thisX500Name = new X500Name(name);
  94         } catch (Exception e) {
  95             throw new IllegalArgumentException(e.toString());
  96         }
  97 
  98         this.name = name;
  99     }
 100 
 101     /**
 102      * Return the Unix username for this <code>X500Principal</code>.
 103      *
 104      * <p>
 105      *
 106      * @return the Unix username for this <code>X500Principal</code>
 107      */
 108     public String getName() {
 109         return thisX500Name.getName();
 110     }
 111 
 112     /**
 113      * Return a string representation of this <code>X500Principal</code>.
 114      *
 115      * <p>
 116      *
 117      * @return a string representation of this <code>X500Principal</code>.
 118      */
 119     public String toString() {
 120         return thisX500Name.toString();
 121     }
 122 
 123     /**
 124      * Compares the specified Object with this <code>X500Principal</code>
 125      * for equality.
 126      *
 127      * <p>
 128      *
 129      * @param o Object to be compared for equality with this
 130      *          <code>X500Principal</code>.
 131      *
 132      * @return true if the specified Object is equal equal to this
 133      *          <code>X500Principal</code>.
 134      */
 135     public boolean equals(Object o) {
 136         if (o == null)
 137             return false;
 138 
 139         if (this == o)
 140             return true;
 141 
 142         if (o instanceof X500Principal) {
 143             X500Principal that = (X500Principal)o;
 144             try {
 145                 X500Name thatX500Name = new X500Name(that.getName());
 146                 return thisX500Name.equals(thatX500Name);
 147             } catch (Exception e) {
 148                 // any parsing exceptions, return false
 149                 return false;
 150             }
 151         } else if (o instanceof Principal) {
 152             // this will return 'true' if 'o' is a sun.security.x509.X500Name
 153             // and the X500Names are equal
 154             return o.equals(thisX500Name);
 155         }
 156 
 157         return false;
 158     }
 159 
 160     /**
 161      * Return a hash code for this <code>X500Principal</code>.
 162      *
 163      * <p>
 164      *
 165      * @return a hash code for this <code>X500Principal</code>.
 166      */
 167     public int hashCode() {
 168         return thisX500Name.hashCode();
 169     }
 170 
 171     /**
 172      * Reads this object from a stream (i.e., deserializes it)
 173      */
 174     private void readObject(java.io.ObjectInputStream s) throws
 175                                         java.io.IOException,
 176                                         java.io.NotActiveException,
 177                                         ClassNotFoundException {
 178 
 179         s.defaultReadObject();
 180 
 181         // re-create thisX500Name
 182         thisX500Name = new X500Name(name);
 183     }
 184 }