-
Notifications
You must be signed in to change notification settings - Fork 470
/
ResourceDMBean.java
executable file
·663 lines (562 loc) · 25.5 KB
/
ResourceDMBean.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package org.jgroups.jmx;
import org.jgroups.annotations.MBean;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.ManagedOperation;
import org.jgroups.annotations.Property;
import org.jgroups.conf.AttributeType;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.util.Average;
import org.jgroups.util.Util;
import javax.management.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
/**
*
* A DynamicMBean wrapping an annotated object instance and exposing attributes annotated with @ManagedAttribute and
* operations annotated with @ManagedOperation.
*
* @author Chris Mills
* @author Vladimir Blagojevic
* @author Bela Ban
* @see org.jgroups.annotations.ManagedAttribute
* @see org.jgroups.annotations.ManagedOperation
* @see org.jgroups.annotations.MBean
*
*/
public class ResourceDMBean implements DynamicMBean {
protected static final Class<?>[] primitives= {int.class, byte.class, short.class, long.class,
float.class, double.class, boolean.class, char.class };
protected static final List<Method> OBJECT_METHODS; // all methods of java.lang.Object
protected final boolean expose_all;
protected final Log log=LogFactory.getLog(ResourceDMBean.class);
protected final Object obj;
protected List<Object> objs;
protected final MBeanAttributeInfo[] attrInfo;
protected final MBeanOperationInfo[] opInfo;
protected final HashMap<String,AttributeEntry> atts=new HashMap<>();
protected final List<MBeanOperationInfo> ops=new ArrayList<>();
static {OBJECT_METHODS=new ArrayList<>(Arrays.asList(Object.class.getMethods()));}
protected static final Predicate<AccessibleObject> FILTER=obj -> obj.isAnnotationPresent(ManagedAttribute.class) ||
(obj.isAnnotationPresent(Property.class) && obj.getAnnotation(Property.class).exposeAsManagedAttribute());
public ResourceDMBean(Object instance) {
this(instance, null);
}
public ResourceDMBean(Object instance, Predicate<AccessibleObject> filter) {
if(instance == null)
throw new NullPointerException("Cannot make an MBean wrapper for null instance");
this.obj=instance;
Class<? extends Object> c=obj.getClass();
expose_all=c.isAnnotationPresent(MBean.class) && c.getAnnotation(MBean.class).exposeAll();
findFields(instance, filter);
findMethods(instance, filter);
fixFields(instance);
List<Object> objects=Util.getComponents(instance);
if(objects != null) {
for(Object inst: objects) {
if(inst != null) {
if(objs == null)
objs=new ArrayList<>();
objs.add(inst);
findFields(inst, filter);
findMethods(inst, filter);
fixFields(inst);
}
}
}
attrInfo=new MBeanAttributeInfo[atts.size()];
int i=0;
MBeanAttributeInfo info=null;
for(AttributeEntry entry: atts.values()) {
info=entry.info;
attrInfo[i++]=info;
}
opInfo=new MBeanOperationInfo[ops.size()];
ops.toArray(opInfo);
}
public MBeanInfo getMBeanInfo() {
return new MBeanInfo(obj.getClass().getCanonicalName(), "DynamicMBean", attrInfo, null, opInfo, null);
}
public void forAllAttributes(BiConsumer<String,AttributeEntry> c) {
if(c == null)
return;
for(Map.Entry<String,AttributeEntry> e: atts.entrySet()) {
c.accept(e.getKey(), e.getValue());
}
}
public Object getAttribute(String name) {
if(name == null || name.isEmpty())
throw new NullPointerException("Invalid attribute requested " + name);
Attribute attr=getNamedAttribute(name);
return attr != null? attr.getValue() : null;
}
public void setAttribute(Attribute attribute) {
if(attribute == null || attribute.getName() == null)
throw new NullPointerException("Invalid attribute requested " + attribute);
setNamedAttribute(attribute);
}
public AttributeList getAttributes(String[] names) {
AttributeList al=new AttributeList();
for(String name: names) {
Attribute attr=getNamedAttribute(name);
if(attr != null)
al.add(attr);
else
log.warn("Did not find attribute " + name);
}
return al;
}
public AttributeList setAttributes(AttributeList list) {
AttributeList results=new AttributeList();
for(int i=0;i < list.size();i++) {
Attribute attr=(Attribute)list.get(i);
if(setNamedAttribute(attr))
results.add(attr);
else {
if(log.isWarnEnabled())
log.warn("Failed to update attribute name " + attr.getName() + " with value " + attr.getValue());
}
}
return results;
}
public Object invoke(String name, Object[] args, String[] sig) throws MBeanException, ReflectionException {
try {
Class<?>[] classes=new Class[sig.length];
for(int i=0;i < classes.length;i++)
classes[i]=getClassForName(sig[i]);
Method method=null;
if(objs != null) {
for(Object o: objs) {
try {
method=o.getClass().getMethod(name, classes);
}
catch(Throwable t) {
continue;
}
return method.invoke(o, args);
}
}
method=obj.getClass().getMethod(name, classes);
return method.invoke(obj, args);
}
catch(Exception e) {
throw new MBeanException(e);
}
}
public static boolean isSetMethod(Method method) {
return method.getParameterCount() == 1;
}
public static boolean isGetMethod(Method method) {
return method.getParameterTypes().length == 0 && method.getReturnType() != Void.TYPE;
}
public static boolean isIsMethod(Method method) {
return method.getParameterTypes().length == 0 &&
(method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class);
}
public static void dumpStats(Object obj, final Map<String,Object> map) {
dumpStats(obj, "", map);
}
public static void dumpStats(Object obj, String prefix, final Map<String,Object> map) {
BiConsumer<Field,Object> field_func=(f,o) -> {
String attr_name=null;
try {
f.setAccessible(true);
Object value=f.get(o);
attr_name=Util.getNameFromAnnotation(f);
if(attr_name != null && !attr_name.trim().isEmpty())
attr_name=attr_name.trim();
else
attr_name=f.getName();
if(prefix != null && !prefix.isEmpty())
attr_name=prefix + "." + attr_name;
map.put(attr_name, prettyPrint(value, f));
}
catch(Exception e) {
throw new RuntimeException(String.format("could not read value of attribute \"%s\"", attr_name), e);
}
};
BiConsumer<Method,Object> getter_func=(m,o) -> {
String method_name=null;
if(!ResourceDMBean.isGetMethod(m))
return;
try {
Object value=m.invoke(o);
method_name=Util.getNameFromAnnotation(m);
if(method_name != null && !method_name.trim().isEmpty())
method_name=method_name.trim();
else {
String field_name=Util.methodNameToAttributeName(m.getName());
method_name=Util.attributeNameToMethodName(field_name);
}
String attributeName=Util.methodNameToAttributeName(method_name);
if(prefix != null && !prefix.isEmpty())
attributeName=prefix + "." + attributeName;
map.put(attributeName, prettyPrint(value, m));
}
catch(Exception e) {
throw new RuntimeException(String.format("could not invoke getter method \"%s\"", method_name),e);
}
};
Util.forAllFieldsAndMethods(obj, FILTER, field_func, getter_func);
}
@SuppressWarnings("MalformedFormatString")
public static String prettyPrint(Object val, AccessibleObject a) {
if(val == null) return "null";
Class<?> cl=val.getClass();
AttributeType type=getType(a);
if(type != null) {
switch(type) {
case BYTES:
return Util.printBytes(((Number)val).doubleValue());
case TIME:
TimeUnit unit=getTimeUnit(a);
return Util.printTime(((Number)val).doubleValue(), unit);
case SCALAR:
if(isNumeric(cl))
return String.format("%,d", val);
if(cl.equals(LongAdder.class))
return String.format("%,d", ((LongAdder)val).sum());
if(val instanceof Number)
return String.format("%,d", ((Number)val).longValue());
break;
}
}
if(isNumeric(cl))
return String.format("%d", val);
if(isFractional(cl))
return String.format("%,.2f", val);
if(cl.equals(String[].class))
return Arrays.toString((String[])val);
return val.toString();
}
public static boolean isNumeric(Class<?> cl) {
return cl.equals(short.class) || cl.equals(Short.class) || cl.equals(int.class) || cl.equals(Integer.class)
|| cl.equals(long.class) || cl.equals(Long.class);
}
public static boolean isFractional(Class<?> cl) {
return cl.equals(float.class) || cl.equals(Float.class) || cl.equals(double.class) || cl.equals(Double.class);
}
public static boolean isNumber(Class<?> cl) {
return isNumeric(cl) || isFractional(cl) || Number.class.isAssignableFrom(cl) || Average.class.isAssignableFrom(cl);
}
protected static AttributeType getType(AccessibleObject ao) {
Property prop=ao.getAnnotation(Property.class);
if(prop != null)
return prop.type();
ManagedAttribute attr=ao.getAnnotation(ManagedAttribute.class);
return attr != null? attr.type() : null;
}
protected static TimeUnit getTimeUnit(AccessibleObject ao) {
Property prop=ao.getAnnotation(Property.class);
if(prop != null)
return prop.unit();
ManagedAttribute attr=ao.getAnnotation(ManagedAttribute.class);
return attr != null? attr.unit() : null;
}
protected static Class<?> getClassForName(String name) throws ClassNotFoundException {
try {
return Class.forName(name);
}
catch(ClassNotFoundException cnfe) {
//Could be a primitive - let's check
for(int i=0;i < primitives.length;i++) {
if(name.equals(primitives[i].getName())) {
return primitives[i];
}
}
}
throw new ClassNotFoundException("Class " + name + " cannot be found");
}
protected void findMethods(Object instance, Predicate<AccessibleObject> filter) {
// find all methods but don't include methods from Object class
List<Method> methods = new ArrayList<>(Arrays.asList(instance.getClass().getMethods()));
methods.removeAll(OBJECT_METHODS);
for(Method method: methods) {
// does method have @ManagedAttribute annotation?
if(method.isAnnotationPresent(ManagedAttribute.class) || method.isAnnotationPresent(Property.class)) {
if(filter != null && !filter.test(method))
continue;
exposeManagedAttribute(method, instance);
}
//or @ManagedOperation
else if (method.isAnnotationPresent(ManagedOperation.class) || expose_all){
ManagedOperation op=method.getAnnotation(ManagedOperation.class);
ops.add(new MBeanOperationInfo(op != null? op.description() : "",method));
}
}
}
/** Provides field-based getter and/or setters for all attributes in attrs if not present */
protected void fixFields(Object instance) {
for(AttributeEntry attr: atts.values()) {
if(attr.getter == null)
attr.getter=findGetter(instance, attr.name);
if(attr.setter == null)
attr.setter=findSetter(instance, attr.name);
if(attr.setter == null)
attr.setter=new NoopAccessor();
}
}
protected void exposeManagedAttribute(Method method, Object instance) {
String methodName=method.getName();
ManagedAttribute attr_annotation=method.getAnnotation(ManagedAttribute.class);
Property prop=method.getAnnotation(Property.class);
boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
boolean expose=attr_annotation != null || expose_prop;
if(!expose)
return;
boolean writable=(prop != null && prop.writable()) || (attr_annotation != null && attr_annotation.writable());
// Is name of @ManagedAttributed or @Property used?
String attr_name=attr_annotation != null? attr_annotation.name() : prop != null? prop.name() : null;
if(attr_name != null && !attr_name.trim().isEmpty())
attr_name=attr_name.trim();
else {
// getFooBar() --> foo_bar
attr_name=Util.methodNameToAttributeName(methodName);
if(!atts.containsKey(attr_name)) {
// hmm, maybe we need to look for an attribute fooBar
String tmp=Util.methodNameToJavaAttributeName(methodName);
if(atts.containsKey(tmp))
attr_name=tmp;
}
}
String descr=attr_annotation != null ? attr_annotation.description() : prop != null? prop.description() : null;
AttributeEntry attr=atts.get(attr_name);
if(attr != null) {
if(isSetMethod(method)) {
if(attr.setter != null) {
if(log.isWarnEnabled())
log.warn("setter for \"" + attr_name + "\" is already defined (new method=" + method.getName() + ")");
}
else
attr.setter=new MethodAccessor(method, instance);
}
else {
if(attr.getter != null) {
if(log.isWarnEnabled())
log.warn("getter for \"" + attr_name + "\" is already defined (new method=" + method.getName() + ")");
}
else
attr.getter=new MethodAccessor(method, instance);
}
}
else { // create a new entry in atts
boolean is_setter=isSetMethod(method);
String type=is_setter? method.getParameterTypes()[0].getCanonicalName() : method.getReturnType().getCanonicalName();
MBeanAttributeInfo info=new MBeanAttributeInfo(attr_name, type, descr, true, writable, methodName.startsWith("is"));
AttributeEntry entry=new AttributeEntry(Util.methodNameToAttributeName(methodName), info);
if(is_setter)
entry.setter(new MethodAccessor(method, instance));
else
entry.getter(new MethodAccessor(method, instance));
atts.put(attr_name, entry);
}
}
/** Finds an accessor for an attribute. Tries to find getAttrName(), isAttrName(), attrName() methods. If not
* found, tries to use reflection to get the value of attr_name. If still not found, creates a NullAccessor. */
protected static Accessor findGetter(Object target, String attr_name) {
final String name=Util.attributeNameToMethodName(attr_name);
Class<?> clazz=target.getClass();
Method method=Util.findMethod(target, Arrays.asList("get" + name, "is" + name, toLowerCase(name)));
if(method != null && (isGetMethod(method) || isIsMethod(method)))
return new MethodAccessor(method, target);
// 4. Find a field last_name
Field field=Util.getField(clazz, attr_name);
if(field != null)
return new FieldAccessor(field, target);
return new NoopAccessor();
}
/** Finds an accessor for an attribute. Tries to find setAttrName(), attrName() methods. If not
* found, tries to use reflection to set the value of attr_name. If still not found, creates a NullAccessor. */
public static Accessor findSetter(Object target, String attr_name) {
final String name=Util.attributeNameToMethodName(attr_name);
final String fluent_name=toLowerCase(name);
Class<?> clazz=target.getClass();
Class<?> field_type=null;
Field field=Util.getField(clazz, attr_name);
field_type=field != null? field.getType() : null;
String setter_name="set" + name;
if(field_type != null) {
Method method=Util.findMethod(target, Arrays.asList(fluent_name, setter_name), field_type);
if(method != null && isSetMethod(method))
return new MethodAccessor(method, target);
}
// Find all methods but don't include methods from Object class
List<Method> methods=new ArrayList<>(Arrays.asList(clazz.getMethods()));
methods.removeAll(OBJECT_METHODS);
for(Method method: methods) {
String method_name=method.getName();
if((method_name.equals(name) || method_name.equals(fluent_name) || method_name.equals(setter_name)) && isSetMethod(method))
return new MethodAccessor(method, target);
}
// Find a field last_name
if(field != null)
return new FieldAccessor(field, target);
return null;
}
/** Returns a string with the first letter being lowercase */
protected static String toLowerCase(String input) {
if(Character.isUpperCase(input.charAt(0)))
return input.substring(0, 1).toLowerCase() + input.substring(1);
return input;
}
protected void findFields(Object instance, Predicate<AccessibleObject> filter) {
// traverse class hierarchy and find all annotated fields
for(Class<?> clazz=instance.getClass(); clazz != null && clazz != Object.class; clazz=clazz.getSuperclass()) {
Field[] fields=clazz.getDeclaredFields();
for(Field field: fields) {
if(filter != null && !filter.test(field))
continue;
ManagedAttribute attr=field.getAnnotation(ManagedAttribute.class);
Property prop=field.getAnnotation(Property.class);
boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
boolean expose=attr != null || expose_prop;
if(expose) {
String fieldName=attr != null? attr.name() : (prop != null? prop.name() : null);
if(fieldName != null && fieldName.trim().isEmpty())
fieldName=field.getName();
String descr=attr != null? attr.description() : prop.description();
boolean writable=attr != null? attr.writable() : prop.writable();
MBeanAttributeInfo info=new MBeanAttributeInfo(fieldName,
field.getType().getCanonicalName(),
descr,
true,
!Modifier.isFinal(field.getModifiers()) && writable,
false);
atts.put(fieldName, new AttributeEntry(field.getName(), info));
}
}
}
}
protected Attribute getNamedAttribute(String name) {
AttributeEntry entry=atts.get(name);
if(entry != null) {
try {
return new Attribute(name, entry.getter.invoke(null));
}
catch(Exception e) {
log.warn(Util.getMessage("AttrReadFailure"), name, e);
}
}
else {
log.warn(Util.getMessage("MissingAttribute"), name);
}
return null;
}
protected boolean setNamedAttribute(Attribute attribute) {
AttributeEntry entry=atts.get(attribute.getName());
if(entry != null) {
try {
entry.setter.invoke(attribute.getValue());
return true;
}
catch(Throwable e) {
log.warn(Util.getMessage("AttrWriteFailure"), attribute.getName(), e);
}
}
else {
log.warn(Util.getMessage("MissingAttribute"), attribute.getName());
}
return false;
}
public static class AttributeEntry {
/** The name of the field or method. Can be different from the key in atts when name in @Property or
* @ManagedAttribute was used */
protected final String name;
protected final MBeanAttributeInfo info;
protected Accessor getter;
protected Accessor setter;
public AttributeEntry(String name, MBeanAttributeInfo info) {
this(name, info, null, null);
}
public AttributeEntry(String name, MBeanAttributeInfo info, Accessor getter, Accessor setter) {
this.name=name;
this.info=info;
this.getter=getter;
this.setter=setter;
}
public String name() {return name;}
public MBeanAttributeInfo info() {return info;}
public Accessor getter() {return getter;}
public AttributeEntry getter(Accessor new_getter) {this.getter=new_getter; return this;}
public Accessor setter() {return setter;}
public AttributeEntry setter(Accessor new_setter) {this.setter=new_setter; return this;}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("AttributeEntry[" + name);
if(getter != null)
sb.append(", getter=" + getter);
if(setter() != null)
sb.append(", setter=" + setter);
sb.append("]");
return sb.toString();
}
}
public interface Accessor {
/** Invokes a getter or setter. For the getter, new_val must be ignored (null) */
Object invoke(Object new_val) throws Exception;
}
public static class MethodAccessor implements Accessor {
protected final Method method;
protected final Object target;
public MethodAccessor(Method method, Object target) {
this.method=method;
this.target=target;
}
public Method getMethod() {return method;}
public Object invoke(Object new_val) throws Exception {
return new_val != null? method.invoke(target, new_val) : method.invoke(target);
}
public String toString() {return "method[" + method.getName() + "()]";}
}
public static class FieldAccessor implements Accessor {
protected final Field field;
protected final Object target;
protected static final MethodHandles.Lookup LOOKUP=MethodHandles.lookup();
protected MethodHandle mh;
public FieldAccessor(Field field, Object target) {
this.field=field;
this.target=target;
if(!field.isAccessible())
field.setAccessible(true);
try {
mh=LOOKUP.unreflectGetter(field);
}
catch(IllegalAccessException e) {
// todo: log warning?
// throw new RuntimeException(e);
}
}
public Field getField() {return field;}
public Object invoke(Object new_val) throws Exception {
if(new_val == null) {
if(mh != null) {
try {
return mh.invoke(target);
}
catch(Throwable e) {
;
}
}
return field.get(target);
}
else {
field.set(target, new_val);
return null;
}
}
public String toString() {return "field[" + field.getName() + "]";}
}
public static class NoopAccessor implements Accessor {
public Object invoke(Object new_val) throws Exception {return null;}
public String toString() {return "NoopAccessor";}
}
}