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