1 /*
   2  * Copyright (c) 2002, 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 
  27 package javax.management.remote;
  28 
  29 import java.io.IOException;
  30 import java.io.InvalidObjectException;
  31 import java.io.ObjectInputStream;
  32 import java.io.Serializable;
  33 import java.security.Principal;
  34 
  35 /**
  36  * <p>The identity of a remote client of the JMX Remote API.</p>
  37  *
  38  * <p>Principals such as this <code>JMXPrincipal</code>
  39  * may be associated with a particular <code>Subject</code>
  40  * to augment that <code>Subject</code> with an additional
  41  * identity.  Refer to the {@link javax.security.auth.Subject}
  42  * class for more information on how to achieve this.
  43  * Authorization decisions can then be based upon
  44  * the Principals associated with a <code>Subject</code>.
  45  *
  46  * @see java.security.Principal
  47  * @see javax.security.auth.Subject
  48  * @since 1.5
  49  */
  50 public class JMXPrincipal implements Principal, Serializable {
  51 
  52     private static final long serialVersionUID = -4184480100214577411L;
  53 
  54     /**
  55      * @serial The JMX Remote API name for the identity represented by
  56      * this <code>JMXPrincipal</code> object.
  57      * @see #getName()
  58      */
  59     private String name;
  60 
  61     /**
  62      * <p>Creates a JMXPrincipal for a given identity.</p>
  63      *
  64      * @param name the JMX Remote API name for this identity.
  65      *
  66      * @exception NullPointerException if the <code>name</code> is
  67      * <code>null</code>.
  68      */
  69     public JMXPrincipal(String name) {
  70         validate(name);
  71         this.name = name;
  72     }
  73 
  74     /**
  75      * Returns the name of this principal.
  76      *
  77      * <p>
  78      *
  79      * @return the name of this <code>JMXPrincipal</code>.
  80      */
  81     public String getName() {
  82         return name;
  83     }
  84 
  85     /**
  86      * Returns a string representation of this <code>JMXPrincipal</code>.
  87      *
  88      * <p>
  89      *
  90      * @return a string representation of this <code>JMXPrincipal</code>.
  91      */
  92     public String toString() {
  93         return("JMXPrincipal:  " + name);
  94     }
  95 
  96     /**
  97      * Compares the specified Object with this <code>JMXPrincipal</code>
  98      * for equality.  Returns true if the given object is also a
  99      * <code>JMXPrincipal</code> and the two JMXPrincipals
 100      * have the same name.
 101      *
 102      * <p>
 103      *
 104      * @param o Object to be compared for equality with this
 105      * <code>JMXPrincipal</code>.
 106      *
 107      * @return true if the specified Object is equal to this
 108      * <code>JMXPrincipal</code>.
 109      */
 110     public boolean equals(Object o) {
 111         if (o == null)
 112             return false;
 113 
 114         if (this == o)
 115             return true;
 116 
 117         if (!(o instanceof JMXPrincipal))
 118             return false;
 119         JMXPrincipal that = (JMXPrincipal)o;
 120 
 121         return (this.getName().equals(that.getName()));
 122     }
 123 
 124     /**
 125      * Returns a hash code for this <code>JMXPrincipal</code>.
 126      *
 127      * <p>
 128      *
 129      * @return a hash code for this <code>JMXPrincipal</code>.
 130      */
 131     public int hashCode() {
 132         return name.hashCode();
 133     }
 134 
 135     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
 136         ObjectInputStream.GetField gf = ois.readFields();
 137         String principalName = (String)gf.get("name", null);
 138         try {
 139             validate(principalName);
 140             this.name = principalName;
 141         } catch (NullPointerException e) {
 142             throw new InvalidObjectException(e.getMessage());
 143         }
 144     }
 145 
 146     private static void validate(String name) throws NullPointerException {
 147         if (name == null)
 148             throw new NullPointerException("illegal null input");
 149     }
 150 }