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-- |
2 | 1 |
/* |
24368 | 2 |
* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package javax.management.remote; |
|
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 | 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 |
/** |
|
24368 | 62 |
* Creates a JMXPrincipal for a given identity. |
2 | 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) { |
|
20809
b5e0612d60b9
8014085: Better serialization support in JMX classes
jbachorik
parents:
5506
diff
changeset
|
70 |
validate(name); |
2 | 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 |
} |
|
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 | 142 |
} |