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