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