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 * Creates a JMXPrincipal for a given identity. 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 * @return the name of this <code>JMXPrincipal</code>. 78 */ 79 public String getName() { 80 return name; 81 } 82 83 /** 84 * Returns a string representation of this <code>JMXPrincipal</code>. 85 * 86 * @return a string representation of this <code>JMXPrincipal</code>. 87 */ 88 public String toString() { 89 return("JMXPrincipal: " + name); 90 } 91 92 /** 93 * Compares the specified Object with this <code>JMXPrincipal</code> 94 * for equality. Returns true if the given object is also a 95 * <code>JMXPrincipal</code> and the two JMXPrincipals 96 * have the same name. 97 * 98 * @param o Object to be compared for equality with this 99 * <code>JMXPrincipal</code>. 100 * 101 * @return true if the specified Object is equal to this 102 * <code>JMXPrincipal</code>. 103 */ 104 public boolean equals(Object o) { 105 if (o == null) 106 return false; 107 108 if (this == o) 109 return true; 110 111 if (!(o instanceof JMXPrincipal)) 112 return false; 113 JMXPrincipal that = (JMXPrincipal)o; 114 115 return (this.getName().equals(that.getName())); 116 } 117 118 /** 119 * Returns a hash code for this <code>JMXPrincipal</code>. 120 * 121 * @return a hash code for this <code>JMXPrincipal</code>. 122 */ 123 public int hashCode() { 124 return name.hashCode(); 125 } 126 127 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { 128 ObjectInputStream.GetField gf = ois.readFields(); 129 String principalName = (String)gf.get("name", null); 130 try { 131 validate(principalName); 132 this.name = principalName; 133 } catch (NullPointerException e) { 134 throw new InvalidObjectException(e.getMessage()); 135 } 136 } 137 138 private static void validate(String name) throws NullPointerException { 139 if (name == null) 140 throw new NullPointerException("illegal null input"); 141 } 142 }