2
|
1 |
/*
|
|
2 |
* Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
import java.lang.annotation.Annotation;
|
|
29 |
import java.lang.reflect.AnnotatedElement;
|
|
30 |
import java.lang.reflect.Constructor;
|
|
31 |
import java.lang.reflect.Method;
|
|
32 |
import java.lang.reflect.Modifier;
|
|
33 |
import java.lang.reflect.UndeclaredThrowableException;
|
|
34 |
import java.util.Arrays;
|
|
35 |
import java.util.HashMap;
|
|
36 |
import java.util.Map;
|
|
37 |
|
|
38 |
import javax.management.Descriptor;
|
|
39 |
import javax.management.DescriptorKey;
|
|
40 |
import javax.management.DynamicMBean;
|
|
41 |
import javax.management.ImmutableDescriptor;
|
|
42 |
import javax.management.MBeanInfo;
|
|
43 |
import javax.management.NotCompliantMBeanException;
|
|
44 |
|
|
45 |
import com.sun.jmx.mbeanserver.Util;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* This class contains the methods for performing all the tests needed to verify
|
|
49 |
* that a class represents a JMX compliant MBean.
|
|
50 |
*
|
|
51 |
* @since 1.5
|
|
52 |
*/
|
|
53 |
public class Introspector {
|
|
54 |
|
|
55 |
|
|
56 |
/*
|
|
57 |
* ------------------------------------------
|
|
58 |
* PRIVATE CONSTRUCTORS
|
|
59 |
* ------------------------------------------
|
|
60 |
*/
|
|
61 |
|
|
62 |
// private constructor defined to "hide" the default public constructor
|
|
63 |
private Introspector() {
|
|
64 |
|
|
65 |
// ------------------------------
|
|
66 |
// ------------------------------
|
|
67 |
|
|
68 |
}
|
|
69 |
|
|
70 |
/*
|
|
71 |
* ------------------------------------------
|
|
72 |
* PUBLIC METHODS
|
|
73 |
* ------------------------------------------
|
|
74 |
*/
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Tell whether a MBean of the given class is a Dynamic MBean.
|
|
78 |
* This method does nothing more than returning
|
|
79 |
* <pre>
|
|
80 |
* javax.management.DynamicMBean.class.isAssignableFrom(c)
|
|
81 |
* </pre>
|
|
82 |
* This method does not check for any JMX MBean compliance:
|
|
83 |
* <ul><li>If <code>true</code> is returned, then instances of
|
|
84 |
* <code>c</code> are DynamicMBean.</li>
|
|
85 |
* <li>If <code>false</code> is returned, then no further
|
|
86 |
* assumption can be made on instances of <code>c</code>.
|
|
87 |
* In particular, instances of <code>c</code> may, or may not
|
|
88 |
* be JMX standard MBeans.</li>
|
|
89 |
* </ul>
|
|
90 |
* @param c The class of the MBean under examination.
|
|
91 |
* @return <code>true</code> if instances of <code>c</code> are
|
|
92 |
* Dynamic MBeans, <code>false</code> otherwise.
|
|
93 |
*
|
|
94 |
**/
|
|
95 |
public static final boolean isDynamic(final Class c) {
|
|
96 |
// Check if the MBean implements the DynamicMBean interface
|
|
97 |
return javax.management.DynamicMBean.class.isAssignableFrom(c);
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Basic method for testing that a MBean of a given class can be
|
|
102 |
* instantiated by the MBean server.<p>
|
|
103 |
* This method checks that:
|
|
104 |
* <ul><li>The given class is a concrete class.</li>
|
|
105 |
* <li>The given class exposes at least one public constructor.</li>
|
|
106 |
* </ul>
|
|
107 |
* If these conditions are not met, throws a NotCompliantMBeanException.
|
|
108 |
* @param c The class of the MBean we want to create.
|
|
109 |
* @exception NotCompliantMBeanException if the MBean class makes it
|
|
110 |
* impossible to instantiate the MBean from within the
|
|
111 |
* MBeanServer.
|
|
112 |
*
|
|
113 |
**/
|
|
114 |
public static void testCreation(Class c)
|
|
115 |
throws NotCompliantMBeanException {
|
|
116 |
// Check if the class is a concrete class
|
|
117 |
final int mods = c.getModifiers();
|
|
118 |
if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
|
|
119 |
throw new NotCompliantMBeanException("MBean class must be concrete");
|
|
120 |
}
|
|
121 |
|
|
122 |
// Check if the MBean has a public constructor
|
|
123 |
final Constructor[] consList = c.getConstructors();
|
|
124 |
if (consList.length == 0) {
|
|
125 |
throw new NotCompliantMBeanException("MBean class must have public constructor");
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
public static void checkCompliance(Class mbeanClass)
|
|
130 |
throws NotCompliantMBeanException {
|
|
131 |
// Is DynamicMBean?
|
|
132 |
//
|
|
133 |
if (DynamicMBean.class.isAssignableFrom(mbeanClass))
|
|
134 |
return;
|
|
135 |
// Is Standard MBean?
|
|
136 |
//
|
|
137 |
final Exception mbeanException;
|
|
138 |
try {
|
|
139 |
getStandardMBeanInterface(mbeanClass);
|
|
140 |
return;
|
|
141 |
} catch (NotCompliantMBeanException e) {
|
|
142 |
mbeanException = e;
|
|
143 |
}
|
|
144 |
// Is MXBean?
|
|
145 |
//
|
|
146 |
final Exception mxbeanException;
|
|
147 |
try {
|
|
148 |
getMXBeanInterface(mbeanClass);
|
|
149 |
return;
|
|
150 |
} catch (NotCompliantMBeanException e) {
|
|
151 |
mxbeanException = e;
|
|
152 |
}
|
|
153 |
final String msg =
|
|
154 |
"MBean class " + mbeanClass.getName() + " does not implement " +
|
|
155 |
"DynamicMBean, neither follows the Standard MBean conventions (" +
|
|
156 |
mbeanException.toString() + ") nor the MXBean conventions (" +
|
|
157 |
mxbeanException.toString() + ")";
|
|
158 |
throw new NotCompliantMBeanException(msg);
|
|
159 |
}
|
|
160 |
|
|
161 |
public static DynamicMBean makeDynamicMBean(Object mbean)
|
|
162 |
throws NotCompliantMBeanException {
|
|
163 |
if (mbean instanceof DynamicMBean)
|
|
164 |
return (DynamicMBean) mbean;
|
|
165 |
final Class mbeanClass = mbean.getClass();
|
|
166 |
Class<?> c = null;
|
|
167 |
try {
|
|
168 |
c = getStandardMBeanInterface(mbeanClass);
|
|
169 |
} catch (NotCompliantMBeanException e) {
|
|
170 |
// Ignore exception - we need to check whether
|
|
171 |
// mbean is an MXBean first.
|
|
172 |
}
|
|
173 |
if (c != null)
|
|
174 |
return new StandardMBeanSupport(mbean, Util.<Class<Object>>cast(c));
|
|
175 |
|
|
176 |
try {
|
|
177 |
c = getMXBeanInterface(mbeanClass);
|
|
178 |
} catch (NotCompliantMBeanException e) {
|
|
179 |
// Ignore exception - we cannot decide whether mbean was supposed
|
|
180 |
// to be an MBean or an MXBean. We will call checkCompliance()
|
|
181 |
// to generate the appropriate exception.
|
|
182 |
}
|
|
183 |
if (c != null)
|
|
184 |
return new MXBeanSupport(mbean, Util.<Class<Object>>cast(c));
|
|
185 |
checkCompliance(mbeanClass);
|
|
186 |
throw new NotCompliantMBeanException("Not compliant"); // not reached
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Basic method for testing if a given class is a JMX compliant MBean.
|
|
191 |
*
|
|
192 |
* @param baseClass The class to be tested
|
|
193 |
*
|
|
194 |
* @return <code>null</code> if the MBean is a DynamicMBean,
|
|
195 |
* the computed {@link javax.management.MBeanInfo} otherwise.
|
|
196 |
* @exception NotCompliantMBeanException The specified class is not a
|
|
197 |
* JMX compliant MBean
|
|
198 |
*/
|
|
199 |
public static MBeanInfo testCompliance(Class baseClass)
|
|
200 |
throws NotCompliantMBeanException {
|
|
201 |
|
|
202 |
// ------------------------------
|
|
203 |
// ------------------------------
|
|
204 |
|
|
205 |
// Check if the MBean implements the MBean or the Dynamic
|
|
206 |
// MBean interface
|
|
207 |
if (isDynamic(baseClass))
|
|
208 |
return null;
|
|
209 |
|
|
210 |
return testCompliance(baseClass, null);
|
|
211 |
}
|
|
212 |
|
|
213 |
public static void testComplianceMXBeanInterface(Class interfaceClass)
|
|
214 |
throws NotCompliantMBeanException {
|
|
215 |
MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Basic method for testing if a given class is a JMX compliant
|
|
220 |
* Standard MBean. This method is only called by the legacy code
|
|
221 |
* in com.sun.management.jmx.
|
|
222 |
*
|
|
223 |
* @param baseClass The class to be tested.
|
|
224 |
*
|
|
225 |
* @param mbeanInterface the MBean interface that the class implements,
|
|
226 |
* or null if the interface must be determined by introspection.
|
|
227 |
*
|
|
228 |
* @return the computed {@link javax.management.MBeanInfo}.
|
|
229 |
* @exception NotCompliantMBeanException The specified class is not a
|
|
230 |
* JMX compliant Standard MBean
|
|
231 |
*/
|
|
232 |
public static synchronized MBeanInfo
|
|
233 |
testCompliance(final Class<?> baseClass,
|
|
234 |
Class<?> mbeanInterface)
|
|
235 |
throws NotCompliantMBeanException {
|
|
236 |
if (mbeanInterface == null)
|
|
237 |
mbeanInterface = getStandardMBeanInterface(baseClass);
|
|
238 |
MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
|
|
239 |
return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
|
|
240 |
}
|
|
241 |
|
|
242 |
private static <M> MBeanInfo
|
|
243 |
getClassMBeanInfo(MBeanIntrospector<M> introspector,
|
|
244 |
Class<?> baseClass, Class<?> mbeanInterface)
|
|
245 |
throws NotCompliantMBeanException {
|
|
246 |
PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface);
|
|
247 |
return introspector.getClassMBeanInfo(baseClass, perInterface);
|
|
248 |
}
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Get the MBean interface implemented by a JMX Standard
|
|
252 |
* MBean class. This method is only called by the legacy
|
|
253 |
* code in "com.sun.management.jmx".
|
|
254 |
*
|
|
255 |
* @param baseClass The class to be tested.
|
|
256 |
*
|
|
257 |
* @return The MBean interface implemented by the MBean.
|
|
258 |
* Return <code>null</code> if the MBean is a DynamicMBean,
|
|
259 |
* or if no MBean interface is found.
|
|
260 |
*/
|
|
261 |
public static Class getMBeanInterface(Class baseClass) {
|
|
262 |
// Check if the given class implements the MBean interface
|
|
263 |
// or the Dynamic MBean interface
|
|
264 |
if (isDynamic(baseClass)) return null;
|
|
265 |
try {
|
|
266 |
return getStandardMBeanInterface(baseClass);
|
|
267 |
} catch (NotCompliantMBeanException e) {
|
|
268 |
return null;
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Get the MBean interface implemented by a JMX Standard MBean class.
|
|
274 |
*
|
|
275 |
* @param baseClass The class to be tested.
|
|
276 |
*
|
|
277 |
* @return The MBean interface implemented by the Standard MBean.
|
|
278 |
*
|
|
279 |
* @throws NotCompliantMBeanException The specified class is
|
|
280 |
* not a JMX compliant Standard MBean.
|
|
281 |
*/
|
|
282 |
public static Class getStandardMBeanInterface(Class baseClass)
|
|
283 |
throws NotCompliantMBeanException {
|
|
284 |
Class current = baseClass;
|
|
285 |
Class mbeanInterface = null;
|
|
286 |
while (current != null) {
|
|
287 |
mbeanInterface =
|
|
288 |
findMBeanInterface(current, current.getName());
|
|
289 |
if (mbeanInterface != null) break;
|
|
290 |
current = current.getSuperclass();
|
|
291 |
}
|
|
292 |
if (mbeanInterface != null) {
|
|
293 |
return mbeanInterface;
|
|
294 |
} else {
|
|
295 |
final String msg =
|
|
296 |
"Class " + baseClass.getName() +
|
|
297 |
" is not a JMX compliant Standard MBean";
|
|
298 |
throw new NotCompliantMBeanException(msg);
|
|
299 |
}
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* Get the MXBean interface implemented by a JMX MXBean class.
|
|
304 |
*
|
|
305 |
* @param baseClass The class to be tested.
|
|
306 |
*
|
|
307 |
* @return The MXBean interface implemented by the MXBean.
|
|
308 |
*
|
|
309 |
* @throws NotCompliantMBeanException The specified class is
|
|
310 |
* not a JMX compliant MXBean.
|
|
311 |
*/
|
|
312 |
public static Class getMXBeanInterface(Class baseClass)
|
|
313 |
throws NotCompliantMBeanException {
|
|
314 |
try {
|
|
315 |
return MXBeanSupport.findMXBeanInterface(baseClass);
|
|
316 |
} catch (Exception e) {
|
|
317 |
throw throwException(baseClass,e);
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
/*
|
|
322 |
* ------------------------------------------
|
|
323 |
* PRIVATE METHODS
|
|
324 |
* ------------------------------------------
|
|
325 |
*/
|
|
326 |
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Try to find the MBean interface corresponding to the class aName
|
|
330 |
* - i.e. <i>aName</i>MBean, from within aClass and its superclasses.
|
|
331 |
**/
|
|
332 |
private static Class findMBeanInterface(Class aClass, String aName) {
|
|
333 |
Class current = aClass;
|
|
334 |
while (current != null) {
|
|
335 |
final Class[] interfaces = current.getInterfaces();
|
|
336 |
final int len = interfaces.length;
|
|
337 |
for (int i=0;i<len;i++) {
|
|
338 |
final Class inter =
|
|
339 |
implementsMBean(interfaces[i], aName);
|
|
340 |
if (inter != null) return inter;
|
|
341 |
}
|
|
342 |
current = current.getSuperclass();
|
|
343 |
}
|
|
344 |
return null;
|
|
345 |
}
|
|
346 |
|
|
347 |
public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
|
|
348 |
if (elmt == null)
|
|
349 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR;
|
|
350 |
final Annotation[] annots = elmt.getAnnotations();
|
|
351 |
return descriptorForAnnotations(annots);
|
|
352 |
}
|
|
353 |
|
|
354 |
public static Descriptor descriptorForAnnotations(Annotation[] annots) {
|
|
355 |
if (annots.length == 0)
|
|
356 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR;
|
|
357 |
Map<String, Object> descriptorMap = new HashMap<String, Object>();
|
|
358 |
for (Annotation a : annots) {
|
|
359 |
Class<? extends Annotation> c = a.annotationType();
|
|
360 |
Method[] elements = c.getMethods();
|
|
361 |
for (Method element : elements) {
|
|
362 |
DescriptorKey key = element.getAnnotation(DescriptorKey.class);
|
|
363 |
if (key != null) {
|
|
364 |
String name = key.value();
|
|
365 |
Object value;
|
|
366 |
try {
|
|
367 |
value = element.invoke(a);
|
|
368 |
} catch (RuntimeException e) {
|
|
369 |
// we don't expect this - except for possibly
|
|
370 |
// security exceptions?
|
|
371 |
// RuntimeExceptions shouldn't be "UndeclaredThrowable".
|
|
372 |
// anyway...
|
|
373 |
//
|
|
374 |
throw e;
|
|
375 |
} catch (Exception e) {
|
|
376 |
// we don't expect this
|
|
377 |
throw new UndeclaredThrowableException(e);
|
|
378 |
}
|
|
379 |
value = annotationToField(value);
|
|
380 |
Object oldValue = descriptorMap.put(name, value);
|
|
381 |
if (oldValue != null && !equals(oldValue, value)) {
|
|
382 |
final String msg =
|
|
383 |
"Inconsistent values for descriptor field " + name +
|
|
384 |
" from annotations: " + value + " :: " + oldValue;
|
|
385 |
throw new IllegalArgumentException(msg);
|
|
386 |
}
|
|
387 |
}
|
|
388 |
}
|
|
389 |
}
|
|
390 |
|
|
391 |
if (descriptorMap.isEmpty())
|
|
392 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR;
|
|
393 |
else
|
|
394 |
return new ImmutableDescriptor(descriptorMap);
|
|
395 |
}
|
|
396 |
|
|
397 |
/**
|
|
398 |
* Throws a NotCompliantMBeanException or a SecurityException.
|
|
399 |
* @param notCompliant the class which was under examination
|
|
400 |
* @param cause the raeson why NotCompliantMBeanException should
|
|
401 |
* be thrown.
|
|
402 |
* @return nothing - this method always throw an exception.
|
|
403 |
* The return type makes it possible to write
|
|
404 |
* <pre> throw throwException(clazz,cause); </pre>
|
|
405 |
* @throws SecurityException - if cause is a SecurityException
|
|
406 |
* @throws NotCompliantMBeanException otherwise.
|
|
407 |
**/
|
|
408 |
static NotCompliantMBeanException throwException(Class<?> notCompliant,
|
|
409 |
Throwable cause)
|
|
410 |
throws NotCompliantMBeanException, SecurityException {
|
|
411 |
if (cause instanceof SecurityException)
|
|
412 |
throw (SecurityException) cause;
|
|
413 |
if (cause instanceof NotCompliantMBeanException)
|
|
414 |
throw (NotCompliantMBeanException)cause;
|
|
415 |
final String classname =
|
|
416 |
(notCompliant==null)?"null class":notCompliant.getName();
|
|
417 |
final String reason =
|
|
418 |
(cause==null)?"Not compliant":cause.getMessage();
|
|
419 |
final NotCompliantMBeanException res =
|
|
420 |
new NotCompliantMBeanException(classname+": "+reason);
|
|
421 |
res.initCause(cause);
|
|
422 |
throw res;
|
|
423 |
}
|
|
424 |
|
|
425 |
// Convert a value from an annotation element to a descriptor field value
|
|
426 |
// E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
|
|
427 |
// will produce a Descriptor field value "java.lang.String"
|
|
428 |
private static Object annotationToField(Object x) {
|
|
429 |
// An annotation element cannot have a null value but never mind
|
|
430 |
if (x == null)
|
|
431 |
return null;
|
|
432 |
if (x instanceof Number || x instanceof String ||
|
|
433 |
x instanceof Character || x instanceof Boolean ||
|
|
434 |
x instanceof String[])
|
|
435 |
return x;
|
|
436 |
// Remaining possibilities: array of primitive (e.g. int[]),
|
|
437 |
// enum, class, array of enum or class.
|
|
438 |
Class<?> c = x.getClass();
|
|
439 |
if (c.isArray()) {
|
|
440 |
if (c.getComponentType().isPrimitive())
|
|
441 |
return x;
|
|
442 |
Object[] xx = (Object[]) x;
|
|
443 |
String[] ss = new String[xx.length];
|
|
444 |
for (int i = 0; i < xx.length; i++)
|
|
445 |
ss[i] = (String) annotationToField(xx[i]);
|
|
446 |
return ss;
|
|
447 |
}
|
|
448 |
if (x instanceof Class)
|
|
449 |
return ((Class<?>) x).getName();
|
|
450 |
if (x instanceof Enum)
|
|
451 |
return ((Enum) x).name();
|
|
452 |
// The only other possibility is that the value is another
|
|
453 |
// annotation, or that the language has evolved since this code
|
|
454 |
// was written. We don't allow for either of those currently.
|
|
455 |
throw new IllegalArgumentException("Illegal type for annotation " +
|
|
456 |
"element: " + x.getClass().getName());
|
|
457 |
}
|
|
458 |
|
|
459 |
// This must be consistent with the check for duplicate field values in
|
|
460 |
// ImmutableDescriptor.union. But we don't expect to be called very
|
|
461 |
// often so this inefficient check should be enough.
|
|
462 |
private static boolean equals(Object x, Object y) {
|
|
463 |
return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
|
|
464 |
}
|
|
465 |
|
|
466 |
/**
|
|
467 |
* Returns the XXMBean interface or null if no such interface exists
|
|
468 |
*
|
|
469 |
* @param c The interface to be tested
|
|
470 |
* @param clName The name of the class implementing this interface
|
|
471 |
*/
|
|
472 |
private static Class implementsMBean(Class c, String clName) {
|
|
473 |
String clMBeanName = clName + "MBean";
|
|
474 |
if (c.getName().equals(clMBeanName)) {
|
|
475 |
return c;
|
|
476 |
}
|
|
477 |
Class[] interfaces = c.getInterfaces();
|
|
478 |
for (int i = 0;i < interfaces.length; i++) {
|
|
479 |
if (interfaces[i].getName().equals(clMBeanName))
|
|
480 |
return interfaces[i];
|
|
481 |
}
|
|
482 |
|
|
483 |
return null;
|
|
484 |
}
|
|
485 |
}
|