2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 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 |
|
687
|
28 |
import com.sun.jmx.mbeanserver.MBeanIntrospector.MBeanInfoMap;
|
|
29 |
import com.sun.jmx.mbeanserver.MBeanIntrospector.PerInterfaceMap;
|
2
|
30 |
import java.lang.annotation.Annotation;
|
687
|
31 |
import java.lang.ref.WeakReference;
|
2
|
32 |
import java.lang.reflect.GenericArrayType;
|
|
33 |
import java.lang.reflect.InvocationTargetException;
|
|
34 |
import java.lang.reflect.Method;
|
|
35 |
import java.lang.reflect.Type;
|
687
|
36 |
import java.util.Map;
|
|
37 |
import java.util.WeakHashMap;
|
2
|
38 |
import javax.management.Descriptor;
|
|
39 |
import javax.management.ImmutableDescriptor;
|
687
|
40 |
import javax.management.IntrospectionException;
|
|
41 |
import javax.management.JMX;
|
2
|
42 |
import javax.management.MBeanAttributeInfo;
|
|
43 |
import javax.management.MBeanException;
|
|
44 |
import javax.management.MBeanOperationInfo;
|
|
45 |
import javax.management.MBeanParameterInfo;
|
|
46 |
import javax.management.NotCompliantMBeanException;
|
687
|
47 |
import javax.management.openmbean.MXBeanMappingFactory;
|
2
|
48 |
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
|
|
49 |
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
|
|
50 |
import javax.management.openmbean.OpenMBeanParameterInfo;
|
|
51 |
import javax.management.openmbean.OpenMBeanParameterInfoSupport;
|
|
52 |
import javax.management.openmbean.OpenType;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Introspector for MXBeans. There is exactly one instance of this class.
|
|
56 |
*
|
|
57 |
* @since 1.6
|
|
58 |
*/
|
|
59 |
class MXBeanIntrospector extends MBeanIntrospector<ConvertingMethod> {
|
687
|
60 |
/* We keep one MXBeanIntrospector per MXBeanMappingFactory, since the results
|
|
61 |
* of the introspection depend on the factory. The MXBeanIntrospector
|
|
62 |
* has a reference back to the factory, so we wrap it in a WeakReference.
|
|
63 |
* It will be strongly referenced by any MXBeanSupport instances using it;
|
|
64 |
* if there are none then it is OK to gc it.
|
|
65 |
*/
|
|
66 |
private static final
|
|
67 |
Map<MXBeanMappingFactory, WeakReference<MXBeanIntrospector>> map =
|
|
68 |
new WeakHashMap<MXBeanMappingFactory, WeakReference<MXBeanIntrospector>>();
|
2
|
69 |
|
687
|
70 |
static MXBeanIntrospector getInstance(MXBeanMappingFactory factory) {
|
|
71 |
if (factory == null)
|
|
72 |
factory = MXBeanMappingFactory.DEFAULT;
|
|
73 |
synchronized (map) {
|
|
74 |
MXBeanIntrospector intro;
|
|
75 |
WeakReference<MXBeanIntrospector> wr = map.get(factory);
|
|
76 |
if (wr != null) {
|
|
77 |
intro = wr.get();
|
|
78 |
if (intro != null)
|
|
79 |
return intro;
|
|
80 |
}
|
|
81 |
intro = new MXBeanIntrospector(factory);
|
|
82 |
wr = new WeakReference<MXBeanIntrospector>(intro);
|
|
83 |
map.put(factory, wr);
|
|
84 |
return intro;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
private MXBeanIntrospector(MXBeanMappingFactory factory) {
|
|
89 |
this.mappingFactory = factory;
|
2
|
90 |
}
|
|
91 |
|
|
92 |
@Override
|
|
93 |
PerInterfaceMap<ConvertingMethod> getPerInterfaceMap() {
|
|
94 |
return perInterfaceMap;
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
|
98 |
MBeanInfoMap getMBeanInfoMap() {
|
|
99 |
return mbeanInfoMap;
|
|
100 |
}
|
|
101 |
|
|
102 |
@Override
|
|
103 |
MBeanAnalyzer<ConvertingMethod> getAnalyzer(Class<?> mbeanInterface)
|
|
104 |
throws NotCompliantMBeanException {
|
|
105 |
return MBeanAnalyzer.analyzer(mbeanInterface, this);
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
boolean isMXBean() {
|
|
110 |
return true;
|
|
111 |
}
|
|
112 |
|
|
113 |
@Override
|
|
114 |
ConvertingMethod mFrom(Method m) {
|
687
|
115 |
return ConvertingMethod.from(m, mappingFactory);
|
2
|
116 |
}
|
|
117 |
|
|
118 |
@Override
|
|
119 |
String getName(ConvertingMethod m) {
|
|
120 |
return m.getName();
|
|
121 |
}
|
|
122 |
|
|
123 |
@Override
|
|
124 |
Type getGenericReturnType(ConvertingMethod m) {
|
|
125 |
return m.getGenericReturnType();
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
Type[] getGenericParameterTypes(ConvertingMethod m) {
|
|
130 |
return m.getGenericParameterTypes();
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
String[] getSignature(ConvertingMethod m) {
|
|
135 |
return m.getOpenSignature();
|
|
136 |
}
|
|
137 |
|
|
138 |
@Override
|
|
139 |
void checkMethod(ConvertingMethod m) {
|
|
140 |
m.checkCallFromOpen();
|
|
141 |
}
|
|
142 |
|
|
143 |
@Override
|
|
144 |
Object invokeM2(ConvertingMethod m, Object target, Object[] args,
|
|
145 |
Object cookie)
|
|
146 |
throws InvocationTargetException, IllegalAccessException,
|
|
147 |
MBeanException {
|
|
148 |
return m.invokeWithOpenReturn((MXBeanLookup) cookie, target, args);
|
|
149 |
}
|
|
150 |
|
|
151 |
@Override
|
|
152 |
boolean validParameter(ConvertingMethod m, Object value, int paramNo,
|
|
153 |
Object cookie) {
|
|
154 |
if (value == null) {
|
|
155 |
// Null is a valid value for all OpenTypes, even though
|
|
156 |
// OpenType.isValue(null) will return false. It can always be
|
|
157 |
// matched to the corresponding Java type, except when that
|
|
158 |
// type is primitive.
|
|
159 |
Type t = m.getGenericParameterTypes()[paramNo];
|
|
160 |
return (!(t instanceof Class) || !((Class) t).isPrimitive());
|
|
161 |
} else {
|
|
162 |
Object v;
|
|
163 |
try {
|
|
164 |
v = m.fromOpenParameter((MXBeanLookup) cookie, value, paramNo);
|
|
165 |
} catch (Exception e) {
|
|
166 |
// Ignore the exception and let MBeanIntrospector.invokeSetter()
|
|
167 |
// throw the initial exception.
|
|
168 |
return true;
|
|
169 |
}
|
|
170 |
return isValidParameter(m.getMethod(), v, paramNo);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
@Override
|
|
175 |
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
|
687
|
176 |
ConvertingMethod getter, ConvertingMethod setter)
|
|
177 |
throws IntrospectionException {
|
2
|
178 |
|
|
179 |
final boolean isReadable = (getter != null);
|
|
180 |
final boolean isWritable = (setter != null);
|
|
181 |
final boolean isIs = isReadable && getName(getter).startsWith("is");
|
|
182 |
|
|
183 |
final String description = attributeName;
|
|
184 |
|
|
185 |
final OpenType<?> openType;
|
|
186 |
final Type originalType;
|
|
187 |
if (isReadable) {
|
|
188 |
openType = getter.getOpenReturnType();
|
|
189 |
originalType = getter.getGenericReturnType();
|
|
190 |
} else {
|
|
191 |
openType = setter.getOpenParameterTypes()[0];
|
|
192 |
originalType = setter.getGenericParameterTypes()[0];
|
|
193 |
}
|
|
194 |
Descriptor descriptor = typeDescriptor(openType, originalType);
|
|
195 |
if (isReadable) {
|
|
196 |
descriptor = ImmutableDescriptor.union(descriptor,
|
|
197 |
getter.getDescriptor());
|
|
198 |
}
|
|
199 |
if (isWritable) {
|
|
200 |
descriptor = ImmutableDescriptor.union(descriptor,
|
|
201 |
setter.getDescriptor());
|
|
202 |
}
|
|
203 |
|
|
204 |
final MBeanAttributeInfo ai;
|
|
205 |
if (canUseOpenInfo(originalType)) {
|
|
206 |
ai = new OpenMBeanAttributeInfoSupport(attributeName,
|
|
207 |
description,
|
|
208 |
openType,
|
|
209 |
isReadable,
|
|
210 |
isWritable,
|
|
211 |
isIs,
|
|
212 |
descriptor);
|
|
213 |
} else {
|
|
214 |
ai = new MBeanAttributeInfo(attributeName,
|
|
215 |
originalTypeString(originalType),
|
|
216 |
description,
|
|
217 |
isReadable,
|
|
218 |
isWritable,
|
|
219 |
isIs,
|
|
220 |
descriptor);
|
|
221 |
}
|
|
222 |
// could also consult annotations for defaultValue,
|
|
223 |
// minValue, maxValue, legalValues
|
|
224 |
|
|
225 |
return ai;
|
|
226 |
}
|
|
227 |
|
|
228 |
@Override
|
|
229 |
MBeanOperationInfo getMBeanOperationInfo(String operationName,
|
|
230 |
ConvertingMethod operation) {
|
|
231 |
final Method method = operation.getMethod();
|
|
232 |
final String description = operationName;
|
|
233 |
/* Ideally this would be an empty string, but
|
|
234 |
OMBOperationInfo constructor forbids that. Also, we
|
|
235 |
could consult an annotation to get a useful
|
|
236 |
description. */
|
|
237 |
|
|
238 |
final int impact = MBeanOperationInfo.UNKNOWN;
|
|
239 |
|
|
240 |
final OpenType<?> returnType = operation.getOpenReturnType();
|
|
241 |
final Type originalReturnType = operation.getGenericReturnType();
|
|
242 |
final OpenType<?>[] paramTypes = operation.getOpenParameterTypes();
|
|
243 |
final Type[] originalParamTypes = operation.getGenericParameterTypes();
|
|
244 |
final MBeanParameterInfo[] params =
|
|
245 |
new MBeanParameterInfo[paramTypes.length];
|
|
246 |
boolean openReturnType = canUseOpenInfo(originalReturnType);
|
|
247 |
boolean openParameterTypes = true;
|
|
248 |
Annotation[][] annots = method.getParameterAnnotations();
|
|
249 |
for (int i = 0; i < paramTypes.length; i++) {
|
|
250 |
final String paramName = "p" + i;
|
|
251 |
final String paramDescription = paramName;
|
|
252 |
final OpenType<?> openType = paramTypes[i];
|
|
253 |
final Type originalType = originalParamTypes[i];
|
|
254 |
Descriptor descriptor =
|
|
255 |
typeDescriptor(openType, originalType);
|
|
256 |
descriptor = ImmutableDescriptor.union(descriptor,
|
|
257 |
Introspector.descriptorForAnnotations(annots[i]));
|
|
258 |
final MBeanParameterInfo pi;
|
|
259 |
if (canUseOpenInfo(originalType)) {
|
687
|
260 |
pi = new OpenMBeanParameterInfoSupport(paramName,
|
2
|
261 |
paramDescription,
|
|
262 |
openType,
|
|
263 |
descriptor);
|
|
264 |
} else {
|
|
265 |
openParameterTypes = false;
|
|
266 |
pi = new MBeanParameterInfo(
|
687
|
267 |
paramName,
|
2
|
268 |
originalTypeString(originalType),
|
|
269 |
paramDescription,
|
|
270 |
descriptor);
|
|
271 |
}
|
|
272 |
params[i] = pi;
|
|
273 |
}
|
|
274 |
|
|
275 |
Descriptor descriptor =
|
|
276 |
typeDescriptor(returnType, originalReturnType);
|
|
277 |
descriptor = ImmutableDescriptor.union(descriptor,
|
|
278 |
Introspector.descriptorForElement(method));
|
|
279 |
final MBeanOperationInfo oi;
|
|
280 |
if (openReturnType && openParameterTypes) {
|
|
281 |
/* If the return value and all the parameters can be faithfully
|
|
282 |
* represented as OpenType then we return an OpenMBeanOperationInfo.
|
|
283 |
* If any of them is a primitive type, we can't. Compatibility
|
|
284 |
* with JSR 174 means that we must return an MBean*Info where
|
|
285 |
* the getType() is the primitive type, not its wrapped type as
|
|
286 |
* we would get with an OpenMBean*Info. The OpenType is available
|
|
287 |
* in the Descriptor in either case.
|
|
288 |
*/
|
|
289 |
final OpenMBeanParameterInfo[] oparams =
|
|
290 |
new OpenMBeanParameterInfo[params.length];
|
|
291 |
System.arraycopy(params, 0, oparams, 0, params.length);
|
|
292 |
oi = new OpenMBeanOperationInfoSupport(operationName,
|
|
293 |
description,
|
|
294 |
oparams,
|
|
295 |
returnType,
|
|
296 |
impact,
|
|
297 |
descriptor);
|
|
298 |
} else {
|
|
299 |
oi = new MBeanOperationInfo(operationName,
|
|
300 |
description,
|
|
301 |
params,
|
|
302 |
openReturnType ?
|
|
303 |
returnType.getClassName() :
|
|
304 |
originalTypeString(originalReturnType),
|
|
305 |
impact,
|
|
306 |
descriptor);
|
|
307 |
}
|
|
308 |
|
|
309 |
return oi;
|
|
310 |
}
|
|
311 |
|
|
312 |
@Override
|
|
313 |
Descriptor getBasicMBeanDescriptor() {
|
|
314 |
return new ImmutableDescriptor("mxbean=true",
|
|
315 |
"immutableInfo=true");
|
|
316 |
}
|
|
317 |
|
|
318 |
@Override
|
|
319 |
Descriptor getMBeanDescriptor(Class<?> resourceClass) {
|
|
320 |
/* We already have immutableInfo=true in the Descriptor
|
|
321 |
* included in the MBeanInfo for the MXBean interface. This
|
|
322 |
* method is being called for the MXBean *class* to add any
|
|
323 |
* new items beyond those in the interface Descriptor, which
|
|
324 |
* currently it does not.
|
|
325 |
*/
|
|
326 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR;
|
|
327 |
}
|
|
328 |
|
687
|
329 |
@Override
|
|
330 |
Descriptor getSpecificMBeanDescriptor() {
|
|
331 |
if (mappingFactory == MXBeanMappingFactory.DEFAULT)
|
|
332 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR;
|
|
333 |
else {
|
|
334 |
return new ImmutableDescriptor(
|
|
335 |
JMX.MXBEAN_MAPPING_FACTORY_CLASS_FIELD + "=" +
|
|
336 |
mappingFactory.getClass().getName());
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
2
|
340 |
private static Descriptor typeDescriptor(OpenType openType,
|
|
341 |
Type originalType) {
|
|
342 |
return new ImmutableDescriptor(
|
|
343 |
new String[] {"openType",
|
|
344 |
"originalType"},
|
|
345 |
new Object[] {openType,
|
|
346 |
originalTypeString(originalType)});
|
|
347 |
}
|
|
348 |
|
|
349 |
/**
|
|
350 |
* <p>True if this type can be faithfully represented in an
|
|
351 |
* OpenMBean*Info.</p>
|
|
352 |
*
|
|
353 |
* <p>Compatibility with JSR 174 means that primitive types must be
|
|
354 |
* represented by an MBean*Info whose getType() is the primitive type
|
|
355 |
* string, e.g. "int". If we used an OpenMBean*Info then this string
|
|
356 |
* would be the wrapped type, e.g. "java.lang.Integer".</p>
|
|
357 |
*
|
|
358 |
* <p>Compatibility with JMX 1.2 (including J2SE 5.0) means that arrays
|
|
359 |
* of primitive types cannot use an ArrayType representing an array of
|
|
360 |
* primitives, because that didn't exist in JMX 1.2.</p>
|
|
361 |
*/
|
|
362 |
private static boolean canUseOpenInfo(Type type) {
|
|
363 |
if (type instanceof GenericArrayType) {
|
|
364 |
return canUseOpenInfo(
|
|
365 |
((GenericArrayType) type).getGenericComponentType());
|
|
366 |
} else if (type instanceof Class && ((Class<?>) type).isArray()) {
|
|
367 |
return canUseOpenInfo(
|
|
368 |
((Class<?>) type).getComponentType());
|
|
369 |
}
|
|
370 |
return (!(type instanceof Class && ((Class<?>) type).isPrimitive()));
|
|
371 |
}
|
|
372 |
|
|
373 |
private static String originalTypeString(Type type) {
|
|
374 |
if (type instanceof Class)
|
|
375 |
return ((Class) type).getName();
|
|
376 |
else
|
|
377 |
return type.toString();
|
|
378 |
}
|
|
379 |
|
687
|
380 |
private final PerInterfaceMap<ConvertingMethod>
|
2
|
381 |
perInterfaceMap = new PerInterfaceMap<ConvertingMethod>();
|
|
382 |
|
687
|
383 |
private final MBeanInfoMap mbeanInfoMap = new MBeanInfoMap();
|
|
384 |
|
|
385 |
private final MXBeanMappingFactory mappingFactory;
|
2
|
386 |
}
|