author | emcmanus |
Wed, 21 Oct 2009 17:33:18 +0200 | |
changeset 4156 | acaa49a2768a |
parent 1702 | 50e12093123c |
child 4328 | 9591511c1d88 |
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 com.sun.jmx.mbeanserver; |
|
27 |
||
28 |
||
29 |
import static com.sun.jmx.mbeanserver.Util.*; |
|
30 |
||
31 |
import java.lang.ref.WeakReference; |
|
32 |
import java.lang.reflect.Array; |
|
33 |
import java.lang.reflect.Constructor; |
|
34 |
import java.lang.reflect.InvocationTargetException; |
|
35 |
import java.lang.reflect.Method; |
|
36 |
import java.lang.reflect.Type; |
|
287 | 37 |
import java.util.Arrays; |
2 | 38 |
import java.util.List; |
39 |
import java.util.WeakHashMap; |
|
40 |
||
41 |
import javax.management.Descriptor; |
|
42 |
import javax.management.ImmutableDescriptor; |
|
687 | 43 |
import javax.management.IntrospectionException; |
2 | 44 |
import javax.management.InvalidAttributeValueException; |
45 |
import javax.management.MBeanAttributeInfo; |
|
46 |
import javax.management.MBeanConstructorInfo; |
|
47 |
import javax.management.MBeanException; |
|
48 |
import javax.management.MBeanInfo; |
|
49 |
import javax.management.MBeanNotificationInfo; |
|
50 |
import javax.management.MBeanOperationInfo; |
|
51 |
import javax.management.NotCompliantMBeanException; |
|
52 |
import javax.management.NotificationBroadcaster; |
|
53 |
import javax.management.ReflectionException; |
|
54 |
||
55 |
/** |
|
56 |
* An introspector for MBeans of a certain type. There is one instance |
|
687 | 57 |
* of this class for Standard MBeans, and one for every MXBeanMappingFactory; |
58 |
* these two cases correspond to the two concrete subclasses of this abstract |
|
59 |
* class. |
|
2 | 60 |
* |
61 |
* @param <M> the representation of methods for this kind of MBean: |
|
62 |
* Method for Standard MBeans, ConvertingMethod for MXBeans. |
|
63 |
* |
|
64 |
* @since 1.6 |
|
65 |
*/ |
|
66 |
/* |
|
67 |
* Using a type parameter <M> allows us to deal with the fact that |
|
68 |
* Method and ConvertingMethod have no useful common ancestor, on |
|
69 |
* which we could call getName, getGenericReturnType, etc. A simpler approach |
|
70 |
* would be to wrap every Method in an object that does have a common |
|
71 |
* ancestor with ConvertingMethod. But that would mean an extra object |
|
72 |
* for every Method in every Standard MBean interface. |
|
73 |
*/ |
|
4156 | 74 |
abstract class MBeanIntrospector<M> { |
2 | 75 |
static final class PerInterfaceMap<M> |
76 |
extends WeakHashMap<Class<?>, WeakReference<PerInterface<M>>> {} |
|
77 |
||
78 |
/** The map from interface to PerInterface for this type of MBean. */ |
|
79 |
abstract PerInterfaceMap<M> getPerInterfaceMap(); |
|
80 |
/** |
|
81 |
* The map from concrete implementation class and interface to |
|
82 |
* MBeanInfo for this type of MBean. |
|
83 |
*/ |
|
84 |
abstract MBeanInfoMap getMBeanInfoMap(); |
|
85 |
||
86 |
/** Make an interface analyzer for this type of MBean. */ |
|
87 |
abstract MBeanAnalyzer<M> getAnalyzer(Class<?> mbeanInterface) |
|
88 |
throws NotCompliantMBeanException; |
|
89 |
||
90 |
/** True if MBeans with this kind of introspector are MXBeans. */ |
|
91 |
abstract boolean isMXBean(); |
|
92 |
||
93 |
/** Find the M corresponding to the given Method. */ |
|
94 |
abstract M mFrom(Method m); |
|
95 |
||
96 |
/** Get the name of this method. */ |
|
97 |
abstract String getName(M m); |
|
98 |
||
99 |
/** |
|
100 |
* Get the return type of this method. This is the return type |
|
101 |
* of a method in a Java interface, so for MXBeans it is the |
|
102 |
* declared Java type, not the mapped Open Type. |
|
103 |
*/ |
|
104 |
abstract Type getGenericReturnType(M m); |
|
105 |
||
106 |
/** |
|
107 |
* Get the parameter types of this method in the Java interface |
|
108 |
* it came from. |
|
109 |
*/ |
|
110 |
abstract Type[] getGenericParameterTypes(M m); |
|
111 |
||
112 |
/** |
|
113 |
* Get the signature of this method as a caller would have to supply |
|
114 |
* it in MBeanServer.invoke. For MXBeans, the named types will be |
|
115 |
* the mapped Open Types for the parameters. |
|
116 |
*/ |
|
117 |
abstract String[] getSignature(M m); |
|
118 |
||
119 |
/** |
|
120 |
* Check that this method is valid. For example, a method in an |
|
121 |
* MXBean interface is not valid if one of its parameters cannot be |
|
122 |
* mapped to an Open Type. |
|
123 |
*/ |
|
687 | 124 |
abstract void checkMethod(M m); |
2 | 125 |
|
126 |
/** |
|
127 |
* Invoke the method with the given target and arguments. |
|
128 |
* |
|
129 |
* @param cookie Additional information about the target. For an |
|
130 |
* MXBean, this is the MXBeanLookup associated with the MXBean. |
|
131 |
*/ |
|
132 |
/* |
|
133 |
* It would be cleaner if the type of the cookie were a |
|
134 |
* type parameter to this class, but that would involve a lot of |
|
135 |
* messy type parameter propagation just to avoid a couple of casts. |
|
136 |
*/ |
|
137 |
abstract Object invokeM2(M m, Object target, Object[] args, Object cookie) |
|
138 |
throws InvocationTargetException, IllegalAccessException, |
|
139 |
MBeanException; |
|
140 |
||
141 |
/** |
|
142 |
* Test whether the given value is valid for the given parameter of this |
|
143 |
* M. |
|
144 |
*/ |
|
145 |
abstract boolean validParameter(M m, Object value, int paramNo, |
|
146 |
Object cookie); |
|
147 |
||
148 |
/** |
|
149 |
* Construct an MBeanAttributeInfo for the given attribute based on the |
|
150 |
* given getter and setter. One but not both of the getter and setter |
|
151 |
* may be null. |
|
152 |
*/ |
|
153 |
abstract MBeanAttributeInfo getMBeanAttributeInfo(String attributeName, |
|
4156 | 154 |
M getter, M setter); |
2 | 155 |
/** |
156 |
* Construct an MBeanOperationInfo for the given operation based on |
|
157 |
* the M it was derived from. |
|
158 |
*/ |
|
159 |
abstract MBeanOperationInfo getMBeanOperationInfo(String operationName, |
|
160 |
M operation); |
|
161 |
||
162 |
/** |
|
163 |
* Get a Descriptor containing fields that MBeans of this kind will |
|
164 |
* always have. For example, MXBeans will always have "mxbean=true". |
|
165 |
*/ |
|
166 |
abstract Descriptor getBasicMBeanDescriptor(); |
|
167 |
||
168 |
/** |
|
169 |
* Get a Descriptor containing additional fields beyond the ones |
|
170 |
* from getBasicMBeanDescriptor that MBeans whose concrete class |
|
171 |
* is resourceClass will always have. |
|
172 |
*/ |
|
173 |
abstract Descriptor getMBeanDescriptor(Class<?> resourceClass); |
|
174 |
||
687 | 175 |
/** |
287 | 176 |
* Get the methods to be analyzed to build the MBean interface. |
177 |
*/ |
|
178 |
List<Method> getMethods(final Class<?> mbeanType) throws Exception { |
|
4156 | 179 |
return Arrays.asList(mbeanType.getMethods()); |
287 | 180 |
} |
2 | 181 |
|
182 |
final PerInterface<M> getPerInterface(Class<?> mbeanInterface) |
|
183 |
throws NotCompliantMBeanException { |
|
184 |
PerInterfaceMap<M> map = getPerInterfaceMap(); |
|
185 |
synchronized (map) { |
|
186 |
WeakReference<PerInterface<M>> wr = map.get(mbeanInterface); |
|
187 |
PerInterface<M> pi = (wr == null) ? null : wr.get(); |
|
188 |
if (pi == null) { |
|
189 |
try { |
|
190 |
MBeanAnalyzer<M> analyzer = getAnalyzer(mbeanInterface); |
|
191 |
MBeanInfo mbeanInfo = |
|
192 |
makeInterfaceMBeanInfo(mbeanInterface, analyzer); |
|
193 |
pi = new PerInterface<M>(mbeanInterface, this, analyzer, |
|
194 |
mbeanInfo); |
|
195 |
wr = new WeakReference<PerInterface<M>>(pi); |
|
196 |
map.put(mbeanInterface, wr); |
|
197 |
} catch (Exception x) { |
|
198 |
throw Introspector.throwException(mbeanInterface,x); |
|
199 |
} |
|
200 |
} |
|
201 |
return pi; |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Make the MBeanInfo skeleton for the given MBean interface using |
|
207 |
* the given analyzer. This will never be the MBeanInfo of any real |
|
208 |
* MBean (because the getClassName() must be a concrete class), but |
|
209 |
* its MBeanAttributeInfo[] and MBeanOperationInfo[] can be inserted |
|
210 |
* into such an MBeanInfo, and its Descriptor can be the basis for |
|
211 |
* the MBeanInfo's Descriptor. |
|
212 |
*/ |
|
213 |
private MBeanInfo makeInterfaceMBeanInfo(Class<?> mbeanInterface, |
|
4156 | 214 |
MBeanAnalyzer<M> analyzer) { |
2 | 215 |
final MBeanInfoMaker maker = new MBeanInfoMaker(); |
216 |
analyzer.visit(maker); |
|
4156 | 217 |
final String description = |
2 | 218 |
"Information on the management interface of the MBean"; |
219 |
return maker.makeMBeanInfo(mbeanInterface, description); |
|
220 |
} |
|
221 |
||
222 |
/** True if the given getter and setter are consistent. */ |
|
223 |
final boolean consistent(M getter, M setter) { |
|
224 |
return (getter == null || setter == null || |
|
225 |
getGenericReturnType(getter).equals(getGenericParameterTypes(setter)[0])); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Invoke the given M on the given target with the given args and cookie. |
|
230 |
* Wrap exceptions appropriately. |
|
231 |
*/ |
|
232 |
final Object invokeM(M m, Object target, Object[] args, Object cookie) |
|
233 |
throws MBeanException, ReflectionException { |
|
234 |
try { |
|
235 |
return invokeM2(m, target, args, cookie); |
|
236 |
} catch (InvocationTargetException e) { |
|
237 |
unwrapInvocationTargetException(e); |
|
238 |
throw new RuntimeException(e); // not reached |
|
239 |
} catch (IllegalAccessException e) { |
|
240 |
throw new ReflectionException(e, e.toString()); |
|
241 |
} |
|
242 |
/* We do not catch and wrap RuntimeException or Error, |
|
243 |
* because we're in a DynamicMBean, so the logic for DynamicMBeans |
|
244 |
* will do the wrapping. |
|
245 |
*/ |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Invoke the given setter on the given target with the given argument |
|
250 |
* and cookie. Wrap exceptions appropriately. |
|
251 |
*/ |
|
252 |
/* If the value is of the wrong type for the method we are about to |
|
253 |
* invoke, we are supposed to throw an InvalidAttributeValueException. |
|
254 |
* Rather than making the check always, we invoke the method, then |
|
255 |
* if it throws an exception we check the type to see if that was |
|
256 |
* what caused the exception. The assumption is that an exception |
|
257 |
* from an invalid type will arise before any user method is ever |
|
258 |
* called (either in reflection or in OpenConverter). |
|
259 |
*/ |
|
260 |
final void invokeSetter(String name, M setter, Object target, Object arg, |
|
261 |
Object cookie) |
|
262 |
throws MBeanException, ReflectionException, |
|
263 |
InvalidAttributeValueException { |
|
264 |
try { |
|
265 |
invokeM2(setter, target, new Object[] {arg}, cookie); |
|
266 |
} catch (IllegalAccessException e) { |
|
267 |
throw new ReflectionException(e, e.toString()); |
|
268 |
} catch (RuntimeException e) { |
|
269 |
maybeInvalidParameter(name, setter, arg, cookie); |
|
270 |
throw e; |
|
271 |
} catch (InvocationTargetException e) { |
|
272 |
maybeInvalidParameter(name, setter, arg, cookie); |
|
273 |
unwrapInvocationTargetException(e); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
private void maybeInvalidParameter(String name, M setter, Object arg, |
|
278 |
Object cookie) |
|
279 |
throws InvalidAttributeValueException { |
|
280 |
if (!validParameter(setter, arg, 0, cookie)) { |
|
281 |
final String msg = |
|
282 |
"Invalid value for attribute " + name + ": " + arg; |
|
283 |
throw new InvalidAttributeValueException(msg); |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
static boolean isValidParameter(Method m, Object value, int paramNo) { |
|
288 |
Class<?> c = m.getParameterTypes()[paramNo]; |
|
289 |
try { |
|
290 |
// Following is expensive but we only call this method to determine |
|
291 |
// if an exception is due to an incompatible parameter type. |
|
292 |
// Plain old c.isInstance doesn't work for primitive types. |
|
293 |
Object a = Array.newInstance(c, 1); |
|
294 |
Array.set(a, 0, value); |
|
295 |
return true; |
|
296 |
} catch (IllegalArgumentException e) { |
|
297 |
return false; |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
private static void |
|
302 |
unwrapInvocationTargetException(InvocationTargetException e) |
|
303 |
throws MBeanException { |
|
304 |
Throwable t = e.getCause(); |
|
305 |
if (t instanceof RuntimeException) |
|
306 |
throw (RuntimeException) t; |
|
307 |
else if (t instanceof Error) |
|
308 |
throw (Error) t; |
|
309 |
else |
|
310 |
throw new MBeanException((Exception) t, |
|
311 |
(t == null ? null : t.toString())); |
|
312 |
} |
|
313 |
||
314 |
/** A visitor that constructs the per-interface MBeanInfo. */ |
|
687 | 315 |
private class MBeanInfoMaker |
4156 | 316 |
implements MBeanAnalyzer.MBeanVisitor<M> { |
2 | 317 |
|
318 |
public void visitAttribute(String attributeName, |
|
319 |
M getter, |
|
4156 | 320 |
M setter) { |
2 | 321 |
MBeanAttributeInfo mbai = |
322 |
getMBeanAttributeInfo(attributeName, getter, setter); |
|
323 |
||
324 |
attrs.add(mbai); |
|
325 |
} |
|
326 |
||
327 |
public void visitOperation(String operationName, |
|
328 |
M operation) { |
|
329 |
MBeanOperationInfo mboi = |
|
330 |
getMBeanOperationInfo(operationName, operation); |
|
331 |
||
332 |
ops.add(mboi); |
|
333 |
} |
|
334 |
||
335 |
/** Make an MBeanInfo based on the attributes and operations |
|
336 |
* found in the interface. */ |
|
337 |
MBeanInfo makeMBeanInfo(Class<?> mbeanInterface, |
|
338 |
String description) { |
|
339 |
final MBeanAttributeInfo[] attrArray = |
|
340 |
attrs.toArray(new MBeanAttributeInfo[0]); |
|
341 |
final MBeanOperationInfo[] opArray = |
|
342 |
ops.toArray(new MBeanOperationInfo[0]); |
|
343 |
final String interfaceClassName = |
|
344 |
"interfaceClassName=" + mbeanInterface.getName(); |
|
687 | 345 |
final Descriptor classNameDescriptor = |
2 | 346 |
new ImmutableDescriptor(interfaceClassName); |
347 |
final Descriptor mbeanDescriptor = getBasicMBeanDescriptor(); |
|
348 |
final Descriptor annotatedDescriptor = |
|
4156 | 349 |
Introspector.descriptorForElement(mbeanInterface); |
2 | 350 |
final Descriptor descriptor = |
687 | 351 |
DescriptorCache.getInstance().union( |
352 |
classNameDescriptor, |
|
2 | 353 |
mbeanDescriptor, |
354 |
annotatedDescriptor); |
|
355 |
||
356 |
return new MBeanInfo(mbeanInterface.getName(), |
|
357 |
description, |
|
358 |
attrArray, |
|
359 |
null, |
|
360 |
opArray, |
|
361 |
null, |
|
362 |
descriptor); |
|
363 |
} |
|
364 |
||
365 |
private final List<MBeanAttributeInfo> attrs = newList(); |
|
366 |
private final List<MBeanOperationInfo> ops = newList(); |
|
367 |
} |
|
368 |
||
369 |
/* |
|
370 |
* Looking up the MBeanInfo for a given base class (implementation class) |
|
371 |
* is complicated by the fact that we may use the same base class with |
|
372 |
* several different explicit MBean interfaces via the |
|
373 |
* javax.management.StandardMBean class. It is further complicated |
|
374 |
* by the fact that we have to be careful not to retain a strong reference |
|
375 |
* to any Class object for fear we would prevent a ClassLoader from being |
|
376 |
* garbage-collected. So we have a first lookup from the base class |
|
377 |
* to a map for each interface that base class might specify giving |
|
378 |
* the MBeanInfo constructed for that base class and interface. |
|
379 |
*/ |
|
380 |
static class MBeanInfoMap |
|
381 |
extends WeakHashMap<Class<?>, WeakHashMap<Class<?>, MBeanInfo>> { |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* Return the MBeanInfo for the given resource, based on the given |
|
386 |
* per-interface data. |
|
387 |
*/ |
|
4156 | 388 |
final MBeanInfo getMBeanInfo(Object resource, PerInterface<M> perInterface) { |
2 | 389 |
MBeanInfo mbi = |
390 |
getClassMBeanInfo(resource.getClass(), perInterface); |
|
4156 | 391 |
MBeanNotificationInfo[] notifs = findNotifications(resource); |
392 |
if (notifs == null || notifs.length == 0) |
|
2 | 393 |
return mbi; |
394 |
else { |
|
395 |
return new MBeanInfo(mbi.getClassName(), |
|
396 |
mbi.getDescription(), |
|
397 |
mbi.getAttributes(), |
|
398 |
mbi.getConstructors(), |
|
399 |
mbi.getOperations(), |
|
400 |
notifs, |
|
4156 | 401 |
mbi.getDescriptor()); |
2 | 402 |
} |
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Return the basic MBeanInfo for resources of the given class and |
|
407 |
* per-interface data. This MBeanInfo might not be the final MBeanInfo |
|
408 |
* for instances of the class, because if the class is a |
|
409 |
* NotificationBroadcaster then each instance gets to decide what |
|
410 |
* MBeanNotificationInfo[] to put in its own MBeanInfo. |
|
411 |
*/ |
|
412 |
final MBeanInfo getClassMBeanInfo(Class<?> resourceClass, |
|
413 |
PerInterface<M> perInterface) { |
|
414 |
MBeanInfoMap map = getMBeanInfoMap(); |
|
415 |
synchronized (map) { |
|
416 |
WeakHashMap<Class<?>, MBeanInfo> intfMap = map.get(resourceClass); |
|
417 |
if (intfMap == null) { |
|
418 |
intfMap = new WeakHashMap<Class<?>, MBeanInfo>(); |
|
419 |
map.put(resourceClass, intfMap); |
|
420 |
} |
|
421 |
Class<?> intfClass = perInterface.getMBeanInterface(); |
|
422 |
MBeanInfo mbi = intfMap.get(intfClass); |
|
423 |
if (mbi == null) { |
|
424 |
MBeanInfo imbi = perInterface.getMBeanInfo(); |
|
425 |
Descriptor descriptor = |
|
426 |
ImmutableDescriptor.union(imbi.getDescriptor(), |
|
427 |
getMBeanDescriptor(resourceClass)); |
|
428 |
mbi = new MBeanInfo(resourceClass.getName(), |
|
429 |
imbi.getDescription(), |
|
430 |
imbi.getAttributes(), |
|
431 |
findConstructors(resourceClass), |
|
432 |
imbi.getOperations(), |
|
433 |
(MBeanNotificationInfo[]) null, |
|
434 |
descriptor); |
|
435 |
intfMap.put(intfClass, mbi); |
|
436 |
} |
|
437 |
return mbi; |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
static MBeanNotificationInfo[] findNotifications(Object moi) { |
|
4156 | 442 |
if (!(moi instanceof NotificationBroadcaster)) |
443 |
return null; |
|
444 |
MBeanNotificationInfo[] mbn = |
|
445 |
((NotificationBroadcaster) moi).getNotificationInfo(); |
|
446 |
if (mbn == null) |
|
447 |
return null; |
|
448 |
MBeanNotificationInfo[] result = |
|
449 |
new MBeanNotificationInfo[mbn.length]; |
|
450 |
for (int i = 0; i < mbn.length; i++) { |
|
451 |
MBeanNotificationInfo ni = mbn[i]; |
|
452 |
if (ni.getClass() != MBeanNotificationInfo.class) |
|
453 |
ni = (MBeanNotificationInfo) ni.clone(); |
|
454 |
result[i] = ni; |
|
2 | 455 |
} |
4156 | 456 |
return result; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
457 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
458 |
|
2 | 459 |
private static MBeanConstructorInfo[] findConstructors(Class<?> c) { |
1221
e3dc70e4e610
6746196: Some JMX classes do not compile with Eclipse compiler
emcmanus
parents:
834
diff
changeset
|
460 |
Constructor<?>[] cons = c.getConstructors(); |
2 | 461 |
MBeanConstructorInfo[] mbc = new MBeanConstructorInfo[cons.length]; |
462 |
for (int i = 0; i < cons.length; i++) { |
|
4156 | 463 |
final String descr = "Public constructor of the MBean"; |
2 | 464 |
mbc[i] = new MBeanConstructorInfo(descr, cons[i]); |
465 |
} |
|
466 |
return mbc; |
|
467 |
} |
|
468 |
||
469 |
} |