author | emcmanus |
Wed, 06 Aug 2008 18:28:53 +0200 | |
changeset 1011 | 2cc4873fa29f |
parent 1004 | 5ba8217eb504 |
child 1153 | 6b88c071a015 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2005-2008 Sun Microsystems, Inc. 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.management; |
|
27 |
||
28 |
import java.lang.annotation.Documented; |
|
29 |
import java.lang.annotation.ElementType; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
30 |
import java.lang.annotation.Inherited; |
2 | 31 |
import java.lang.annotation.Retention; |
32 |
import java.lang.annotation.RetentionPolicy; |
|
33 |
import java.lang.annotation.Target; |
|
34 |
||
35 |
// remaining imports are for Javadoc |
|
36 |
import java.io.InvalidObjectException; |
|
37 |
import java.lang.management.MemoryUsage; |
|
38 |
import java.lang.reflect.UndeclaredThrowableException; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.List; |
|
41 |
import javax.management.openmbean.ArrayType; |
|
42 |
import javax.management.openmbean.CompositeData; |
|
43 |
import javax.management.openmbean.CompositeDataInvocationHandler; |
|
44 |
import javax.management.openmbean.CompositeDataSupport; |
|
45 |
import javax.management.openmbean.CompositeDataView; |
|
46 |
import javax.management.openmbean.CompositeType; |
|
687 | 47 |
import javax.management.openmbean.MXBeanMapping; |
48 |
import javax.management.openmbean.MXBeanMappingClass; |
|
49 |
import javax.management.openmbean.MXBeanMappingFactory; |
|
50 |
import javax.management.openmbean.MXBeanMappingFactoryClass; |
|
2 | 51 |
import javax.management.openmbean.OpenDataException; |
52 |
import javax.management.openmbean.OpenMBeanInfo; |
|
53 |
import javax.management.openmbean.OpenType; |
|
54 |
import javax.management.openmbean.SimpleType; |
|
55 |
import javax.management.openmbean.TabularData; |
|
56 |
import javax.management.openmbean.TabularDataSupport; |
|
57 |
import javax.management.openmbean.TabularType; |
|
58 |
||
59 |
/** |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
60 |
<p>Annotation to mark a class or interface explicitly as being an MXBean, |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
61 |
or as not being an MXBean. By default, an |
2 | 62 |
interface is an MXBean interface if its name ends with {@code |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
63 |
MXBean}, as in {@code SomethingMXBean}. A class is never an MXBean by |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
64 |
default.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
65 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
66 |
<p>The following interfaces are MXBean interfaces:</p> |
2 | 67 |
|
68 |
<pre> |
|
69 |
public interface WhatsitMXBean {} |
|
70 |
||
71 |
@MXBean |
|
72 |
public interface Whatsit1Interface {} |
|
73 |
||
74 |
@MXBean(true) |
|
75 |
public interface Whatsit2Interface {} |
|
76 |
</pre> |
|
77 |
||
78 |
<p>The following interfaces are not MXBean interfaces:</p> |
|
79 |
||
80 |
<pre> |
|
81 |
public interface Whatsit3Interface{} |
|
82 |
||
83 |
@MXBean(false) |
|
84 |
public interface MisleadingMXBean {} |
|
85 |
</pre> |
|
86 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
87 |
<p>A class can be annotated with {@code @MXBean} to indicate that it |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
88 |
is an MXBean. In this case, its methods should have <code>@{@link |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
89 |
ManagedAttribute}</code> or <code>@{@link ManagedOperation}</code> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
90 |
annotations, as described for <code>@{@link MBean}</code>.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
91 |
|
687 | 92 |
<h3 id="MXBean-spec">MXBean specification</h3> |
2 | 93 |
|
94 |
<p>The MXBean concept provides a simple way to code an MBean |
|
95 |
that only references a predefined set of types, the ones defined |
|
96 |
by {@link javax.management.openmbean}. In this way, you can be |
|
97 |
sure that your MBean will be usable by any client, including |
|
98 |
remote clients, without any requirement that the client have |
|
99 |
access to <em>model-specific classes</em> representing the types |
|
100 |
of your MBeans.</p> |
|
101 |
||
102 |
<p>The concepts are easier to understand by comparison with the |
|
103 |
Standard MBean concept. Here is how a managed object might be |
|
104 |
represented as a Standard MBean, and as an MXBean:</p> |
|
105 |
||
106 |
<table border="1" cellpadding="5"> |
|
107 |
<tr> |
|
108 |
<th>Standard MBean</th><th>MXBean</th> |
|
109 |
</tr> |
|
110 |
<tr> |
|
111 |
<td><pre> |
|
112 |
public interface MemoryPool<b>MBean</b> { |
|
113 |
String getName(); |
|
114 |
MemoryUsage getUsage(); |
|
115 |
// ... |
|
116 |
} |
|
117 |
</pre></td> |
|
118 |
<td><pre> |
|
119 |
public interface MemoryPool<b>MXBean</b> { |
|
120 |
String getName(); |
|
121 |
MemoryUsage getUsage(); |
|
122 |
// ... |
|
123 |
} |
|
124 |
</pre></td> |
|
125 |
</tr> |
|
126 |
</table> |
|
127 |
||
128 |
<p>As you can see, the definitions are very similar. The only |
|
129 |
difference is that the convention for naming the interface is to use |
|
130 |
<code><em>Something</em>MXBean</code> for MXBeans, rather than |
|
131 |
<code><em>Something</em>MBean</code> for Standard MBeans.</p> |
|
132 |
||
133 |
<p>In this managed object, there is an attribute called |
|
134 |
<code>Usage</code> of type {@link MemoryUsage}. The point of an |
|
135 |
attribute like this is that it gives a coherent snapshot of a set |
|
136 |
of data items. For example, it might include the current amount |
|
137 |
of used memory in the memory pool, and the current maximum of the |
|
138 |
memory pool. If these were separate items, obtained with separate |
|
139 |
{@link MBeanServer#getAttribute getAttribute} calls, then we could |
|
140 |
get values seen at different times that were not consistent. We |
|
141 |
might get a <code>used</code> value that was greater than the |
|
142 |
<code>max</code> value.</p> |
|
143 |
||
144 |
<p>So, we might define <code>MemoryUsage</code> like this:</p> |
|
145 |
||
146 |
<table border="1" cellpadding="5"> |
|
147 |
<tr> |
|
148 |
<th>Standard MBean</th><th>MXBean</th> |
|
149 |
</tr> |
|
150 |
<tr> |
|
151 |
<td><pre> |
|
152 |
public class MemoryUsage <b>implements Serializable</b> { |
|
153 |
// standard JavaBean conventions with getters |
|
154 |
||
155 |
public MemoryUsage(long init, long used, |
|
156 |
long committed, long max) {...} |
|
157 |
long getInit() {...} |
|
158 |
long getUsed() {...} |
|
159 |
long getCommitted() {...} |
|
160 |
long getMax() {...} |
|
161 |
} |
|
162 |
</pre></td> |
|
163 |
<td><pre> |
|
164 |
public class MemoryUsage { |
|
165 |
// standard JavaBean conventions with getters |
|
166 |
<b>@ConstructorProperties({"init", "used", "committed", "max"})</b> |
|
167 |
public MemoryUsage(long init, long used, |
|
168 |
long committed, long max) {...} |
|
169 |
long getInit() {...} |
|
170 |
long getUsed() {...} |
|
171 |
long getCommitted() {...} |
|
172 |
long getMax() {...} |
|
173 |
} |
|
174 |
</pre></td> |
|
175 |
</tr> |
|
176 |
</table> |
|
177 |
||
178 |
<p>The definitions are the same in the two cases, except |
|
179 |
that with the MXBean, <code>MemoryUsage</code> no longer needs to |
|
180 |
be marked <code>Serializable</code> (though it can be). On |
|
181 |
the other hand, we have added a {@code @ConstructorProperties} annotation |
|
182 |
to link the constructor parameters to the corresponding getters. |
|
183 |
We will see more about this below.</p> |
|
184 |
||
185 |
<p><code>MemoryUsage</code> is a <em>model-specific class</em>. |
|
186 |
With Standard MBeans, a client of the MBean Server cannot access the |
|
187 |
<code>Usage</code> attribute if it does not know the class |
|
188 |
<code>MemoryUsage</code>. Suppose the client is a generic console |
|
189 |
based on JMX technology. Then the console would have to be |
|
190 |
configured with the model-specific classes of every application it |
|
191 |
might connect to. The problem is even worse for clients that are |
|
192 |
not written in the Java language. Then there may not be any way |
|
193 |
to tell the client what a <code>MemoryUsage</code> looks like.</p> |
|
194 |
||
195 |
<p>This is where MXBeans differ from Standard MBeans. Although we |
|
196 |
define the management interface in almost exactly the same way, |
|
197 |
the MXBean framework <em>converts</em> model-specific classes into |
|
198 |
standard classes from the Java platform. Using arrays and the |
|
199 |
{@link javax.management.openmbean.CompositeData CompositeData} and |
|
200 |
{@link javax.management.openmbean.TabularData TabularData} classes |
|
201 |
from the standard {@link javax.management.openmbean} package, it |
|
202 |
is possible to build data structures of arbitrary complexity |
|
203 |
using only standard classes.</p> |
|
204 |
||
205 |
<p>This becomes clearer if we compare what the clients of the two |
|
206 |
models might look like:</p> |
|
207 |
||
208 |
<table border="1" cellpadding="5"> |
|
209 |
<tr> |
|
210 |
<th>Standard MBean</th><th>MXBean</th> |
|
211 |
</tr> |
|
212 |
<tr> |
|
213 |
<td><pre> |
|
214 |
String name = (String) |
|
215 |
mbeanServer.{@link MBeanServer#getAttribute |
|
216 |
getAttribute}(objectName, "Name"); |
|
217 |
<b>MemoryUsage</b> usage = (<b>MemoryUsage</b>) |
|
218 |
mbeanServer.getAttribute(objectName, "Usage"); |
|
219 |
<b>long used = usage.getUsed();</b> |
|
220 |
</pre></td> |
|
221 |
<td><pre> |
|
222 |
String name = (String) |
|
223 |
mbeanServer.{@link MBeanServer#getAttribute |
|
224 |
getAttribute}(objectName, "Name"); |
|
225 |
<b>{@link CompositeData}</b> usage = (<b>CompositeData</b>) |
|
226 |
mbeanServer.getAttribute(objectName, "Usage"); |
|
227 |
<b>long used = (Long) usage.{@link CompositeData#get get}("used");</b> |
|
228 |
</pre></td> |
|
229 |
</table> |
|
230 |
||
231 |
<p>For attributes with simple types like <code>String</code>, the |
|
232 |
code is the same. But for attributes with complex types, the |
|
233 |
Standard MBean code requires the client to know the model-specific |
|
234 |
class <code>MemoryUsage</code>, while the MXBean code requires no |
|
235 |
non-standard classes.</p> |
|
236 |
||
237 |
<p>The client code shown here is slightly more complicated for the |
|
238 |
MXBean client. But, if the client does in fact know the model, |
|
239 |
here the interface <code>MemoryPoolMXBean</code> and the |
|
240 |
class <code>MemoryUsage</code>, then it can construct a |
|
241 |
<em>proxy</em>. This is the recommended way to interact with |
|
242 |
managed objects when you know the model beforehand, regardless |
|
243 |
of whether you are using Standard MBeans or MXBeans:</p> |
|
244 |
||
245 |
<table border="1" cellpadding="5"> |
|
246 |
<tr> |
|
247 |
<th>Standard MBean</th><th>MXBean</th> |
|
248 |
</tr> |
|
249 |
<tr> |
|
250 |
<td><pre> |
|
251 |
MemoryPool<b>MBean</b> proxy = |
|
252 |
JMX.<b>{@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName, |
|
253 |
Class) newMBeanProxy}</b>( |
|
254 |
mbeanServer, |
|
255 |
objectName, |
|
256 |
MemoryPool<b>MBean</b>.class); |
|
257 |
String name = proxy.getName(); |
|
258 |
MemoryUsage usage = proxy.getUsage(); |
|
259 |
long used = usage.getUsed(); |
|
260 |
</pre></td> |
|
261 |
<td><pre> |
|
262 |
MemoryPool<b>MXBean</b> proxy = |
|
263 |
JMX.<b>{@link JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, |
|
264 |
Class) newMXBeanProxy}</b>( |
|
265 |
mbeanServer, |
|
266 |
objectName, |
|
267 |
MemoryPool<b>MXBean</b>.class); |
|
268 |
String name = proxy.getName(); |
|
269 |
MemoryUsage usage = proxy.getUsage(); |
|
270 |
long used = usage.getUsed(); |
|
271 |
</pre></td> |
|
272 |
</tr> |
|
273 |
</table> |
|
274 |
||
275 |
<p>Implementing the MemoryPool object works similarly for both |
|
276 |
Standard MBeans and MXBeans.</p> |
|
277 |
||
278 |
<table border="1" cellpadding="5"> |
|
279 |
<tr> |
|
280 |
<th>Standard MBean</th><th>MXBean</th> |
|
281 |
</tr> |
|
282 |
<tr> |
|
283 |
<td><pre> |
|
284 |
public class MemoryPool |
|
285 |
implements MemoryPool<b>MBean</b> { |
|
286 |
public String getName() {...} |
|
287 |
public MemoryUsage getUsage() {...} |
|
288 |
// ... |
|
289 |
} |
|
290 |
</pre></td> |
|
291 |
<td><pre> |
|
292 |
public class MemoryPool |
|
293 |
implements MemoryPool<b>MXBean</b> { |
|
294 |
public String getName() {...} |
|
295 |
public MemoryUsage getUsage() {...} |
|
296 |
// ... |
|
297 |
} |
|
298 |
</pre></td> |
|
299 |
</tr> |
|
300 |
</table> |
|
301 |
||
302 |
<p>Registering the MBean in the MBean Server works in the same way |
|
303 |
in both cases:</p> |
|
304 |
||
305 |
<table border="1" cellpadding="5"> |
|
306 |
<tr> |
|
307 |
<th>Standard MBean</th><th>MXBean</th> |
|
308 |
</tr> |
|
309 |
<tr> |
|
310 |
<td><pre> |
|
311 |
{ |
|
312 |
MemoryPool<b>MBean</b> pool = new MemoryPool(); |
|
313 |
mbeanServer.{@link MBeanServer#registerMBean |
|
314 |
registerMBean}(pool, objectName); |
|
315 |
} |
|
316 |
</pre></td> |
|
317 |
<td><pre> |
|
318 |
{ |
|
319 |
MemoryPool<b>MXBean</b> pool = new MemoryPool(); |
|
320 |
mbeanServer.{@link MBeanServer#registerMBean |
|
321 |
registerMBean}(pool, objectName); |
|
322 |
} |
|
323 |
</pre></td> |
|
324 |
</tr> |
|
325 |
</table> |
|
326 |
||
327 |
||
687 | 328 |
<h2 id="mxbean-def">Definition of an MXBean</h2> |
2 | 329 |
|
330 |
<p>An MXBean is a kind of MBean. An MXBean object can be |
|
331 |
registered directly in the MBean Server, or it can be used as an |
|
332 |
argument to {@link StandardMBean} and the resultant MBean |
|
333 |
registered in the MBean Server.</p> |
|
334 |
||
335 |
<p>When an object is registered in the MBean Server using the |
|
336 |
{@code registerMBean} or {@code createMBean} methods of the |
|
337 |
{@link MBeanServer} interface, the object's class is examined |
|
338 |
to determine what type of MBean it is:</p> |
|
339 |
||
340 |
<ul> |
|
341 |
<li>If the class implements the interface {@link DynamicMBean} |
|
342 |
then the MBean is a Dynamic MBean. Note that the class |
|
343 |
{@code StandardMBean} implements this interface, so this |
|
344 |
case applies to a Standard MBean or MXBean created using |
|
345 |
the class {@code StandardMBean}.</li> |
|
346 |
||
347 |
<li>Otherwise, if the class matches the Standard MBean naming |
|
348 |
conventions, then the MBean is a Standard MBean.</li> |
|
349 |
||
350 |
<li>Otherwise, it may be an MXBean. The set of interfaces |
|
351 |
implemented by the object is examined for interfaces that: |
|
352 |
||
353 |
<ul> |
|
354 |
<li>have a class name <code><em>S</em>MXBean</code> where |
|
355 |
<code><em>S</em></code> is any non-empty string, and |
|
356 |
do not have an annotation {@code @MXBean(false)}; and/or</li> |
|
357 |
<li>have an annotation {@code @MXBean(true)} |
|
358 |
or just {@code @MXBean}.</li> |
|
359 |
</ul> |
|
360 |
||
361 |
If there is exactly one such interface, or if there is one |
|
362 |
such interface that is a subinterface of all the others, then |
|
363 |
the object is an MXBean. The interface in question is the |
|
364 |
<em>MXBean interface</em>. In the example above, the MXBean |
|
365 |
interface is {@code MemoryPoolMXBean}. |
|
366 |
||
367 |
<li>If none of these conditions is met, the MBean is invalid and |
|
368 |
the attempt to register it will generate {@link |
|
369 |
NotCompliantMBeanException}. |
|
370 |
</ul> |
|
371 |
||
372 |
<p>Every Java type that appears as the parameter or return type of a |
|
373 |
method in an MXBean interface must be <em>convertible</em> using |
|
374 |
the rules below. Additionally, parameters must be |
|
375 |
<em>reconstructible</em> as defined below.</p> |
|
376 |
||
377 |
<p>An attempt to construct an MXBean that does not conform to the |
|
378 |
above rules will produce an exception.</p> |
|
379 |
||
380 |
||
687 | 381 |
<h2 id="naming-conv">Naming conventions</h2> |
2 | 382 |
|
383 |
<p>The same naming conventions are applied to the methods in an |
|
384 |
MXBean as in a Standard MBean:</p> |
|
385 |
||
386 |
<ol> |
|
387 |
<li>A method <code><em>T</em> get<em>N</em>()</code>, where |
|
388 |
<code><em>T</em></code> is a Java type (not <code>void</code>) |
|
389 |
and <code><em>N</em></code> is a non-empty string, specifies |
|
390 |
that there is a readable attribute called |
|
391 |
<code><em>N</em></code>. The Java type and Open type of the |
|
392 |
attribute are determined by the mapping rules below. |
|
393 |
The method {@code final Class getClass()} inherited from {@code |
|
394 |
Object} is ignored when looking for getters.</li> |
|
395 |
||
396 |
<li>A method <code>boolean is<em>N</em>()</code> specifies that |
|
397 |
there is a readable attribute called <code><em>N</em></code> |
|
398 |
with Java type <code>boolean</code> and Open type |
|
399 |
<code>SimpleType.Boolean</code>.</li> |
|
400 |
||
401 |
<li>A method <code>void set<em>N</em>(<em>T</em> x)</code> |
|
402 |
specifies that there is a writeable attribute called |
|
403 |
<code><em>N</em></code>. The Java type and Open type of the |
|
404 |
attribute are determined by the mapping rules below. (Of |
|
405 |
course, the name <code>x</code> of the parameter is |
|
406 |
irrelevant.)</li> |
|
407 |
||
408 |
<li>Every other method specifies that there is an operation with |
|
409 |
the same name as the method. The Java type and Open type of the |
|
410 |
return value and of each parameter are determined by the mapping |
|
411 |
rules below.</li> |
|
412 |
</ol> |
|
413 |
||
414 |
<p>The rules for <code>get<em>N</em></code> and |
|
415 |
<code>is<em>N</em></code> collectively define the notion of a |
|
416 |
<em>getter</em>. The rule for <code>set<em>N</em></code> defines |
|
417 |
the notion of a <em>setter</em>.</p> |
|
418 |
||
419 |
<p>It is an error for there to be two getters with the same name, or |
|
420 |
two setters with the same name. If there is a getter and a setter |
|
421 |
for the same name, then the type <code><em>T</em></code> in both |
|
422 |
must be the same. In this case the attribute is read/write. If |
|
423 |
there is only a getter or only a setter, the attribute is |
|
424 |
read-only or write-only respectively.</p> |
|
425 |
||
426 |
||
687 | 427 |
<h2 id="mapping-rules">Type mapping rules</h2> |
2 | 428 |
|
429 |
<p>An MXBean is a kind of Open MBean, as defined by the {@link |
|
430 |
javax.management.openmbean} package. This means that the types of |
|
431 |
attributes, operation parameters, and operation return values must |
|
432 |
all be describable using <em>Open Types</em>, that is the four |
|
433 |
standard subclasses of {@link javax.management.openmbean.OpenType}. |
|
434 |
MXBeans achieve this by mapping Java types into Open Types.</p> |
|
435 |
||
436 |
<p>For every Java type <em>J</em>, the MXBean mapping is described |
|
437 |
by the following information:</p> |
|
438 |
||
439 |
<ul> |
|
440 |
<li>The corresponding Open Type, <em>opentype(J)</em>. This is |
|
441 |
an instance of a subclass of {@link |
|
442 |
javax.management.openmbean.OpenType}.</li> |
|
443 |
<li>The <em>mapped</em> Java type, <em>opendata(J)</em>, which is |
|
444 |
always the same for any given <em>opentype(J)</em>. This is a Java |
|
445 |
class.</li> |
|
446 |
<li>How a value is converted from type <em>J</em> to type |
|
447 |
<em>opendata(J)</em>.</li> |
|
448 |
<li>How a value is converted from type <em>opendata(J)</em> to |
|
449 |
type <em>J</em>, if it can be.</li> |
|
450 |
</ul> |
|
451 |
||
452 |
<p>For example, for the Java type {@code List<String>}:</p> |
|
453 |
||
454 |
<ul> |
|
455 |
<li>The Open Type, <em>opentype(</em>{@code |
|
456 |
List<String>}<em>)</em>, is {@link ArrayType}<code>(1, </code>{@link |
|
457 |
SimpleType#STRING}<code>)</code>, representing a 1-dimensional |
|
458 |
array of <code>String</code>s.</li> |
|
459 |
<li>The mapped Java type, <em>opendata(</em>{@code |
|
460 |
List<String>}<em>)</em>, is {@code String[]}.</li> |
|
461 |
<li>A {@code List<String>} can be converted to a {@code String[]} |
|
462 |
using {@link List#toArray(Object[]) List.toArray(new |
|
463 |
String[0])}.</li> |
|
464 |
<li>A {@code String[]} can be converted to a {@code List<String>} |
|
465 |
using {@link Arrays#asList Arrays.asList}.</li> |
|
466 |
</ul> |
|
467 |
||
468 |
<p>If no mapping rules exist to derive <em>opentype(J)</em> from |
|
469 |
<em>J</em>, then <em>J</em> cannot be the type of a method |
|
470 |
parameter or return value in an MXBean interface.</p> |
|
471 |
||
472 |
<p>If there is a way to convert <em>opendata(J)</em> back to |
|
473 |
<em>J</em> then we say that <em>J</em> is |
|
474 |
<em>reconstructible</em>. All method parameters in an MXBean |
|
475 |
interface must be reconstructible, because when the MXBean |
|
476 |
framework is invoking a method it will need to convert those |
|
477 |
parameters from <em>opendata(J)</em> to <em>J</em>. In a proxy |
|
478 |
generated by {@link JMX#newMXBeanProxy(MBeanServerConnection, |
|
479 |
ObjectName, Class) JMX.newMXBeanProxy}, it is the return values |
|
480 |
of the methods in the MXBean interface that must be |
|
481 |
reconstructible.</p> |
|
482 |
||
483 |
<p>Null values are allowed for all Java types and Open Types, |
|
484 |
except primitive Java types where they are not possible. When |
|
485 |
converting from type <em>J</em> to type <em>opendata(J)</em> or |
|
486 |
from type <em>opendata(J)</em> to type <em>J</em>, a null value is |
|
487 |
mapped to a null value.</p> |
|
488 |
||
687 | 489 |
<p>In addition to the default type mapping rules, you can specify |
490 |
custom type mappings, as described <a |
|
491 |
href="#custom">below</a>.</p> |
|
492 |
||
493 |
<p>The following table summarizes the default type mapping rules.</p> |
|
2 | 494 |
|
495 |
<table border="1" cellpadding="5"> |
|
496 |
<tr> |
|
497 |
<th>Java type <em>J</em></th> |
|
498 |
<th><em>opentype(J)</em></th> |
|
499 |
<th><em>opendata(J)</em></th> |
|
500 |
</tr> |
|
501 |
<tbody cellvalign="top"> |
|
502 |
<tr> |
|
503 |
<td>{@code int}, {@code boolean}, etc<br> |
|
504 |
(the 8 primitive Java types)</td> |
|
505 |
<td>{@code SimpleType.INTEGER},<br> |
|
506 |
{@code SimpleType.BOOLEAN}, etc</td> |
|
507 |
<td>{@code Integer}, {@code Boolean}, etc<br> |
|
508 |
(the corresponding boxed types)</td> |
|
509 |
</tr> |
|
510 |
<tr> |
|
511 |
<td>{@code Integer}, {@code ObjectName}, etc<br> |
|
512 |
(the types covered by {@link SimpleType})</td> |
|
513 |
<td>the corresponding {@code SimpleType}</td> |
|
514 |
<td><em>J</em>, the same type</td> |
|
515 |
</tr> |
|
516 |
<tr> |
|
517 |
<td>{@code int[]} etc<br> |
|
518 |
(a one-dimensional array with<br> |
|
519 |
primitive element type)</td> |
|
520 |
<td>{@code ArrayType.getPrimitiveArrayType(int[].class)} etc</td> |
|
521 |
<td><em>J</em>, the same type</td> |
|
522 |
<tr> |
|
523 |
<td><em>E</em>{@code []}<br> |
|
524 |
(an array with non-primitive element type <em>E</em>; |
|
525 |
this includes {@code int[][]}, where <em>E</em> is {@code int[]})</td> |
|
526 |
<td>{@code ArrayType.getArrayType(}<em>opentype(E)</em>{@code )}</td> |
|
527 |
<td><em>opendata(E)</em>{@code []}</td> |
|
528 |
</tr> |
|
529 |
<tr> |
|
530 |
<td>{@code List<}<em>E</em>{@code >}<br> |
|
531 |
{@code Set<}<em>E</em>{@code >}<br> |
|
532 |
{@code SortedSet<}<em>E</em>{@code >} (see below)</td> |
|
533 |
<td>same as for <em>E</em>{@code []}</td> |
|
534 |
<td>same as for <em>E</em>{@code []}</td> |
|
535 |
</tr> |
|
536 |
<tr> |
|
537 |
<td>An enumeration <em>E</em><br> |
|
538 |
(declared in Java as {@code enum }<em>E</em> |
|
539 |
{@code {...}})</td> |
|
540 |
<td>{@code SimpleType.STRING}</td> |
|
541 |
<td>{@code String}</td> |
|
542 |
</tr> |
|
543 |
<tr> |
|
544 |
<td>{@code Map<}<em>K</em>,<em>V</em>{@code >}<br> |
|
545 |
{@code SortedMap<}<em>K</em>,<em>V</em>{@code >}</td> |
|
546 |
<td>{@link TabularType}<br> |
|
547 |
(see below)</td> |
|
548 |
<td>{@link TabularData}<br> |
|
549 |
(see below)</td> |
|
550 |
</tr> |
|
551 |
<tr> |
|
552 |
<td>An MXBean interface</td> |
|
553 |
<td>{@code SimpleType.OBJECTNAME}<br> |
|
554 |
(see below)</td> |
|
555 |
<td>{@link ObjectName}<br> |
|
556 |
(see below)</td> |
|
557 |
</tr> |
|
558 |
<tr> |
|
559 |
<td>Any other type</td> |
|
560 |
<td>{@link CompositeType}, |
|
561 |
if possible<br> |
|
562 |
(see below)</td> |
|
563 |
<td>{@link CompositeData}</td> |
|
564 |
</tbody> |
|
565 |
</table> |
|
566 |
||
567 |
<p>The following sections give further details of these rules.</p> |
|
568 |
||
569 |
||
570 |
<h3>Mappings for primitive types</h3> |
|
571 |
||
572 |
<p>The 8 primitive Java types |
|
573 |
({@code boolean}, {@code byte}, {@code short}, {@code int}, {@code |
|
574 |
long}, {@code float}, {@code double}, {@code char}) are mapped to the |
|
575 |
corresponding boxed types from {@code java.lang}, namely {@code |
|
576 |
Boolean}, {@code Byte}, etc. The Open Type is the corresponding |
|
577 |
{@code SimpleType}. Thus, <em>opentype(</em>{@code |
|
578 |
long}<em>)</em> is {@code SimpleType.LONG}, and |
|
579 |
<em>opendata(</em>{@code long}<em>)</em> is {@code |
|
580 |
java.lang.Long}.</p> |
|
581 |
||
582 |
<p>An array of primitive type such as {@code long[]} can be represented |
|
583 |
directly as an Open Type. Thus, <em>openType(</em>{@code |
|
584 |
long[]}<em>)</em> is {@code |
|
585 |
ArrayType.getPrimitiveArrayType(long[].class)}, and |
|
586 |
<em>opendata(</em>{@code long[]}<em>)</em> is {@code |
|
587 |
long[]}.</p> |
|
588 |
||
589 |
<p>In practice, the difference between a plain {@code int} and {@code |
|
590 |
Integer}, etc, does not show up because operations in the JMX API |
|
591 |
are always on Java objects, not primitives. However, the |
|
592 |
difference <em>does</em> show up with arrays.</p> |
|
593 |
||
594 |
||
595 |
<h3>Mappings for collections ({@code List<}<em>E</em>{@code >} etc)</h3> |
|
596 |
||
597 |
<p>A {@code List<}<em>E</em>{@code >} or {@code |
|
598 |
Set<}<em>E</em>{@code >}, such as {@code List<String>} or {@code |
|
599 |
Set<ObjectName>}, is mapped in the same way as an array of the |
|
600 |
same element type, such as {@code String[]} or {@code |
|
601 |
ObjectName[]}.</p> |
|
602 |
||
603 |
<p>A {@code SortedSet<}<em>E</em>{@code >} is also mapped in the |
|
604 |
same way as an <em>E</em>{@code []}, but it is only convertible if |
|
605 |
<em>E</em> is a class or interface that implements {@link |
|
606 |
java.lang.Comparable}. Thus, a {@code SortedSet<String>} or |
|
607 |
{@code SortedSet<Integer>} is convertible, but a {@code |
|
608 |
SortedSet<int[]>} or {@code SortedSet<List<String>>} is not. The |
|
609 |
conversion of a {@code SortedSet} instance will fail with an |
|
610 |
{@code IllegalArgumentException} if it has a |
|
611 |
non-null {@link java.util.SortedSet#comparator() |
|
612 |
comparator()}.</p> |
|
613 |
||
614 |
<p>A {@code List<}<em>E</em>{@code >} is reconstructed as a |
|
615 |
{@code java.util.ArrayList<}<em>E</em>{@code >}; |
|
616 |
a {@code Set<}<em>E</em>{@code >} as a |
|
617 |
{@code java.util.HashSet<}<em>E</em>{@code >}; |
|
618 |
a {@code SortedSet<}<em>E</em>{@code >} as a |
|
619 |
{@code java.util.TreeSet<}<em>E</em>{@code >}.</p> |
|
620 |
||
621 |
||
622 |
<h3>Mappings for maps ({@code Map<}<em>K</em>,<em>V</em>{@code >} etc)</h3> |
|
623 |
||
624 |
<p>A {@code Map<}<em>K</em>,<em>V</em>{@code >} or {@code |
|
625 |
SortedMap<}<em>K</em>,<em>V</em>{@code >}, for example {@code |
|
626 |
Map<String,ObjectName>}, has Open Type {@link TabularType} and is mapped |
|
627 |
to a {@link TabularData}. |
|
628 |
The {@code TabularType} has two items called {@code key} and |
|
629 |
{@code value}. The Open Type of {@code key} is |
|
630 |
<em>opentype(K)</em>, and the Open Type of {@code value} is |
|
631 |
<em>opentype(V)</em>. The index of the {@code TabularType} is the |
|
632 |
single item {@code key}.</p> |
|
633 |
||
634 |
<p>For example, the {@code TabularType} for a {@code |
|
635 |
Map<String,ObjectName>} might be constructed with code like |
|
636 |
this:</p> |
|
637 |
||
638 |
<pre> |
|
639 |
String typeName = |
|
640 |
"java.util.Map<java.lang.String, javax.management.ObjectName>"; |
|
641 |
String[] keyValue = |
|
642 |
new String[] {"key", "value"}; |
|
643 |
OpenType[] openTypes = |
|
644 |
new OpenType[] {SimpleType.STRING, SimpleType.OBJECTNAME}; |
|
645 |
CompositeType rowType = |
|
646 |
new CompositeType(typeName, typeName, keyValue, keyValue, openTypes); |
|
647 |
TabularType tabularType = |
|
648 |
new TabularType(typeName, typeName, rowType, new String[] {"key"}); |
|
649 |
</pre> |
|
650 |
||
651 |
<p>The {@code typeName} here is determined by the <a href="#type-names"> |
|
652 |
type name rules</a> detailed below. |
|
653 |
||
654 |
<p>A {@code SortedMap<}<em>K</em>,<em>V</em>{@code >} is mapped in the |
|
655 |
same way, but it is only convertible if |
|
656 |
<em>K</em> is a class or interface that implements {@link |
|
657 |
java.lang.Comparable}. Thus, a {@code SortedMap<String,int[]>} |
|
658 |
is convertible, but a |
|
659 |
{@code SortedMap<int[],String>} is not. The conversion of a |
|
660 |
{@code SortedMap} instance will fail with an {@code |
|
661 |
IllegalArgumentException} if it has a non-null {@link |
|
662 |
java.util.SortedMap#comparator() comparator()}.</p> |
|
663 |
||
664 |
<p>A {@code Map<}<em>K</em>,<em>V</em>{@code >} is reconstructed as |
|
665 |
a {@code java.util.HashMap<}<em>K</em>,<em>V</em>{@code >}; |
|
666 |
a {@code SortedMap<}<em>K</em>,<em>V</em>{@code >} as |
|
667 |
a {@code java.util.TreeMap<}<em>K</em>,<em>V</em>{@code >}.</p> |
|
668 |
||
669 |
<p>{@code TabularData} is an interface. The concrete class that is |
|
670 |
used to represent a {@code Map<}<em>K</em>,<em>V</em>{@code >} as |
|
671 |
Open Data is {@link TabularDataSupport}, |
|
672 |
or another class implementing {@code |
|
673 |
TabularData} that serializes as {@code TabularDataSupport}.</p> |
|
674 |
||
675 |
||
687 | 676 |
<h3 id="mxbean-map">Mappings for MXBean interfaces</h3> |
2 | 677 |
|
678 |
<p>An MXBean interface, or a type referenced within an MXBean |
|
679 |
interface, can reference another MXBean interface, <em>J</em>. |
|
680 |
Then <em>opentype(J)</em> is {@code SimpleType.OBJECTNAME} and |
|
681 |
<em>opendata(J)</em> is {@code ObjectName}.</p> |
|
682 |
||
683 |
<p>For example, suppose you have two MXBean interfaces like this:</p> |
|
684 |
||
685 |
<pre> |
|
686 |
public interface ProductMXBean { |
|
687 |
public ModuleMXBean[] getModules(); |
|
688 |
} |
|
689 |
||
690 |
public interface ModuleMXBean { |
|
691 |
public ProductMXBean getProduct(); |
|
692 |
} |
|
693 |
</pre> |
|
694 |
||
695 |
<p>The object implementing the {@code ModuleMXBean} interface |
|
696 |
returns from its {@code getProduct} method an object |
|
697 |
implementing the {@code ProductMXBean} interface. The |
|
698 |
{@code ModuleMXBean} object and the returned {@code |
|
699 |
ProductMXBean} objects must both be registered as MXBeans in the |
|
700 |
same MBean Server.</p> |
|
701 |
||
702 |
<p>The method {@code ModuleMXBean.getProduct()} defines an |
|
703 |
attribute called {@code Product}. The Open Type for this |
|
704 |
attribute is {@code SimpleType.OBJECTNAME}, and the corresponding |
|
705 |
{@code ObjectName} value will be the name under which the |
|
706 |
referenced {@code ProductMXBean} is registered in the MBean |
|
707 |
Server.</p> |
|
708 |
||
709 |
<p>If you make an MXBean proxy for a {@code ModuleMXBean} and |
|
710 |
call its {@code getProduct()} method, the proxy will map the |
|
711 |
{@code ObjectName} back into a {@code ProductMXBean} by making |
|
712 |
another MXBean proxy. More formally, when a proxy made with |
|
713 |
{@link JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, |
|
714 |
Class) |
|
715 |
JMX.newMXBeanProxy(mbeanServerConnection, objectNameX, |
|
716 |
interfaceX)} needs to map {@code objectNameY} back into {@code |
|
717 |
interfaceY}, another MXBean interface, it does so with {@code |
|
718 |
JMX.newMXBeanProxy(mbeanServerConnection, objectNameY, |
|
719 |
interfaceY)}. The implementation may return a proxy that was |
|
720 |
previously created by a call to {@code JMX.newMXBeanProxy} |
|
721 |
with the same parameters, or it may create a new proxy.</p> |
|
722 |
||
723 |
<p>The reverse mapping is illustrated by the following change to the |
|
724 |
{@code ModuleMXBean} interface:</p> |
|
725 |
||
726 |
<pre> |
|
727 |
public interface ModuleMXBean { |
|
728 |
public ProductMXBean getProduct(); |
|
729 |
public void setProduct(ProductMXBean c); |
|
730 |
} |
|
731 |
</pre> |
|
732 |
||
733 |
<p>The presence of the {@code setProduct} method now means that the |
|
734 |
{@code Product} attribute is read/write. As before, the value |
|
735 |
of this attribute is an {@code ObjectName}. When the attribute is |
|
736 |
set, the {@code ObjectName} must be converted into the |
|
737 |
{@code ProductMXBean} object that the {@code setProduct} method |
|
738 |
expects. This object will be an MXBean proxy for the given |
|
739 |
{@code ObjectName} in the same MBean Server.</p> |
|
740 |
||
741 |
<p>If you make an MXBean proxy for a {@code ModuleMXBean} and |
|
742 |
call its {@code setProduct} method, the proxy will map its |
|
743 |
{@code ProductMXBean} argument back into an {@code ObjectName}. |
|
744 |
This will only work if the argument is in fact another proxy, |
|
745 |
for a {@code ProductMXBean} in the same {@code |
|
746 |
MBeanServerConnection}. The proxy can have been returned from |
|
747 |
another proxy (like {@code ModuleMXBean.getProduct()} which |
|
748 |
returns a proxy for a {@code ProductMXBean}); or it can have |
|
749 |
been created by {@link |
|
750 |
JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class) |
|
751 |
JMX.newMXBeanProxy}; or it can have been created using {@link |
|
752 |
java.lang.reflect.Proxy Proxy} with an invocation handler that |
|
753 |
is {@link MBeanServerInvocationHandler} or a subclass.</p> |
|
754 |
||
755 |
<p>If the same MXBean were registered under two different |
|
756 |
{@code ObjectName}s, a reference to that MXBean from another |
|
757 |
MXBean would be ambiguous. Therefore, if an MXBean object is |
|
758 |
already registered in an MBean Server and an attempt is made to |
|
759 |
register it in the same MBean Server under another name, the |
|
760 |
result is an {@link InstanceAlreadyExistsException}. Registering |
|
761 |
the same MBean object under more than one name is discouraged in |
|
762 |
general, notably because it does not work well for MBeans that are |
|
763 |
{@link NotificationBroadcaster}s.</p> |
|
764 |
||
687 | 765 |
<h3 id="composite-map">Mappings for other types</h3> |
2 | 766 |
|
767 |
<p>Given a Java class or interface <em>J</em> that does not match the other |
|
768 |
rules in the table above, the MXBean framework will attempt to map |
|
769 |
it to a {@link CompositeType} as follows. The type name of this |
|
770 |
{@code CompositeType} is determined by the <a href="#type-names"> |
|
771 |
type name rules</a> below.</p> |
|
772 |
||
773 |
<p>The class is examined for getters using the conventions |
|
774 |
<a href="#naming-conv">above</a>. (Getters must be public |
|
775 |
instance methods.) If there are no getters, or if |
|
776 |
any getter has a type that is not convertible, then <em>J</em> is |
|
777 |
not convertible.</p> |
|
778 |
||
779 |
<p>If there is at least one getter and every getter has a |
|
780 |
convertible type, then <em>opentype(J)</em> is a {@code |
|
781 |
CompositeType} with one item for every getter. If the getter is |
|
782 |
||
783 |
<blockquote> |
|
784 |
<code><em>T</em> get<em>Name</em>()</code> |
|
785 |
</blockquote> |
|
786 |
||
787 |
then the item in the {@code CompositeType} is called {@code name} |
|
788 |
and has type <em>opentype(T)</em>. For example, if the item is |
|
789 |
||
790 |
<blockquote> |
|
791 |
<code>String getOwner()</code> |
|
792 |
</blockquote> |
|
793 |
||
794 |
then the item is called {@code owner} and has Open Type {@code |
|
795 |
SimpleType.STRING}. If the getter is |
|
796 |
||
797 |
<blockquote> |
|
798 |
<code>boolean is<em>Name</em>()</code> |
|
799 |
</blockquote> |
|
800 |
||
801 |
then the item in the {@code CompositeType} is called {@code name} |
|
802 |
and has type {@code SimpleType.BOOLEAN}.</p> |
|
803 |
||
804 |
<p>Notice that the first character (or code point) is converted to |
|
805 |
lower case. This follows the Java Beans convention, which for |
|
806 |
historical reasons is different from the Standard MBean |
|
807 |
convention. In a Standard MBean or MXBean interface, a method |
|
808 |
{@code getOwner} defines an attribute called {@code Owner}, while |
|
809 |
in a Java Bean or mapped {@code CompositeType}, a method {@code |
|
810 |
getOwner} defines a property or item called {@code owner}.</p> |
|
811 |
||
812 |
<p>If two methods produce the same item name (for example, {@code |
|
813 |
getOwner} and {@code isOwner}, or {@code getOwner} and {@code |
|
814 |
getowner}) then the type is not convertible.</p> |
|
815 |
||
816 |
<p>When the Open Type is {@code CompositeType}, the corresponding |
|
817 |
mapped Java type (<em>opendata(J)</em>) is {@link |
|
818 |
CompositeData}. The mapping from an instance of <em>J</em> to a |
|
819 |
{@code CompositeData} corresponding to the {@code CompositeType} |
|
820 |
just described is done as follows. First, if <em>J</em> |
|
821 |
implements the interface {@link CompositeDataView}, then that |
|
822 |
interface's {@link CompositeDataView#toCompositeData |
|
823 |
toCompositeData} method is called to do the conversion. |
|
824 |
Otherwise, the {@code CompositeData} is constructed by calling |
|
825 |
the getter for each item and converting it to the corresponding |
|
826 |
Open Data type. Thus, a getter such as</p> |
|
827 |
||
828 |
<blockquote> |
|
829 |
{@code List<String> getNames()} |
|
830 |
</blockquote> |
|
831 |
||
832 |
<p>will have been mapped to an item with name "{@code names}" and |
|
833 |
Open Type {@code ArrayType(1, SimpleType.STRING)}. The conversion |
|
834 |
to {@code CompositeData} will call {@code getNames()} and convert |
|
835 |
the resultant {@code List<String>} into a {@code String[]} for the |
|
836 |
item "{@code names}".</p> |
|
837 |
||
838 |
<p>{@code CompositeData} is an interface. The concrete class that is |
|
839 |
used to represent a type as Open Data is {@link |
|
840 |
CompositeDataSupport}, or another class implementing {@code |
|
841 |
CompositeData} that serializes as {@code |
|
842 |
CompositeDataSupport}.</p> |
|
843 |
||
844 |
||
845 |
<h4>Reconstructing an instance of Java type <em>J</em> from |
|
846 |
a {@code CompositeData}</h4> |
|
847 |
||
848 |
<p>If <em>opendata(J)</em> is {@code CompositeData} for a Java type |
|
849 |
<em>J</em>, then either an instance of <em>J</em> can be |
|
850 |
reconstructed from a {@code CompositeData}, or <em>J</em> is not |
|
851 |
reconstructible. If any item in the {@code CompositeData} is not |
|
852 |
reconstructible, then <em>J</em> is not reconstructible either.</p> |
|
853 |
||
854 |
<p>For any given <em>J</em>, the following rules are consulted to |
|
855 |
determine how to reconstruct instances of <em>J</em> from |
|
856 |
{@code CompositeData}. The first applicable rule in the list is |
|
857 |
the one that will be used.</p> |
|
858 |
||
859 |
<ol> |
|
860 |
||
861 |
<li><p>If <em>J</em> has a method<br> |
|
862 |
{@code public static }<em>J </em>{@code from(CompositeData cd)}<br> |
|
863 |
then that method is called to reconstruct an instance of |
|
864 |
<em>J</em>.</p></li> |
|
865 |
||
866 |
<li><p>Otherwise, if <em>J</em> has at least one public |
|
1004 | 867 |
constructor with a {@link java.beans.ConstructorProperties |
868 |
ConstructorProperties} annotation, then one |
|
2 | 869 |
of those constructors (not necessarily always the same one) |
870 |
will be called to reconstruct an instance of <em>J</em>. |
|
871 |
Every such annotation must list as many strings as the |
|
872 |
constructor has parameters; each string must name a property |
|
873 |
corresponding to a getter of <em>J</em>; and the type of this |
|
874 |
getter must be the same as the corresponding constructor |
|
875 |
parameter. It is not an error for there to be getters that |
|
876 |
are not mentioned in the {@code ConstructorProperties} annotation |
|
877 |
(these may correspond to information that is not needed to |
|
878 |
reconstruct the object).</p> |
|
879 |
||
880 |
<p>An instance of <em>J</em> is reconstructed by calling a |
|
881 |
constructor with the appropriate reconstructed items from the |
|
882 |
{@code CompositeData}. The constructor to be called will be |
|
883 |
determined at runtime based on the items actually present in |
|
884 |
the {@code CompositeData}, given that this {@code |
|
885 |
CompositeData} might come from an earlier version of |
|
886 |
<em>J</em> where not all the items were present. A |
|
887 |
constructor is <em>applicable</em> if all the properties named |
|
888 |
in its {@code ConstructorProperties} annotation are present as items |
|
889 |
in the {@code CompositeData}. If no constructor is |
|
890 |
applicable, then the attempt to reconstruct <em>J</em> fails.</p> |
|
891 |
||
892 |
<p>For any possible combination of properties, it must be the |
|
893 |
case that either (a) there are no applicable constructors, or |
|
894 |
(b) there is exactly one applicable constructor, or (c) one of |
|
895 |
the applicable constructors names a proper superset of the |
|
896 |
properties named by each other applicable constructor. (In |
|
897 |
other words, there should never be ambiguity over which |
|
898 |
constructor to choose.) If this condition is not true, then |
|
899 |
<em>J</em> is not reconstructible.</p></li> |
|
900 |
||
901 |
<li><p>Otherwise, if <em>J</em> has a public no-arg constructor, and |
|
902 |
for every getter in <em>J</em> with type |
|
903 |
<em>T</em> and name <em>N</em> there is a corresponding setter |
|
904 |
with the same name and type, then an instance of <em>J</em> is |
|
905 |
constructed with the no-arg constructor and the setters are |
|
906 |
called with the reconstructed items from the {@code CompositeData} |
|
907 |
to restore the values. For example, if there is a method<br> |
|
908 |
{@code public List<String> getNames()}<br> |
|
909 |
then there must also be a method<br> |
|
910 |
{@code public void setNames(List<String> names)}<br> |
|
911 |
for this rule to apply.</p> |
|
912 |
||
913 |
<p>If the {@code CompositeData} came from an earlier version of |
|
914 |
<em>J</em>, some items might not be present. In this case, |
|
915 |
the corresponding setters will not be called.</p></li> |
|
916 |
||
917 |
<li><p>Otherwise, if <em>J</em> is an interface that has no methods |
|
918 |
other than getters, an instance of <em>J</em> is constructed |
|
919 |
using a {@link java.lang.reflect.Proxy} with a {@link |
|
920 |
CompositeDataInvocationHandler} backed by the {@code |
|
921 |
CompositeData} being converted.</p></li> |
|
922 |
||
923 |
<li><p>Otherwise, <em>J</em> is not reconstructible.</p></li> |
|
924 |
</ol> |
|
925 |
||
926 |
<p>Here are examples showing different ways to code a type {@code |
|
927 |
NamedNumber} that consists of an {@code int} and a {@code |
|
928 |
String}. In each case, the {@code CompositeType} looks like this:</p> |
|
929 |
||
930 |
<blockquote> |
|
931 |
<pre> |
|
932 |
{@link CompositeType}( |
|
933 |
"NamedNumber", // typeName |
|
934 |
"NamedNumber", // description |
|
935 |
new String[] {"number", "name"}, // itemNames |
|
936 |
new String[] {"number", "name"}, // itemDescriptions |
|
937 |
new OpenType[] {SimpleType.INTEGER, |
|
938 |
SimpleType.STRING} // itemTypes |
|
939 |
); |
|
940 |
</pre> |
|
941 |
</blockquote> |
|
942 |
||
943 |
<ol> |
|
944 |
<li>Static {@code from} method: |
|
945 |
||
946 |
<blockquote> |
|
947 |
<pre> |
|
948 |
public class NamedNumber { |
|
949 |
public int getNumber() {return number;} |
|
950 |
public String getName() {return name;} |
|
951 |
private NamedNumber(int number, String name) { |
|
952 |
this.number = number; |
|
953 |
this.name = name; |
|
954 |
} |
|
955 |
<b>public static NamedNumber from(CompositeData cd)</b> { |
|
956 |
return new NamedNumber((Integer) cd.get("number"), |
|
957 |
(String) cd.get("name")); |
|
958 |
} |
|
959 |
private final int number; |
|
960 |
private final String name; |
|
961 |
} |
|
962 |
</pre> |
|
963 |
</blockquote> |
|
964 |
</li> |
|
965 |
||
966 |
<li>Public constructor with <code>@ConstructorProperties</code> annotation: |
|
967 |
||
968 |
<blockquote> |
|
969 |
<pre> |
|
970 |
public class NamedNumber { |
|
971 |
public int getNumber() {return number;} |
|
972 |
public String getName() {return name;} |
|
973 |
<b>@ConstructorProperties({"number", "name"}) |
|
974 |
public NamedNumber(int number, String name)</b> { |
|
975 |
this.number = number; |
|
976 |
this.name = name; |
|
977 |
} |
|
978 |
private final int number; |
|
979 |
private final String name; |
|
980 |
} |
|
981 |
</pre> |
|
982 |
</blockquote> |
|
983 |
</li> |
|
984 |
||
985 |
<li>Setter for every getter: |
|
986 |
||
987 |
<blockquote> |
|
988 |
<pre> |
|
989 |
public class NamedNumber { |
|
990 |
public int getNumber() {return number;} |
|
991 |
public void <b>setNumber</b>(int number) {this.number = number;} |
|
992 |
public String getName() {return name;} |
|
993 |
public void <b>setName</b>(String name) {this.name = name;} |
|
994 |
<b>public NamedNumber()</b> {} |
|
995 |
private int number; |
|
996 |
private String name; |
|
997 |
} |
|
998 |
</pre> |
|
999 |
</blockquote> |
|
1000 |
</li> |
|
1001 |
||
1002 |
<li>Interface with only getters: |
|
1003 |
||
1004 |
<blockquote> |
|
1005 |
<pre> |
|
1006 |
public interface NamedNumber { |
|
1007 |
public int getNumber(); |
|
1008 |
public String getName(); |
|
1009 |
} |
|
1010 |
</pre> |
|
1011 |
</blockquote> |
|
1012 |
</li> |
|
1013 |
</ol> |
|
1014 |
||
1015 |
<p>It is usually better for classes that simply represent a |
|
1016 |
collection of data to be <em>immutable</em>. An instance of an |
|
1017 |
immutable class cannot be changed after it has been constructed. |
|
1018 |
Notice that {@code CompositeData} itself is immutable. |
|
1019 |
Immutability has many advantages, notably with regard to |
|
1020 |
thread-safety and security. So the approach using setters should |
|
1021 |
generally be avoided if possible.</p> |
|
1022 |
||
1023 |
||
1024 |
<h3>Recursive types</h3> |
|
1025 |
||
1026 |
<p>Recursive (self-referential) types cannot be used in MXBean |
|
1027 |
interfaces. This is a consequence of the immutability of {@link |
|
1028 |
CompositeType}. For example, the following type could not be the |
|
1029 |
type of an attribute, because it refers to itself:</p> |
|
1030 |
||
1031 |
<pre> |
|
1032 |
public interface <b>Node</b> { |
|
1033 |
public String getName(); |
|
1034 |
public int getPriority(); |
|
1035 |
public <b>Node</b> getNext(); |
|
1036 |
} |
|
1037 |
</pre> |
|
1038 |
||
1039 |
<p>It is always possible to rewrite recursive types like this so |
|
1040 |
they are no longer recursive. Doing so may require introducing |
|
1041 |
new types. For example:</p> |
|
1042 |
||
1043 |
<pre> |
|
1044 |
public interface <b>NodeList</b> { |
|
1045 |
public List<Node> getNodes(); |
|
1046 |
} |
|
1047 |
||
1048 |
public interface Node { |
|
1049 |
public String getName(); |
|
1050 |
public int getPriority(); |
|
1051 |
} |
|
1052 |
</pre> |
|
1053 |
||
687 | 1054 |
<p>Alternatively, you can define a custom mapping for your recursive |
1055 |
type; see the next section.</p> |
|
1056 |
||
1057 |
<h3 id="custom">Custom MXBean type mappings</h3> |
|
1058 |
||
1059 |
<p>You can augment or replace the default type mappings described |
|
1060 |
above with custom mappings. An example appears in the |
|
1061 |
documentation for {@link MXBeanMapping}.</p> |
|
1062 |
||
1063 |
<p>If an MXBean uses custom mappings, then an MXBean proxy for |
|
1064 |
that MXBean must use the same mappings for correct behavior. |
|
1065 |
This requires more careful synchronization between client and |
|
1066 |
server than is necessary with the default mappings. For example |
|
1067 |
it typically requires the client to have the same implementation |
|
1068 |
of any {@link MXBeanMapping} subclasses as the server. For this |
|
1069 |
reason, custom mappings should be avoided if possible.</p> |
|
1070 |
||
1071 |
<p>Every MXBean has an associated {@link MXBeanMappingFactory}. |
|
1072 |
Call this <code><em>f</em></code>. Then every type that appears |
|
1073 |
in that MXBean has an associated {@link MXBeanMapping} |
|
1074 |
determined by <code><em>f</em></code>. If the type is |
|
1075 |
<code><em>J</em></code>, say, then the mapping is {@link |
|
1076 |
MXBeanMappingFactory#mappingForType |
|
1077 |
<em>f</em>.mappingForType}<code>(<em>J</em>, |
|
1078 |
<em>f</em>)</code>.</p> |
|
1079 |
||
1080 |
<p>The {@code MXBeanMappingFactory} <code><em>f</em></code> for an |
|
1081 |
MXBean is determined as follows.</p> |
|
1082 |
||
1083 |
<ul> |
|
1011
2cc4873fa29f
6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents:
1004
diff
changeset
|
1084 |
<li><p>If a {@link JMX.MBeanOptions} argument is supplied to |
687 | 1085 |
the {@link StandardMBean} constructor that makes an MXBean, |
1011
2cc4873fa29f
6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents:
1004
diff
changeset
|
1086 |
or to the {@link JMX#newMBeanProxy(MBeanServerConnection, |
2cc4873fa29f
6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents:
1004
diff
changeset
|
1087 |
ObjectName, Class, JMX.MBeanOptions) JMX.newMBeanProxy} |
687 | 1088 |
method, and the {@code MBeanOptions} object defines a non-null |
1089 |
{@code MXBeanMappingFactory}, then that is the value of |
|
1090 |
<code><em>f</em></code>.</p></li> |
|
1091 |
||
1092 |
<li><p>Otherwise, if the MXBean interface has an {@link |
|
1093 |
MXBeanMappingFactoryClass} annotation, then that annotation |
|
1094 |
must identify a subclass of {@code MXBeanMappingFactory} |
|
1095 |
with a no-argument constructor. Then |
|
1096 |
<code><em>f</em></code> is the result of calling this |
|
1097 |
constructor. If the class does not have a no-argument |
|
1098 |
constructor, or if calling the constructor produces an |
|
1099 |
exception, then the MXBean is invalid and an attempt to |
|
1100 |
register it in the MBean Server will produce a {@link |
|
1101 |
NotCompliantMBeanException}.</p> |
|
1102 |
||
1103 |
<p>This annotation is not inherited from any parent |
|
1104 |
interfaces. If an MXBean interface has this annotation, |
|
1105 |
then usually any MXBean subinterfaces must repeat the same |
|
1106 |
annotation for correct behavior.</p></li> |
|
1107 |
||
1108 |
<li><p>Otherwise, if the package in which the MXBean interface |
|
1109 |
appears has an {@code MXBeanMappingFactoryClass} annotation, |
|
1110 |
then <code><em>f</em></code> is determined as if that |
|
1111 |
annotation appeared on the MXBean interface.</p></li> |
|
1112 |
||
1113 |
<li><p>Otherwise, <code><em>f</em></code> is the default mapping |
|
1114 |
factory, {@link MXBeanMappingFactory#DEFAULT}.</p></li> |
|
1115 |
</ul> |
|
1116 |
||
1117 |
<p>The default mapping factory recognizes the {@link |
|
1118 |
MXBeanMappingClass} annotation on a class or interface. If |
|
1119 |
<code><em>J</em></code> is a class or interface that has such an |
|
1120 |
annotation, then the {@code MXBeanMapping} for |
|
1121 |
<code><em>J</em></code> produced by the default mapping factory |
|
1122 |
will be determined by the value of the annotation as described |
|
1123 |
in its {@linkplain MXBeanMappingClass documentation}.</p> |
|
1124 |
||
2 | 1125 |
<h3>MBeanInfo contents for an MXBean</h3> |
1126 |
||
1127 |
<p>An MXBean is a type of Open MBean. However, for compatibility |
|
1128 |
reasons, its {@link MBeanInfo} is not an {@link OpenMBeanInfo}. |
|
1129 |
In particular, when the type of an attribute, parameter, or |
|
1130 |
operation return value is a primitive type such as {@code int}, |
|
1131 |
or is {@code void} (for a return type), then the attribute, |
|
1132 |
parameter, or operation will be represented respectively by an |
|
1133 |
{@link MBeanAttributeInfo}, {@link MBeanParameterInfo}, or |
|
1134 |
{@link MBeanOperationInfo} whose {@code getType()} or {@code |
|
1135 |
getReturnType()} returns the primitive name ("{@code int}" etc). |
|
1136 |
This is so even though the mapping rules above specify that the |
|
1137 |
<em>opendata</em> mapping is the wrapped type ({@code Integer} |
|
1138 |
etc).</p> |
|
1139 |
||
1140 |
<p>The array of public constructors returned by {@link |
|
1141 |
MBeanInfo#getConstructors()} for an MXBean that is directly |
|
1142 |
registered in the MBean Server will contain all of the public |
|
1143 |
constructors of that MXBean. If the class of the MXBean is not |
|
1144 |
public then its constructors are not considered public either. |
|
1145 |
The list returned for an MXBean that is constructed using the |
|
1146 |
{@link StandardMBean} class is derived in the same way as for |
|
1147 |
Standard MBeans. Regardless of how the MXBean was constructed, |
|
1148 |
its constructor parameters are not subject to MXBean mapping |
|
1149 |
rules and do not have a corresponding {@code OpenType}.</p> |
|
1150 |
||
1151 |
<p>The array of notification types returned by {@link |
|
1152 |
MBeanInfo#getNotifications()} for an MXBean that is directly |
|
1153 |
registered in the MBean Server will be empty if the MXBean does |
|
1154 |
not implement the {@link NotificationBroadcaster} interface. |
|
1155 |
Otherwise, it will be the result of calling {@link |
|
1156 |
NotificationBroadcaster#getNotificationInfo()} at the time the MXBean |
|
1157 |
was registered. Even if the result of this method changes |
|
1158 |
subsequently, the result of {@code MBeanInfo.getNotifications()} |
|
1159 |
will not. The list returned for an MXBean that is constructed |
|
1160 |
using the {@link StandardMBean} or {@link StandardEmitterMBean} |
|
1161 |
class is derived in the same way as for Standard MBeans.</p> |
|
1162 |
||
1163 |
<p>The {@link Descriptor} for all of the |
|
1164 |
{@code MBeanAttributeInfo}, {@code MBeanParameterInfo}, and |
|
1165 |
{@code MBeanOperationInfo} objects contained in the {@code MBeanInfo} |
|
1166 |
will have a field {@code openType} whose value is the {@link OpenType} |
|
1167 |
specified by the mapping rules above. So even when {@code getType()} |
|
1168 |
is "{@code int}", {@code getDescriptor().getField("openType")} will |
|
1169 |
be {@link SimpleType#INTEGER}.</p> |
|
1170 |
||
1171 |
<p>The {@code Descriptor} for each of these objects will also have a |
|
1172 |
field {@code originalType} that is a string representing the Java type |
|
1173 |
that appeared in the MXBean interface. The format of this string |
|
1174 |
is described in the section <a href="#type-names">Type Names</a> |
|
1175 |
below.</p> |
|
1176 |
||
1177 |
<p>The {@code Descriptor} for the {@code MBeanInfo} will have a field |
|
1178 |
{@code mxbean} whose value is the string "{@code true}".</p> |
|
1179 |
||
1180 |
||
687 | 1181 |
<h3 id="type-names">Type Names</h3> |
2 | 1182 |
|
1183 |
<p>Sometimes the unmapped type <em>T</em> of a method parameter or |
|
1184 |
return value in an MXBean must be represented as a string. If |
|
1185 |
<em>T</em> is a non-generic type, this string is the value |
|
1186 |
returned by {@link Class#getName()}. Otherwise it is the value of |
|
1187 |
<em>genericstring(T)</em>, defined as follows: |
|
1188 |
||
1189 |
<ul> |
|
1190 |
||
1191 |
<li>If <em>T</em> is a non-generic non-array type, |
|
1192 |
<em>genericstring(T)</em> is the value returned by {@link |
|
1193 |
Class#getName()}, for example {@code "int"} or {@code |
|
1194 |
"java.lang.String"}. |
|
1195 |
||
1196 |
<li>If <em>T</em> is an array <em>E[]</em>, |
|
1197 |
<em>genericstring(T)</em> is <em>genericstring(E)</em> followed |
|
1198 |
by {@code "[]"}. For example, <em>genericstring({@code int[]})</em> |
|
1199 |
is {@code "int[]"}, and <em>genericstring({@code |
|
1200 |
List<String>[][]})</em> is {@code |
|
1201 |
"java.util.List<java.lang.String>[][]"}. |
|
1202 |
||
1203 |
<li>Otherwise, <em>T</em> is a parameterized type such as {@code |
|
1204 |
List<String>} and <em>genericstring(T)</em> consists of the |
|
1205 |
following: the fully-qualified name of the parameterized type as |
|
1206 |
returned by {@code Class.getName()}; a left angle bracket ({@code |
|
1207 |
"<"}); <em>genericstring(A)</em> where <em>A</em> is the first |
|
1208 |
type parameter; if there is a second type parameter <em>B</em> |
|
1209 |
then {@code ", "} (a comma and a single space) followed by |
|
1210 |
<em>genericstring(B)</em>; a right angle bracket ({@code ">"}). |
|
1211 |
||
1212 |
</ul> |
|
1213 |
||
1214 |
<p>Note that if a method returns {@code int[]}, this will be |
|
1215 |
represented by the string {@code "[I"} returned by {@code |
|
1216 |
Class.getName()}, but if a method returns {@code List<int[]>}, |
|
1217 |
this will be represented by the string {@code |
|
1218 |
"java.util.List<int[]>"}. |
|
1219 |
||
1220 |
<h3>Exceptions</h3> |
|
1221 |
||
1222 |
<p>A problem with mapping <em>from</em> Java types <em>to</em> |
|
1223 |
Open types is signaled with an {@link OpenDataException}. This |
|
1224 |
can happen when an MXBean interface is being analyzed, for |
|
1225 |
example if it references a type like {@link java.util.Random |
|
1226 |
java.util.Random} that has no getters. Or it can happen when an |
|
1227 |
instance is being converted (a return value from a method in an |
|
1228 |
MXBean or a parameter to a method in an MXBean proxy), for |
|
1229 |
example when converting from {@code SortedSet<String>} to {@code |
|
1230 |
String[]} if the {@code SortedSet} has a non-null {@code |
|
1231 |
Comparator}.</p> |
|
1232 |
||
1233 |
<p>A problem with mapping <em>to</em> Java types <em>from</em> |
|
1234 |
Open types is signaled with an {@link InvalidObjectException}. |
|
1235 |
This can happen when an MXBean interface is being analyzed, for |
|
1236 |
example if it references a type that is not |
|
1237 |
<em>reconstructible</em> according to the rules above, in a |
|
1238 |
context where a reconstructible type is required. Or it can |
|
1239 |
happen when an instance is being converted (a parameter to a |
|
1240 |
method in an MXBean or a return value from a method in an MXBean |
|
1241 |
proxy), for example from a String to an Enum if there is no Enum |
|
1242 |
constant with that name.</p> |
|
1243 |
||
1244 |
<p>Depending on the context, the {@code OpenDataException} or |
|
1245 |
{@code InvalidObjectException} may be wrapped in another |
|
1246 |
exception such as {@link RuntimeMBeanException} or {@link |
|
1247 |
UndeclaredThrowableException}. For every thrown exception, |
|
1248 |
the condition <em>C</em> will be true: "<em>e</em> is {@code |
|
1249 |
OpenDataException} or {@code InvalidObjectException} (as |
|
1250 |
appropriate), or <em>C</em> is true of <em>e</em>.{@link |
|
1251 |
Throwable#getCause() getCause()}".</p> |
|
1252 |
||
687 | 1253 |
@see MXBeanMapping |
1254 |
||
2 | 1255 |
@since 1.6 |
1256 |
*/ |
|
1257 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1258 |
/* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1259 |
* This annotation is @Inherited because if an MXBean is defined as a |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1260 |
* class using annotations, then its subclasses are also MXBeans. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1261 |
* For example: |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1262 |
* @MXBean |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1263 |
* public class Super { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1264 |
* @ManagedAttribute |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1265 |
* public String getName() {...} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1266 |
* } |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1267 |
* public class Sub extends Super {} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1268 |
* Here Sub is an MXBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1269 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1270 |
* The @Inherited annotation has no effect when applied to an interface. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1271 |
*/ |
2 | 1272 |
@Documented |
1273 |
@Retention(RetentionPolicy.RUNTIME) |
|
1274 |
@Target(ElementType.TYPE) |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1275 |
@Inherited |
2 | 1276 |
public @interface MXBean { |
1277 |
/** |
|
1278 |
True if the annotated interface is an MXBean interface. |
|
1279 |
@return true if the annotated interface is an MXBean interface. |
|
1280 |
*/ |
|
1281 |
boolean value() default true; |
|
1282 |
} |