author | emcmanus |
Mon, 27 Oct 2008 14:02:40 +0100 | |
changeset 1510 | e747d3193ef2 |
parent 834 | dc74d4ddc28e |
child 1514 | 7d443908a97d |
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 com.sun.jmx.mbeanserver.Introspector; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
29 |
import com.sun.jmx.mbeanserver.MBeanInjector; |
687 | 30 |
import com.sun.jmx.remote.util.ClassLogger; |
31 |
import java.beans.BeanInfo; |
|
32 |
import java.beans.PropertyDescriptor; |
|
33 |
import java.io.Serializable; |
|
2 | 34 |
import java.lang.reflect.InvocationHandler; |
687 | 35 |
import java.lang.reflect.InvocationTargetException; |
36 |
import java.lang.reflect.Method; |
|
2 | 37 |
import java.lang.reflect.Proxy; |
687 | 38 |
import java.util.Map; |
39 |
import java.util.TreeMap; |
|
40 |
import javax.management.openmbean.MXBeanMappingFactory; |
|
2 | 41 |
|
42 |
/** |
|
43 |
* Static methods from the JMX API. There are no instances of this class. |
|
44 |
* |
|
45 |
* @since 1.6 |
|
46 |
*/ |
|
47 |
public class JMX { |
|
48 |
/* Code within this package can prove that by providing this instance of |
|
49 |
* this class. |
|
50 |
*/ |
|
51 |
static final JMX proof = new JMX(); |
|
687 | 52 |
private static final ClassLogger logger = |
53 |
new ClassLogger("javax.management.misc", "JMX"); |
|
2 | 54 |
|
55 |
private JMX() {} |
|
56 |
||
57 |
/** |
|
58 |
* The name of the <a href="Descriptor.html#defaultValue">{@code |
|
59 |
* defaultValue}</a> field. |
|
60 |
*/ |
|
61 |
public static final String DEFAULT_VALUE_FIELD = "defaultValue"; |
|
62 |
||
63 |
/** |
|
64 |
* The name of the <a href="Descriptor.html#immutableInfo">{@code |
|
65 |
* immutableInfo}</a> field. |
|
66 |
*/ |
|
67 |
public static final String IMMUTABLE_INFO_FIELD = "immutableInfo"; |
|
68 |
||
69 |
/** |
|
70 |
* The name of the <a href="Descriptor.html#interfaceClassName">{@code |
|
71 |
* interfaceClassName}</a> field. |
|
72 |
*/ |
|
73 |
public static final String INTERFACE_CLASS_NAME_FIELD = "interfaceClassName"; |
|
74 |
||
75 |
/** |
|
76 |
* The name of the <a href="Descriptor.html#legalValues">{@code |
|
77 |
* legalValues}</a> field. |
|
78 |
*/ |
|
79 |
public static final String LEGAL_VALUES_FIELD = "legalValues"; |
|
80 |
||
81 |
/** |
|
82 |
* The name of the <a href="Descriptor.html#maxValue">{@code |
|
83 |
* maxValue}</a> field. |
|
84 |
*/ |
|
85 |
public static final String MAX_VALUE_FIELD = "maxValue"; |
|
86 |
||
87 |
/** |
|
88 |
* The name of the <a href="Descriptor.html#minValue">{@code |
|
89 |
* minValue}</a> field. |
|
90 |
*/ |
|
91 |
public static final String MIN_VALUE_FIELD = "minValue"; |
|
92 |
||
93 |
/** |
|
94 |
* The name of the <a href="Descriptor.html#mxbean">{@code |
|
95 |
* mxbean}</a> field. |
|
96 |
*/ |
|
97 |
public static final String MXBEAN_FIELD = "mxbean"; |
|
98 |
||
99 |
/** |
|
687 | 100 |
* The name of the |
101 |
* <a href="Descriptor.html#mxbeanMappingFactoryClass">{@code |
|
102 |
* mxbeanMappingFactoryClass}</a> field. |
|
103 |
*/ |
|
104 |
public static final String MXBEAN_MAPPING_FACTORY_CLASS_FIELD = |
|
105 |
"mxbeanMappingFactoryClass"; |
|
106 |
||
107 |
/** |
|
2 | 108 |
* The name of the <a href="Descriptor.html#openType">{@code |
109 |
* openType}</a> field. |
|
110 |
*/ |
|
111 |
public static final String OPEN_TYPE_FIELD = "openType"; |
|
112 |
||
113 |
/** |
|
114 |
* The name of the <a href="Descriptor.html#originalType">{@code |
|
115 |
* originalType}</a> field. |
|
116 |
*/ |
|
117 |
public static final String ORIGINAL_TYPE_FIELD = "originalType"; |
|
118 |
||
119 |
/** |
|
687 | 120 |
* <p>Options to apply to an MBean proxy or to an instance of {@link |
121 |
* StandardMBean}.</p> |
|
122 |
* |
|
123 |
* <p>For example, to specify a custom {@link MXBeanMappingFactory} |
|
124 |
* for a {@code StandardMBean}, you might write this:</p> |
|
125 |
* |
|
126 |
* <pre> |
|
127 |
* MXBeanMappingFactory factory = new MyMXBeanMappingFactory(); |
|
128 |
* JMX.MBeanOptions opts = new JMX.MBeanOptions(); |
|
129 |
* opts.setMXBeanMappingFactory(factory); |
|
130 |
* StandardMBean mbean = new StandardMBean(impl, intf, opts); |
|
131 |
* </pre> |
|
132 |
* |
|
133 |
* @see javax.management.JMX.ProxyOptions |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
134 |
* @see javax.management.StandardMBean.Options |
687 | 135 |
*/ |
136 |
public static class MBeanOptions implements Serializable, Cloneable { |
|
137 |
private static final long serialVersionUID = -6380842449318177843L; |
|
138 |
||
139 |
static final MBeanOptions MXBEAN = new MBeanOptions(); |
|
140 |
static { |
|
141 |
MXBEAN.setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT); |
|
142 |
} |
|
143 |
||
144 |
private MXBeanMappingFactory mappingFactory; |
|
145 |
||
146 |
/** |
|
147 |
* <p>Construct an {@code MBeanOptions} object where all options have |
|
148 |
* their default values.</p> |
|
149 |
*/ |
|
150 |
public MBeanOptions() {} |
|
151 |
||
152 |
@Override |
|
153 |
public MBeanOptions clone() { |
|
154 |
try { |
|
155 |
return (MBeanOptions) super.clone(); |
|
156 |
} catch (CloneNotSupportedException e) { |
|
157 |
throw new AssertionError(e); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* <p>True if this is an MXBean proxy or a StandardMBean instance |
|
163 |
* that is an MXBean. The default value is false.</p> |
|
164 |
* |
|
165 |
* <p>This method is equivalent to {@link #getMXBeanMappingFactory() |
|
166 |
* this.getMXBeanMappingFactory()}{@code != null}.</p> |
|
167 |
* |
|
168 |
* @return true if this is an MXBean proxy or a StandardMBean instance |
|
169 |
* that is an MXBean. |
|
170 |
*/ |
|
171 |
public boolean isMXBean() { |
|
172 |
return (this.mappingFactory != null); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* <p>The mappings between Java types and Open Types to be used in |
|
177 |
* an MXBean proxy or a StandardMBean instance that is an MXBean, |
|
178 |
* or null if this instance is not for an MXBean. |
|
179 |
* The default value is null.</p> |
|
180 |
* |
|
181 |
* @return the mappings to be used in this proxy or StandardMBean, |
|
182 |
* or null if this instance is not for an MXBean. |
|
183 |
*/ |
|
184 |
public MXBeanMappingFactory getMXBeanMappingFactory() { |
|
185 |
return mappingFactory; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* <p>Set the {@link #getMXBeanMappingFactory() MXBeanMappingFactory} to |
|
190 |
* the given value. The value should be null if this instance is not |
|
191 |
* for an MXBean. If this instance is for an MXBean, the value should |
|
192 |
* usually be either a custom mapping factory, or |
|
193 |
* {@link MXBeanMappingFactory#forInterface |
|
194 |
* MXBeanMappingFactory.forInterface}{@code (mxbeanInterface)} |
|
195 |
* which signifies |
|
196 |
* that the {@linkplain MXBeanMappingFactory#DEFAULT default} mapping |
|
197 |
* factory should be used unless an {@code @}{@link |
|
198 |
* javax.management.openmbean.MXBeanMappingFactoryClass |
|
199 |
* MXBeanMappingFactoryClass} annotation on {@code mxbeanInterface} |
|
200 |
* specifies otherwise.</p> |
|
201 |
* |
|
202 |
* <p>Examples:</p> |
|
203 |
* <pre> |
|
204 |
* MBeanOptions opts = new MBeanOptions(); |
|
205 |
* opts.setMXBeanMappingFactory(myMappingFactory); |
|
206 |
* MyMXBean proxy = JMX.newMBeanProxy( |
|
207 |
* mbeanServerConnection, objectName, MyMXBean.class, opts); |
|
208 |
* |
|
209 |
* // ...or... |
|
210 |
* |
|
211 |
* MBeanOptions opts = new MBeanOptions(); |
|
212 |
* MXBeanMappingFactory defaultFactoryForMyMXBean = |
|
213 |
* MXBeanMappingFactory.forInterface(MyMXBean.class); |
|
214 |
* opts.setMXBeanMappingFactory(defaultFactoryForMyMXBean); |
|
215 |
* MyMXBean proxy = JMX.newMBeanProxy( |
|
216 |
* mbeanServerConnection, objectName, MyMXBean.class, opts); |
|
217 |
* </pre> |
|
218 |
* |
|
219 |
* @param f the new value. If null, this instance is not for an |
|
220 |
* MXBean. |
|
221 |
*/ |
|
222 |
public void setMXBeanMappingFactory(MXBeanMappingFactory f) { |
|
223 |
this.mappingFactory = f; |
|
224 |
} |
|
225 |
||
226 |
/* To maximise object sharing, classes in this package can replace |
|
227 |
* a private MBeanOptions with no MXBeanMappingFactory with one |
|
228 |
* of these shared instances. But they must be EXTREMELY careful |
|
229 |
* never to give out the shared instances to user code, which could |
|
230 |
* modify them. |
|
231 |
*/ |
|
232 |
private static final MBeanOptions[] CANONICALS = { |
|
233 |
new MBeanOptions(), MXBEAN, |
|
234 |
}; |
|
235 |
// Overridden in local subclasses: |
|
236 |
MBeanOptions[] canonicals() { |
|
237 |
return CANONICALS; |
|
238 |
} |
|
239 |
||
240 |
// This is only used by the logic for canonical instances. |
|
241 |
// Overridden in local subclasses: |
|
242 |
boolean same(MBeanOptions opt) { |
|
243 |
return (opt.mappingFactory == mappingFactory); |
|
244 |
} |
|
245 |
||
246 |
final MBeanOptions canonical() { |
|
247 |
for (MBeanOptions opt : canonicals()) { |
|
248 |
if (opt.getClass() == this.getClass() && same(opt)) |
|
249 |
return opt; |
|
250 |
} |
|
251 |
return this; |
|
252 |
} |
|
253 |
||
254 |
final MBeanOptions uncanonical() { |
|
255 |
for (MBeanOptions opt : canonicals()) { |
|
256 |
if (this == opt) |
|
257 |
return clone(); |
|
258 |
} |
|
259 |
return this; |
|
260 |
} |
|
261 |
||
262 |
private Map<String, Object> toMap() { |
|
263 |
Map<String, Object> map = new TreeMap<String, Object>(); |
|
264 |
try { |
|
265 |
BeanInfo bi = java.beans.Introspector.getBeanInfo(getClass()); |
|
266 |
PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
|
267 |
for (PropertyDescriptor pd : pds) { |
|
268 |
String name = pd.getName(); |
|
269 |
if (name.equals("class")) |
|
270 |
continue; |
|
271 |
Method get = pd.getReadMethod(); |
|
272 |
if (get != null) |
|
273 |
map.put(name, get.invoke(this)); |
|
274 |
} |
|
275 |
} catch (Exception e) { |
|
276 |
Throwable t = e; |
|
277 |
if (t instanceof InvocationTargetException) |
|
278 |
t = t.getCause(); |
|
279 |
map.put("Exception", t); |
|
280 |
} |
|
281 |
return map; |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public String toString() { |
|
286 |
return getClass().getSimpleName() + toMap(); |
|
287 |
// For example "MBeanOptions{MXBean=true, <etc>}". |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* <p>Indicates whether some other object is "equal to" this one. The |
|
292 |
* result is true if and only if the other object is also an instance |
|
293 |
* of MBeanOptions or a subclass, and has the same properties with |
|
294 |
* the same values.</p> |
|
295 |
* @return {@inheritDoc} |
|
296 |
*/ |
|
297 |
@Override |
|
298 |
public boolean equals(Object obj) { |
|
299 |
if (obj == this) |
|
300 |
return true; |
|
301 |
if (obj == null || obj.getClass() != this.getClass()) |
|
302 |
return false; |
|
303 |
return toMap().equals(((MBeanOptions) obj).toMap()); |
|
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public int hashCode() { |
|
308 |
return toMap().hashCode(); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* <p>Options to apply to an MBean proxy.</p> |
|
314 |
* |
|
315 |
* @see #newMBeanProxy |
|
316 |
*/ |
|
317 |
public static class ProxyOptions extends MBeanOptions { |
|
318 |
private static final long serialVersionUID = 7238804866098386559L; |
|
319 |
||
320 |
private boolean notificationEmitter; |
|
321 |
||
322 |
/** |
|
323 |
* <p>Construct a {@code ProxyOptions} object where all options have |
|
324 |
* their default values.</p> |
|
325 |
*/ |
|
326 |
public ProxyOptions() {} |
|
327 |
||
328 |
@Override |
|
329 |
public ProxyOptions clone() { |
|
330 |
return (ProxyOptions) super.clone(); |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* <p>Defines whether the returned proxy should |
|
335 |
* implement {@link NotificationEmitter}. The default value is false.</p> |
|
336 |
* |
|
337 |
* @return true if this proxy will be a NotificationEmitter. |
|
338 |
* |
|
339 |
* @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class, |
|
340 |
* MBeanOptions) |
|
341 |
*/ |
|
342 |
public boolean isNotificationEmitter() { |
|
343 |
return this.notificationEmitter; |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* <p>Set the {@link #isNotificationEmitter NotificationEmitter} option to |
|
348 |
* the given value.</p> |
|
349 |
* @param emitter the new value. |
|
350 |
*/ |
|
351 |
public void setNotificationEmitter(boolean emitter) { |
|
352 |
this.notificationEmitter = emitter; |
|
353 |
} |
|
354 |
||
355 |
// Canonical objects for each of (MXBean,!MXBean) x (Emitter,!Emitter) |
|
356 |
private static final ProxyOptions[] CANONICALS = { |
|
357 |
new ProxyOptions(), new ProxyOptions(), |
|
358 |
new ProxyOptions(), new ProxyOptions(), |
|
359 |
}; |
|
360 |
static { |
|
361 |
CANONICALS[1].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT); |
|
362 |
CANONICALS[2].setNotificationEmitter(true); |
|
363 |
CANONICALS[3].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT); |
|
364 |
CANONICALS[3].setNotificationEmitter(true); |
|
365 |
} |
|
366 |
@Override |
|
367 |
MBeanOptions[] canonicals() { |
|
368 |
return CANONICALS; |
|
369 |
} |
|
370 |
||
371 |
@Override |
|
372 |
boolean same(MBeanOptions opt) { |
|
373 |
return (super.same(opt) && opt instanceof ProxyOptions && |
|
374 |
((ProxyOptions) opt).notificationEmitter == notificationEmitter); |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
/** |
|
2 | 379 |
* <p>Make a proxy for a Standard MBean in a local or remote |
380 |
* MBean Server.</p> |
|
381 |
* |
|
382 |
* <p>If you have an MBean Server {@code mbs} containing an MBean |
|
383 |
* with {@link ObjectName} {@code name}, and if the MBean's |
|
384 |
* management interface is described by the Java interface |
|
385 |
* {@code MyMBean}, you can construct a proxy for the MBean like |
|
386 |
* this:</p> |
|
387 |
* |
|
388 |
* <pre> |
|
389 |
* MyMBean proxy = JMX.newMBeanProxy(mbs, name, MyMBean.class); |
|
390 |
* </pre> |
|
391 |
* |
|
392 |
* <p>Suppose, for example, {@code MyMBean} looks like this:</p> |
|
393 |
* |
|
394 |
* <pre> |
|
395 |
* public interface MyMBean { |
|
396 |
* public String getSomeAttribute(); |
|
397 |
* public void setSomeAttribute(String value); |
|
398 |
* public void someOperation(String param1, int param2); |
|
399 |
* } |
|
400 |
* </pre> |
|
401 |
* |
|
402 |
* <p>Then you can execute:</p> |
|
403 |
* |
|
404 |
* <ul> |
|
405 |
* |
|
406 |
* <li>{@code proxy.getSomeAttribute()} which will result in a |
|
407 |
* call to {@code mbs.}{@link MBeanServerConnection#getAttribute |
|
408 |
* getAttribute}{@code (name, "SomeAttribute")}. |
|
409 |
* |
|
410 |
* <li>{@code proxy.setSomeAttribute("whatever")} which will result |
|
411 |
* in a call to {@code mbs.}{@link MBeanServerConnection#setAttribute |
|
412 |
* setAttribute}{@code (name, new Attribute("SomeAttribute", "whatever"))}. |
|
413 |
* |
|
414 |
* <li>{@code proxy.someOperation("param1", 2)} which will be |
|
415 |
* translated into a call to {@code mbs.}{@link |
|
416 |
* MBeanServerConnection#invoke invoke}{@code (name, "someOperation", <etc>)}. |
|
417 |
* |
|
418 |
* </ul> |
|
419 |
* |
|
420 |
* <p>The object returned by this method is a |
|
421 |
* {@link Proxy} whose {@code InvocationHandler} is an |
|
422 |
* {@link MBeanServerInvocationHandler}.</p> |
|
423 |
* |
|
424 |
* <p>This method is equivalent to {@link |
|
425 |
* #newMBeanProxy(MBeanServerConnection, ObjectName, Class, |
|
426 |
* boolean) newMBeanProxy(connection, objectName, interfaceClass, |
|
427 |
* false)}.</p> |
|
428 |
* |
|
429 |
* @param connection the MBean server to forward to. |
|
430 |
* @param objectName the name of the MBean within |
|
431 |
* {@code connection} to forward to. |
|
432 |
* @param interfaceClass the management interface that the MBean |
|
433 |
* exports, which will also be implemented by the returned proxy. |
|
434 |
* |
|
435 |
* @param <T> allows the compiler to know that if the {@code |
|
436 |
* interfaceClass} parameter is {@code MyMBean.class}, for |
|
437 |
* example, then the return type is {@code MyMBean}. |
|
438 |
* |
|
439 |
* @return the new proxy instance. |
|
440 |
*/ |
|
441 |
public static <T> T newMBeanProxy(MBeanServerConnection connection, |
|
442 |
ObjectName objectName, |
|
443 |
Class<T> interfaceClass) { |
|
444 |
return newMBeanProxy(connection, objectName, interfaceClass, false); |
|
445 |
} |
|
446 |
||
447 |
/** |
|
448 |
* <p>Make a proxy for a Standard MBean in a local or remote MBean |
|
449 |
* Server that may also support the methods of {@link |
|
450 |
* NotificationEmitter}.</p> |
|
451 |
* |
|
452 |
* <p>This method behaves the same as {@link |
|
453 |
* #newMBeanProxy(MBeanServerConnection, ObjectName, Class)}, but |
|
687 | 454 |
* additionally, if {@code notificationEmitter} is {@code |
2 | 455 |
* true}, then the MBean is assumed to be a {@link |
456 |
* NotificationBroadcaster} or {@link NotificationEmitter} and the |
|
457 |
* returned proxy will implement {@link NotificationEmitter} as |
|
458 |
* well as {@code interfaceClass}. A call to {@link |
|
459 |
* NotificationBroadcaster#addNotificationListener} on the proxy |
|
460 |
* will result in a call to {@link |
|
461 |
* MBeanServerConnection#addNotificationListener(ObjectName, |
|
462 |
* NotificationListener, NotificationFilter, Object)}, and |
|
463 |
* likewise for the other methods of {@link |
|
464 |
* NotificationBroadcaster} and {@link NotificationEmitter}.</p> |
|
465 |
* |
|
466 |
* @param connection the MBean server to forward to. |
|
467 |
* @param objectName the name of the MBean within |
|
468 |
* {@code connection} to forward to. |
|
469 |
* @param interfaceClass the management interface that the MBean |
|
470 |
* exports, which will also be implemented by the returned proxy. |
|
687 | 471 |
* @param notificationEmitter make the returned proxy |
2 | 472 |
* implement {@link NotificationEmitter} by forwarding its methods |
473 |
* via {@code connection}. |
|
474 |
* @param <T> allows the compiler to know that if the {@code |
|
475 |
* interfaceClass} parameter is {@code MyMBean.class}, for |
|
476 |
* example, then the return type is {@code MyMBean}. |
|
477 |
* @return the new proxy instance. |
|
478 |
*/ |
|
479 |
public static <T> T newMBeanProxy(MBeanServerConnection connection, |
|
480 |
ObjectName objectName, |
|
481 |
Class<T> interfaceClass, |
|
687 | 482 |
boolean notificationEmitter) { |
483 |
ProxyOptions opts = new ProxyOptions(); |
|
484 |
opts.setNotificationEmitter(notificationEmitter); |
|
485 |
return newMBeanProxy(connection, objectName, interfaceClass, opts); |
|
2 | 486 |
} |
487 |
||
488 |
/** |
|
489 |
* <p>Make a proxy for an MXBean in a local or remote |
|
490 |
* MBean Server.</p> |
|
491 |
* |
|
492 |
* <p>If you have an MBean Server {@code mbs} containing an |
|
493 |
* MXBean with {@link ObjectName} {@code name}, and if the |
|
494 |
* MXBean's management interface is described by the Java |
|
495 |
* interface {@code MyMXBean}, you can construct a proxy for |
|
496 |
* the MXBean like this:</p> |
|
497 |
* |
|
498 |
* <pre> |
|
499 |
* MyMXBean proxy = JMX.newMXBeanProxy(mbs, name, MyMXBean.class); |
|
500 |
* </pre> |
|
501 |
* |
|
502 |
* <p>Suppose, for example, {@code MyMXBean} looks like this:</p> |
|
503 |
* |
|
504 |
* <pre> |
|
505 |
* public interface MyMXBean { |
|
506 |
* public String getSimpleAttribute(); |
|
507 |
* public void setSimpleAttribute(String value); |
|
508 |
* public {@link java.lang.management.MemoryUsage} getMappedAttribute(); |
|
509 |
* public void setMappedAttribute(MemoryUsage memoryUsage); |
|
510 |
* public MemoryUsage someOperation(String param1, MemoryUsage param2); |
|
511 |
* } |
|
512 |
* </pre> |
|
513 |
* |
|
514 |
* <p>Then:</p> |
|
515 |
* |
|
516 |
* <ul> |
|
517 |
* |
|
518 |
* <li><p>{@code proxy.getSimpleAttribute()} will result in a |
|
519 |
* call to {@code mbs.}{@link MBeanServerConnection#getAttribute |
|
520 |
* getAttribute}{@code (name, "SimpleAttribute")}.</p> |
|
521 |
* |
|
522 |
* <li><p>{@code proxy.setSimpleAttribute("whatever")} will result |
|
523 |
* in a call to {@code mbs.}{@link |
|
524 |
* MBeanServerConnection#setAttribute setAttribute}<code>(name, |
|
525 |
* new Attribute("SimpleAttribute", "whatever"))</code>.<p> |
|
526 |
* |
|
527 |
* <p>Because {@code String} is a <em>simple type</em>, in the |
|
528 |
* sense of {@link javax.management.openmbean.SimpleType}, it |
|
529 |
* is not changed in the context of an MXBean. The MXBean |
|
530 |
* proxy behaves the same as a Standard MBean proxy (see |
|
531 |
* {@link #newMBeanProxy(MBeanServerConnection, ObjectName, |
|
532 |
* Class) newMBeanProxy}) for the attribute {@code |
|
533 |
* SimpleAttribute}.</p> |
|
534 |
* |
|
535 |
* <li><p>{@code proxy.getMappedAttribute()} will result in a call |
|
536 |
* to {@code mbs.getAttribute("MappedAttribute")}. The MXBean |
|
537 |
* mapping rules mean that the actual type of the attribute {@code |
|
538 |
* MappedAttribute} will be {@link |
|
539 |
* javax.management.openmbean.CompositeData CompositeData} and |
|
540 |
* that is what the {@code mbs.getAttribute} call will return. |
|
541 |
* The proxy will then convert the {@code CompositeData} back into |
|
542 |
* the expected type {@code MemoryUsage} using the MXBean mapping |
|
543 |
* rules.</p> |
|
544 |
* |
|
545 |
* <li><p>Similarly, {@code proxy.setMappedAttribute(memoryUsage)} |
|
546 |
* will convert the {@code MemoryUsage} argument into a {@code |
|
547 |
* CompositeData} before calling {@code mbs.setAttribute}.</p> |
|
548 |
* |
|
549 |
* <li><p>{@code proxy.someOperation("whatever", memoryUsage)} |
|
550 |
* will convert the {@code MemoryUsage} argument into a {@code |
|
551 |
* CompositeData} and call {@code mbs.invoke}. The value returned |
|
552 |
* by {@code mbs.invoke} will be also be a {@code CompositeData}, |
|
553 |
* and the proxy will convert this into the expected type {@code |
|
554 |
* MemoryUsage} using the MXBean mapping rules.</p> |
|
555 |
* |
|
556 |
* </ul> |
|
557 |
* |
|
558 |
* <p>The object returned by this method is a |
|
559 |
* {@link Proxy} whose {@code InvocationHandler} is an |
|
560 |
* {@link MBeanServerInvocationHandler}.</p> |
|
561 |
* |
|
562 |
* <p>This method is equivalent to {@link |
|
563 |
* #newMXBeanProxy(MBeanServerConnection, ObjectName, Class, |
|
564 |
* boolean) newMXBeanProxy(connection, objectName, interfaceClass, |
|
565 |
* false)}.</p> |
|
566 |
* |
|
567 |
* @param connection the MBean server to forward to. |
|
568 |
* @param objectName the name of the MBean within |
|
569 |
* {@code connection} to forward to. |
|
570 |
* @param interfaceClass the MXBean interface, |
|
571 |
* which will also be implemented by the returned proxy. |
|
572 |
* |
|
573 |
* @param <T> allows the compiler to know that if the {@code |
|
574 |
* interfaceClass} parameter is {@code MyMXBean.class}, for |
|
575 |
* example, then the return type is {@code MyMXBean}. |
|
576 |
* |
|
577 |
* @return the new proxy instance. |
|
578 |
*/ |
|
579 |
public static <T> T newMXBeanProxy(MBeanServerConnection connection, |
|
580 |
ObjectName objectName, |
|
581 |
Class<T> interfaceClass) { |
|
582 |
return newMXBeanProxy(connection, objectName, interfaceClass, false); |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* <p>Make a proxy for an MXBean in a local or remote MBean |
|
587 |
* Server that may also support the methods of {@link |
|
588 |
* NotificationEmitter}.</p> |
|
589 |
* |
|
590 |
* <p>This method behaves the same as {@link |
|
591 |
* #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)}, but |
|
687 | 592 |
* additionally, if {@code notificationEmitter} is {@code |
2 | 593 |
* true}, then the MXBean is assumed to be a {@link |
594 |
* NotificationBroadcaster} or {@link NotificationEmitter} and the |
|
595 |
* returned proxy will implement {@link NotificationEmitter} as |
|
596 |
* well as {@code interfaceClass}. A call to {@link |
|
597 |
* NotificationBroadcaster#addNotificationListener} on the proxy |
|
598 |
* will result in a call to {@link |
|
599 |
* MBeanServerConnection#addNotificationListener(ObjectName, |
|
600 |
* NotificationListener, NotificationFilter, Object)}, and |
|
601 |
* likewise for the other methods of {@link |
|
602 |
* NotificationBroadcaster} and {@link NotificationEmitter}.</p> |
|
603 |
* |
|
604 |
* @param connection the MBean server to forward to. |
|
605 |
* @param objectName the name of the MBean within |
|
606 |
* {@code connection} to forward to. |
|
607 |
* @param interfaceClass the MXBean interface, |
|
608 |
* which will also be implemented by the returned proxy. |
|
687 | 609 |
* @param notificationEmitter make the returned proxy |
2 | 610 |
* implement {@link NotificationEmitter} by forwarding its methods |
611 |
* via {@code connection}. |
|
612 |
* @param <T> allows the compiler to know that if the {@code |
|
613 |
* interfaceClass} parameter is {@code MyMXBean.class}, for |
|
614 |
* example, then the return type is {@code MyMXBean}. |
|
615 |
* @return the new proxy instance. |
|
616 |
*/ |
|
617 |
public static <T> T newMXBeanProxy(MBeanServerConnection connection, |
|
618 |
ObjectName objectName, |
|
619 |
Class<T> interfaceClass, |
|
687 | 620 |
boolean notificationEmitter) { |
621 |
ProxyOptions opts = new ProxyOptions(); |
|
622 |
MXBeanMappingFactory f = MXBeanMappingFactory.forInterface(interfaceClass); |
|
623 |
opts.setMXBeanMappingFactory(f); |
|
624 |
opts.setNotificationEmitter(notificationEmitter); |
|
625 |
return newMBeanProxy(connection, objectName, interfaceClass, opts); |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* <p>Make a proxy for a Standard MBean or MXBean in a local or remote MBean |
|
630 |
* Server that may also support the methods of {@link |
|
631 |
* NotificationEmitter} and (for an MXBean) that may define custom MXBean |
|
632 |
* type mappings.</p> |
|
633 |
* |
|
634 |
* <p>This method behaves the same as |
|
635 |
* {@link #newMBeanProxy(MBeanServerConnection, ObjectName, Class)} or |
|
636 |
* {@link #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)}, |
|
637 |
* according as {@code opts.isMXBean()} is respectively false or true; but |
|
638 |
* with the following changes based on {@code opts}.</p> |
|
639 |
* |
|
640 |
* <ul> |
|
641 |
* <li>If {@code opts.isNotificationEmitter()} is {@code |
|
642 |
* true}, then the MBean is assumed to be a {@link |
|
643 |
* NotificationBroadcaster} or {@link NotificationEmitter} and the |
|
644 |
* returned proxy will implement {@link NotificationEmitter} as |
|
645 |
* well as {@code interfaceClass}. A call to {@link |
|
646 |
* NotificationBroadcaster#addNotificationListener} on the proxy |
|
647 |
* will result in a call to {@link |
|
648 |
* MBeanServerConnection#addNotificationListener(ObjectName, |
|
649 |
* NotificationListener, NotificationFilter, Object)}, and |
|
650 |
* likewise for the other methods of {@link |
|
651 |
* NotificationBroadcaster} and {@link NotificationEmitter}.</li> |
|
652 |
* |
|
653 |
* <li>If {@code opts.getMXBeanMappingFactory()} is not null, |
|
654 |
* then the mappings it defines will be applied to convert between |
|
655 |
* arbitrary Java types and Open Types.</li> |
|
656 |
* </ul> |
|
657 |
* |
|
658 |
* @param connection the MBean server to forward to. |
|
659 |
* @param objectName the name of the MBean within |
|
660 |
* {@code connection} to forward to. |
|
661 |
* @param interfaceClass the Standard MBean or MXBean interface, |
|
662 |
* which will also be implemented by the returned proxy. |
|
663 |
* @param opts the options to apply for this proxy. Can be null, |
|
664 |
* in which case default options are applied. |
|
665 |
* @param <T> allows the compiler to know that if the {@code |
|
666 |
* interfaceClass} parameter is {@code MyMXBean.class}, for |
|
667 |
* example, then the return type is {@code MyMXBean}. |
|
668 |
* @return the new proxy instance. |
|
669 |
* |
|
670 |
* @throws IllegalArgumentException if {@code interfaceClass} is not a |
|
671 |
* valid MXBean interface. |
|
672 |
*/ |
|
673 |
public static <T> T newMBeanProxy(MBeanServerConnection connection, |
|
674 |
ObjectName objectName, |
|
675 |
Class<T> interfaceClass, |
|
676 |
MBeanOptions opts) { |
|
2 | 677 |
try { |
687 | 678 |
return newMBeanProxy2(connection, objectName, interfaceClass, opts); |
2 | 679 |
} catch (NotCompliantMBeanException e) { |
680 |
throw new IllegalArgumentException(e); |
|
681 |
} |
|
687 | 682 |
} |
683 |
||
684 |
private static <T> T newMBeanProxy2(MBeanServerConnection connection, |
|
685 |
ObjectName objectName, |
|
686 |
Class<T> interfaceClass, |
|
687 |
MBeanOptions opts) |
|
688 |
throws NotCompliantMBeanException { |
|
689 |
||
690 |
if (opts == null) |
|
691 |
opts = new MBeanOptions(); |
|
692 |
||
693 |
boolean notificationEmitter = opts instanceof ProxyOptions && |
|
694 |
((ProxyOptions) opts).isNotificationEmitter(); |
|
695 |
||
696 |
MXBeanMappingFactory mappingFactory = opts.getMXBeanMappingFactory(); |
|
697 |
||
698 |
if (mappingFactory != null) { |
|
699 |
// Check interface for MXBean compliance |
|
700 |
Introspector.testComplianceMXBeanInterface(interfaceClass, |
|
701 |
mappingFactory); |
|
702 |
} |
|
703 |
||
2 | 704 |
InvocationHandler handler = new MBeanServerInvocationHandler( |
687 | 705 |
connection, objectName, opts); |
1510 | 706 |
final Class<?>[] interfaces; |
687 | 707 |
if (notificationEmitter) { |
2 | 708 |
interfaces = |
709 |
new Class<?>[] {interfaceClass, NotificationEmitter.class}; |
|
710 |
} else |
|
1510 | 711 |
interfaces = new Class<?>[] {interfaceClass}; |
2 | 712 |
Object proxy = Proxy.newProxyInstance( |
713 |
interfaceClass.getClassLoader(), |
|
714 |
interfaces, |
|
715 |
handler); |
|
716 |
return interfaceClass.cast(proxy); |
|
717 |
} |
|
718 |
||
719 |
/** |
|
720 |
* <p>Test whether an interface is an MXBean interface. |
|
721 |
* An interface is an MXBean interface if it is annotated |
|
722 |
* {@link MXBean @MXBean} or {@code @MXBean(true)} |
|
723 |
* or if it does not have an {@code @MXBean} annotation |
|
724 |
* and its name ends with "{@code MXBean}".</p> |
|
725 |
* |
|
726 |
* @param interfaceClass The candidate interface. |
|
727 |
* |
|
728 |
* @return true if {@code interfaceClass} is an interface and |
|
729 |
* meets the conditions described. |
|
730 |
* |
|
731 |
* @throws NullPointerException if {@code interfaceClass} is null. |
|
732 |
*/ |
|
733 |
public static boolean isMXBeanInterface(Class<?> interfaceClass) { |
|
734 |
if (!interfaceClass.isInterface()) |
|
735 |
return false; |
|
736 |
MXBean a = interfaceClass.getAnnotation(MXBean.class); |
|
737 |
if (a != null) |
|
738 |
return a.value(); |
|
739 |
return interfaceClass.getName().endsWith("MXBean"); |
|
740 |
// We don't bother excluding the case where the name is |
|
741 |
// exactly the string "MXBean" since that would mean there |
|
742 |
// was no package name, which is pretty unlikely in practice. |
|
743 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
744 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
745 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
746 |
* <p>Test if an MBean can emit notifications. An MBean can emit |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
747 |
* notifications if either it implements {@link NotificationBroadcaster} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
748 |
* (perhaps through its child interface {@link NotificationEmitter}), or |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
749 |
* it uses <a href="MBeanRegistration.html#injection">resource |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
750 |
* injection</a> to obtain an instance of {@link SendNotification} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
751 |
* through which it can send notifications.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
752 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
753 |
* @param mbean an MBean object. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
754 |
* @return true if the given object is a valid MBean that can emit |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
755 |
* notifications; false if the object is a valid MBean but that |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
756 |
* cannot emit notifications. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
757 |
* @throws NotCompliantMBeanException if the given object is not |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
758 |
* a valid MBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
759 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
760 |
public static boolean isNotificationSource(Object mbean) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
761 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
762 |
if (mbean instanceof NotificationBroadcaster) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
763 |
return true; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
764 |
Object resource = (mbean instanceof DynamicWrapperMBean) ? |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
765 |
((DynamicWrapperMBean) mbean).getWrappedObject() : mbean; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
766 |
return (MBeanInjector.injectsSendNotification(resource)); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
767 |
} |
2 | 768 |
} |