jdk/src/java.management/share/classes/javax/management/remote/JMXPrincipal.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24368 jdk/src/share/classes/javax/management/remote/JMXPrincipal.java@2b4801b94265
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
20809
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
    29
import java.io.IOException;
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
    30
import java.io.InvalidObjectException;
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
    31
import java.io.ObjectInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>The identity of a remote client of the JMX Remote API.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>Principals such as this <code>JMXPrincipal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * may be associated with a particular <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to augment that <code>Subject</code> with an additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * identity.  Refer to the {@link javax.security.auth.Subject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * class for more information on how to achieve this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Authorization decisions can then be based upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the Principals associated with a <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.security.Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see javax.security.auth.Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class JMXPrincipal implements Principal, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final long serialVersionUID = -4184480100214577411L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @serial The JMX Remote API name for the identity represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * this <code>JMXPrincipal</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see #getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
    62
     * Creates a JMXPrincipal for a given identity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param name the JMX Remote API name for this identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @exception NullPointerException if the <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public JMXPrincipal(String name) {
20809
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
    70
        validate(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the name of this principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the name of this <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Returns a string representation of this <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return a string representation of this <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return("JMXPrincipal:  " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Compares the specified Object with this <code>JMXPrincipal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * for equality.  Returns true if the given object is also a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <code>JMXPrincipal</code> and the two JMXPrincipals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * have the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param o Object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return true if the specified Object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (this == o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!(o instanceof JMXPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        JMXPrincipal that = (JMXPrincipal)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return (this.getName().equals(that.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns a hash code for this <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return a hash code for this <code>JMXPrincipal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
20809
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   126
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   127
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   128
        ObjectInputStream.GetField gf = ois.readFields();
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   129
        String principalName = (String)gf.get("name", null);
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   130
        try {
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   131
            validate(principalName);
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   132
            this.name = principalName;
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   133
        } catch (NullPointerException e) {
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   134
            throw new InvalidObjectException(e.getMessage());
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   135
        }
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   136
    }
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   137
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   138
    private static void validate(String name) throws NullPointerException {
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   139
        if (name == null)
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   140
            throw new NullPointerException("illegal null input");
b5e0612d60b9 8014085: Better serialization support in JMX classes
jbachorik
parents: 5506
diff changeset
   141
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}