author | stefank |
Fri, 17 Apr 2015 17:10:38 +0000 | |
changeset 30266 | ef82cd1f2db3 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, 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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* Simple definition of a standard MBean, named "SimpleStandard". |
|
26 |
* |
|
27 |
* The "SimpleStandard" standard MBean shows how to expose attributes and |
|
28 |
* operations for management by implementing its corresponding |
|
29 |
* "SimpleStandardMBean" management interface. |
|
30 |
* |
|
31 |
* This MBean has two attributes and one operation exposed |
|
32 |
* for management by a JMX agent: |
|
33 |
* - the read/write "State" attribute, |
|
34 |
* - the read only "NbChanges" attribute, |
|
35 |
* - the "reset()" operation. |
|
36 |
* |
|
37 |
* This object also has one property and one method not exposed |
|
38 |
* for management by a JMX agent: |
|
39 |
* - the "NbResets" property, |
|
40 |
* - the "getNbResets()" method. |
|
41 |
*/ |
|
42 |
||
43 |
import java.security.AccessControlContext; |
|
44 |
import java.security.AccessController; |
|
45 |
import java.security.Principal; |
|
46 |
import java.util.Set; |
|
47 |
import javax.management.AttributeChangeNotification; |
|
48 |
import javax.management.NotificationBroadcasterSupport; |
|
49 |
import javax.management.remote.JMXPrincipal; |
|
50 |
import javax.security.auth.Subject; |
|
51 |
||
52 |
public class SimpleStandard |
|
53 |
extends NotificationBroadcasterSupport |
|
54 |
implements SimpleStandardMBean { |
|
55 |
||
56 |
/* |
|
57 |
* ----------------------------------------------------- |
|
58 |
* CONSTRUCTORS |
|
59 |
* ----------------------------------------------------- |
|
60 |
*/ |
|
61 |
||
62 |
public SimpleStandard(String principalName) { |
|
63 |
this.principalName = principalName; |
|
64 |
} |
|
65 |
||
66 |
/* |
|
67 |
* ----------------------------------------------------- |
|
68 |
* IMPLEMENTATION OF THE SimpleStandardMBean INTERFACE |
|
69 |
* ----------------------------------------------------- |
|
70 |
*/ |
|
71 |
||
72 |
/** |
|
73 |
* Getter: get the "State" attribute of the "SimpleStandard" standard MBean. |
|
74 |
* |
|
75 |
* @return the current value of the "State" attribute. |
|
76 |
*/ |
|
77 |
public String getState() { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
78 |
checkSubject("getState"); |
2 | 79 |
return state; |
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Setter: set the "State" attribute of the "SimpleStandard" standard MBean. |
|
84 |
* |
|
85 |
* @param <VAR>s</VAR> the new value of the "State" attribute. |
|
86 |
*/ |
|
87 |
public void setState(String s) { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
88 |
checkSubject("setState"); |
2 | 89 |
state = s; |
90 |
nbChanges++; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Getter: get the "NbChanges" attribute of the "SimpleStandard" standard |
|
95 |
* MBean. |
|
96 |
* |
|
97 |
* @return the current value of the "NbChanges" attribute. |
|
98 |
*/ |
|
99 |
public int getNbChanges() { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
100 |
checkSubject("getNbChanges"); |
2 | 101 |
return nbChanges; |
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Operation: reset to their initial values the "State" and "NbChanges" |
|
106 |
* attributes of the "SimpleStandard" standard MBean. |
|
107 |
*/ |
|
108 |
public void reset() { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
109 |
checkSubject("reset"); |
2 | 110 |
AttributeChangeNotification acn = |
111 |
new AttributeChangeNotification(this, |
|
112 |
0, |
|
113 |
0, |
|
114 |
"NbChanges reset", |
|
115 |
"NbChanges", |
|
116 |
"Integer", |
|
117 |
new Integer(nbChanges), |
|
118 |
new Integer(0)); |
|
119 |
state = "initial state"; |
|
120 |
nbChanges = 0; |
|
121 |
nbResets++; |
|
122 |
sendNotification(acn); |
|
123 |
} |
|
124 |
||
125 |
/* |
|
126 |
* ----------------------------------------------------- |
|
127 |
* METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT |
|
128 |
* ----------------------------------------------------- |
|
129 |
*/ |
|
130 |
||
131 |
/** |
|
132 |
* Return the "NbResets" property. |
|
133 |
* This method is not a Getter in the JMX sense because |
|
134 |
* it is not exposed in the "SimpleStandardMBean" interface. |
|
135 |
* |
|
136 |
* @return the current value of the "NbResets" property. |
|
137 |
*/ |
|
138 |
public int getNbResets() { |
|
139 |
return nbResets; |
|
140 |
} |
|
141 |
||
142 |
/* |
|
143 |
* --------------- |
|
144 |
* PRIVATE METHODS |
|
145 |
* --------------- |
|
146 |
*/ |
|
147 |
||
148 |
/** |
|
149 |
* Check that the principal contained in the Subject is of |
|
150 |
* type JMXPrincipal and refers to the principalName identity. |
|
151 |
*/ |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
152 |
private void checkSubject(String op) { |
2 | 153 |
AccessControlContext acc = AccessController.getContext(); |
154 |
Subject subject = Subject.getSubject(acc); |
|
155 |
Set principals = subject.getPrincipals(); |
|
156 |
Principal principal = (Principal) principals.iterator().next(); |
|
157 |
if (!(principal instanceof JMXPrincipal)) |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
158 |
throw new SecurityException(op+": Authenticated subject contains " + |
2 | 159 |
"invalid principal type = " + |
160 |
principal.getClass().getName()); |
|
161 |
String identity = principal.getName(); |
|
162 |
if (!identity.equals(principalName)) |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
2
diff
changeset
|
163 |
throw new SecurityException(op+": Authenticated subject contains " + |
2 | 164 |
"invalid principal name = " + identity); |
165 |
} |
|
166 |
||
167 |
/* |
|
168 |
* ----------------------------------------------------- |
|
169 |
* ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT |
|
170 |
* ----------------------------------------------------- |
|
171 |
*/ |
|
172 |
||
173 |
private String state = "initial state"; |
|
174 |
private int nbChanges = 0; |
|
175 |
||
176 |
/* |
|
177 |
* ----------------------------------------------------- |
|
178 |
* PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT |
|
179 |
* ----------------------------------------------------- |
|
180 |
*/ |
|
181 |
||
182 |
private int nbResets = 0; |
|
183 |
private String principalName; |
|
184 |
} |