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