-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
MetadataMBeanInfoAssembler.java
451 lines (395 loc) · 16.8 KB
/
MetadataMBeanInfoAssembler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jmx.export.assembler;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import javax.management.Descriptor;
import javax.management.MBeanParameterInfo;
import javax.management.modelmbean.ModelMBeanNotificationInfo;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jmx.export.metadata.InvalidMetadataException;
import org.springframework.jmx.export.metadata.JmxAttributeSource;
import org.springframework.jmx.export.metadata.JmxMetadataUtils;
import org.springframework.jmx.export.metadata.ManagedAttribute;
import org.springframework.jmx.export.metadata.ManagedMetric;
import org.springframework.jmx.export.metadata.ManagedNotification;
import org.springframework.jmx.export.metadata.ManagedOperation;
import org.springframework.jmx.export.metadata.ManagedOperationParameter;
import org.springframework.jmx.export.metadata.ManagedResource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Implementation of the {@link MBeanInfoAssembler} interface that reads
* the management interface information from source level metadata.
*
* <p>Uses the {@link JmxAttributeSource} strategy interface, so that
* metadata can be read using any supported implementation. Out of the box,
* Spring provides an implementation based on annotations:
* {@code AnnotationJmxAttributeSource}.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Jennifer Hickey
* @since 1.2
* @see #setAttributeSource
* @see org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource
*/
public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler
implements AutodetectCapableMBeanInfoAssembler, InitializingBean {
@Nullable
private JmxAttributeSource attributeSource;
/**
* Create a new {@code MetadataMBeanInfoAssembler} which needs to be
* configured through the {@link #setAttributeSource} method.
*/
public MetadataMBeanInfoAssembler() {
}
/**
* Create a new {@code MetadataMBeanInfoAssembler} for the given
* {@code JmxAttributeSource}.
* @param attributeSource the JmxAttributeSource to use
*/
public MetadataMBeanInfoAssembler(JmxAttributeSource attributeSource) {
Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
this.attributeSource = attributeSource;
}
/**
* Set the {@code JmxAttributeSource} implementation to use for
* reading the metadata from the bean class.
* @see org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource
*/
public void setAttributeSource(JmxAttributeSource attributeSource) {
Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
this.attributeSource = attributeSource;
}
@Override
public void afterPropertiesSet() {
if (this.attributeSource == null) {
throw new IllegalArgumentException("Property 'attributeSource' is required");
}
}
private JmxAttributeSource obtainAttributeSource() {
Assert.state(this.attributeSource != null, "No JmxAttributeSource set");
return this.attributeSource;
}
/**
* Throws an IllegalArgumentException if it encounters a JDK dynamic proxy.
* Metadata can only be read from target classes and CGLIB proxies!
*/
@Override
protected void checkManagedBean(Object managedBean) throws IllegalArgumentException {
if (AopUtils.isJdkDynamicProxy(managedBean)) {
throw new IllegalArgumentException(
"MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " +
"export the target beans directly or use CGLIB proxies instead");
}
}
/**
* Used for auto-detection of beans. Checks to see if the bean's class has a
* {@code ManagedResource} attribute. If so, it will add it to the list of included beans.
* @param beanClass the class of the bean
* @param beanName the name of the bean in the bean factory
*/
@Override
public boolean includeBean(Class<?> beanClass, String beanName) {
return (obtainAttributeSource().getManagedResource(getClassToExpose(beanClass)) != null);
}
/**
* Vote on the inclusion of an attribute accessor.
* @param method the accessor method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
@Override
protected boolean includeReadAttribute(Method method, String beanKey) {
return hasManagedAttribute(method) || hasManagedMetric(method);
}
/**
* Votes on the inclusion of an attribute mutator.
* @param method the mutator method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
@Override
protected boolean includeWriteAttribute(Method method, String beanKey) {
return hasManagedAttribute(method);
}
/**
* Votes on the inclusion of an operation.
* @param method the operation method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
@Override
protected boolean includeOperation(Method method, String beanKey) {
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
return (pd != null && hasManagedAttribute(method)) || hasManagedOperation(method);
}
/**
* Checks to see if the given Method has the {@code ManagedAttribute} attribute.
*/
private boolean hasManagedAttribute(Method method) {
return (obtainAttributeSource().getManagedAttribute(method) != null);
}
/**
* Checks to see if the given Method has the {@code ManagedMetric} attribute.
*/
private boolean hasManagedMetric(Method method) {
return (obtainAttributeSource().getManagedMetric(method) != null);
}
/**
* Checks to see if the given Method has the {@code ManagedOperation} attribute.
* @param method the method to check
*/
private boolean hasManagedOperation(Method method) {
return (obtainAttributeSource().getManagedOperation(method) != null);
}
/**
* Reads managed resource description from the source level metadata.
* Returns an empty {@code String} if no description can be found.
*/
@Override
protected String getDescription(Object managedBean, String beanKey) {
ManagedResource mr = obtainAttributeSource().getManagedResource(getClassToExpose(managedBean));
return (mr != null ? mr.getDescription() : "");
}
/**
* Creates a description for the attribute corresponding to this property
* descriptor. Attempts to create the description using metadata from either
* the getter or setter attributes, otherwise uses the property name.
*/
@Override
protected String getAttributeDescription(PropertyDescriptor propertyDescriptor, String beanKey) {
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
ManagedAttribute getter =
(readMethod != null ? obtainAttributeSource().getManagedAttribute(readMethod) : null);
ManagedAttribute setter =
(writeMethod != null ? obtainAttributeSource().getManagedAttribute(writeMethod) : null);
if (getter != null && StringUtils.hasText(getter.getDescription())) {
return getter.getDescription();
}
else if (setter != null && StringUtils.hasText(setter.getDescription())) {
return setter.getDescription();
}
ManagedMetric metric = (readMethod != null ? obtainAttributeSource().getManagedMetric(readMethod) : null);
if (metric != null && StringUtils.hasText(metric.getDescription())) {
return metric.getDescription();
}
return propertyDescriptor.getDisplayName();
}
/**
* Retrieves the description for the supplied {@code Method} from the
* metadata. Uses the method name is no description is present in the metadata.
*/
@Override
protected String getOperationDescription(Method method, String beanKey) {
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
if (pd != null) {
ManagedAttribute ma = obtainAttributeSource().getManagedAttribute(method);
if (ma != null && StringUtils.hasText(ma.getDescription())) {
return ma.getDescription();
}
ManagedMetric metric = obtainAttributeSource().getManagedMetric(method);
if (metric != null && StringUtils.hasText(metric.getDescription())) {
return metric.getDescription();
}
return method.getName();
}
else {
ManagedOperation mo = obtainAttributeSource().getManagedOperation(method);
if (mo != null && StringUtils.hasText(mo.getDescription())) {
return mo.getDescription();
}
return method.getName();
}
}
/**
* Reads {@code MBeanParameterInfo} from the {@code ManagedOperationParameter}
* attributes attached to a method. Returns an empty array of {@code MBeanParameterInfo}
* if no attributes are found.
*/
@Override
protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) {
ManagedOperationParameter[] params = obtainAttributeSource().getManagedOperationParameters(method);
if (ObjectUtils.isEmpty(params)) {
return super.getOperationParameters(method, beanKey);
}
MBeanParameterInfo[] parameterInfo = new MBeanParameterInfo[params.length];
Class<?>[] methodParameters = method.getParameterTypes();
for (int i = 0; i < params.length; i++) {
ManagedOperationParameter param = params[i];
parameterInfo[i] =
new MBeanParameterInfo(param.getName(), methodParameters[i].getName(), param.getDescription());
}
return parameterInfo;
}
/**
* Reads the {@link ManagedNotification} metadata from the {@code Class} of the managed resource
* and generates and returns the corresponding {@link ModelMBeanNotificationInfo} metadata.
*/
@Override
protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) {
ManagedNotification[] notificationAttributes =
obtainAttributeSource().getManagedNotifications(getClassToExpose(managedBean));
ModelMBeanNotificationInfo[] notificationInfos =
new ModelMBeanNotificationInfo[notificationAttributes.length];
for (int i = 0; i < notificationAttributes.length; i++) {
ManagedNotification attribute = notificationAttributes[i];
notificationInfos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(attribute);
}
return notificationInfos;
}
/**
* Adds descriptor fields from the {@code ManagedResource} attribute
* to the MBean descriptor. Specifically, adds the {@code currencyTimeLimit},
* {@code persistPolicy}, {@code persistPeriod}, {@code persistLocation}
* and {@code persistName} descriptor fields if they are present in the metadata.
*/
@Override
protected void populateMBeanDescriptor(Descriptor desc, Object managedBean, String beanKey) {
ManagedResource mr = obtainAttributeSource().getManagedResource(getClassToExpose(managedBean));
if (mr == null) {
throw new InvalidMetadataException(
"No ManagedResource attribute found for class: " + getClassToExpose(managedBean));
}
applyCurrencyTimeLimit(desc, mr.getCurrencyTimeLimit());
if (mr.isLog()) {
desc.setField(FIELD_LOG, "true");
}
if (StringUtils.hasLength(mr.getLogFile())) {
desc.setField(FIELD_LOG_FILE, mr.getLogFile());
}
if (StringUtils.hasLength(mr.getPersistPolicy())) {
desc.setField(FIELD_PERSIST_POLICY, mr.getPersistPolicy());
}
if (mr.getPersistPeriod() >= 0) {
desc.setField(FIELD_PERSIST_PERIOD, Integer.toString(mr.getPersistPeriod()));
}
if (StringUtils.hasLength(mr.getPersistName())) {
desc.setField(FIELD_PERSIST_NAME, mr.getPersistName());
}
if (StringUtils.hasLength(mr.getPersistLocation())) {
desc.setField(FIELD_PERSIST_LOCATION, mr.getPersistLocation());
}
}
/**
* Adds descriptor fields from the {@code ManagedAttribute} attribute or the {@code ManagedMetric} attribute
* to the attribute descriptor.
*/
@Override
protected void populateAttributeDescriptor(
Descriptor desc, @Nullable Method getter, @Nullable Method setter, String beanKey) {
if (getter != null) {
ManagedMetric metric = obtainAttributeSource().getManagedMetric(getter);
if (metric != null) {
populateMetricDescriptor(desc, metric);
return;
}
}
ManagedAttribute gma = (getter != null ? obtainAttributeSource().getManagedAttribute(getter) : null);
ManagedAttribute sma = (setter != null ? obtainAttributeSource().getManagedAttribute(setter) : null);
populateAttributeDescriptor(desc,
(gma != null ? gma : ManagedAttribute.EMPTY),
(sma != null ? sma : ManagedAttribute.EMPTY));
}
private void populateAttributeDescriptor(Descriptor desc, ManagedAttribute gma, ManagedAttribute sma) {
applyCurrencyTimeLimit(desc, resolveIntDescriptor(gma.getCurrencyTimeLimit(), sma.getCurrencyTimeLimit()));
Object defaultValue = resolveObjectDescriptor(gma.getDefaultValue(), sma.getDefaultValue());
desc.setField(FIELD_DEFAULT, defaultValue);
String persistPolicy = resolveStringDescriptor(gma.getPersistPolicy(), sma.getPersistPolicy());
if (StringUtils.hasLength(persistPolicy)) {
desc.setField(FIELD_PERSIST_POLICY, persistPolicy);
}
int persistPeriod = resolveIntDescriptor(gma.getPersistPeriod(), sma.getPersistPeriod());
if (persistPeriod >= 0) {
desc.setField(FIELD_PERSIST_PERIOD, Integer.toString(persistPeriod));
}
}
private void populateMetricDescriptor(Descriptor desc, ManagedMetric metric) {
applyCurrencyTimeLimit(desc, metric.getCurrencyTimeLimit());
if (StringUtils.hasLength(metric.getPersistPolicy())) {
desc.setField(FIELD_PERSIST_POLICY, metric.getPersistPolicy());
}
if (metric.getPersistPeriod() >= 0) {
desc.setField(FIELD_PERSIST_PERIOD, Integer.toString(metric.getPersistPeriod()));
}
if (StringUtils.hasLength(metric.getDisplayName())) {
desc.setField(FIELD_DISPLAY_NAME, metric.getDisplayName());
}
if (StringUtils.hasLength(metric.getUnit())) {
desc.setField(FIELD_UNITS, metric.getUnit());
}
if (StringUtils.hasLength(metric.getCategory())) {
desc.setField(FIELD_METRIC_CATEGORY, metric.getCategory());
}
desc.setField(FIELD_METRIC_TYPE, metric.getMetricType().toString());
}
/**
* Adds descriptor fields from the {@code ManagedAttribute} attribute
* to the attribute descriptor. Specifically, adds the {@code currencyTimeLimit}
* descriptor field if it is present in the metadata.
*/
@Override
protected void populateOperationDescriptor(Descriptor desc, Method method, String beanKey) {
ManagedOperation mo = obtainAttributeSource().getManagedOperation(method);
if (mo != null) {
applyCurrencyTimeLimit(desc, mo.getCurrencyTimeLimit());
}
}
/**
* Determines which of two {@code int} values should be used as the value
* for an attribute descriptor. In general, only the getter or the setter will
* have a non-negative value, so we use that value. In the event that both values
* are non-negative, we use the greater of the two. This method can be used to
* resolve any {@code int} valued descriptor where there are two possible values.
* @param getter the int value associated with the getter for this attribute
* @param setter the int associated with the setter for this attribute
*/
private int resolveIntDescriptor(int getter, int setter) {
return Math.max(getter, setter);
}
/**
* Locates the value of a descriptor based on values attached
* to both the getter and setter methods. If both have values
* supplied then the value attached to the getter is preferred.
* @param getter the Object value associated with the get method
* @param setter the Object value associated with the set method
* @return the appropriate Object to use as the value for the descriptor
*/
@Nullable
private Object resolveObjectDescriptor(@Nullable Object getter, @Nullable Object setter) {
return (getter != null ? getter : setter);
}
/**
* Locates the value of a descriptor based on values attached
* to both the getter and setter methods. If both have values
* supplied then the value attached to the getter is preferred.
* The supplied default value is used to check to see if the value
* associated with the getter has changed from the default.
* @param getter the String value associated with the get method
* @param setter the String value associated with the set method
* @return the appropriate String to use as the value for the descriptor
*/
@Nullable
private String resolveStringDescriptor(@Nullable String getter, @Nullable String setter) {
return (StringUtils.hasLength(getter) ? getter : setter);
}
}