org.jgroups.stack.NonReflectiveProbeHandler Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
package org.jgroups.stack;
import org.jgroups.JChannel;
import org.jgroups.JChannelProbeHandler;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.ManagedOperation;
import org.jgroups.annotations.Property;
import org.jgroups.jmx.ResourceDMBean;
import org.jgroups.util.Util;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
/**
* A {@link org.jgroups.stack.DiagnosticsHandler.ProbeHandler} that does not use reflection. Can be used instead of the
* default ProbeHandler for commands "jmx" and "op"
* @author Bela Ban
* @since 4.1.0
*/
public class NonReflectiveProbeHandler extends JChannelProbeHandler {
// List of fields and field getters. Can be used to get field values, invoke field getters to get field values
// and set fields (via Field.set()).
protected final Map> attrs=new LinkedHashMap<>();
// List of fields and corresponding setters. Can be used to set values of fields
protected final Map> setters=new LinkedHashMap<>();
// List of operations
protected final Map> operations=new LinkedHashMap<>();
protected static final Predicate FILTER=obj -> obj.isAnnotationPresent(ManagedAttribute.class) ||
(obj.isAnnotationPresent(Property.class) && obj.getAnnotation(Property.class).exposeAsManagedAttribute()) ||
obj.isAnnotationPresent(ManagedOperation.class);
public NonReflectiveProbeHandler(JChannel ch) {
super(ch);
}
public NonReflectiveProbeHandler initialize(Protocol[] protocols) {
return initialize(Arrays.asList(protocols));
}
public NonReflectiveProbeHandler initialize(Collection prots) {
for(Protocol prot: prots) {
String prot_name=prot.getName();
Map m=attrs.computeIfAbsent(prot_name, k -> new TreeMap<>());
BiConsumer field_func=(f,o) -> m.put(f.getName(), new ResourceDMBean.FieldAccessor(f, o));
BiConsumer method_func=(method,obj) -> { // getter
if(method.isAnnotationPresent(ManagedOperation.class)) {
Map tmp=operations.computeIfAbsent(prot_name, k -> new TreeMap<>());
tmp.put(method.getName(), new ResourceDMBean.MethodAccessor(method, obj));
}
else if(ResourceDMBean.isGetMethod(method)) {
String method_name=Util.getNameFromAnnotation(method);
String attributeName=Util.methodNameToAttributeName(method_name);
m.put(attributeName, new ResourceDMBean.MethodAccessor(method, obj));
}
else if(ResourceDMBean.isSetMethod(method)) { // setter
Map tmp=setters.computeIfAbsent(prot_name, k -> new TreeMap<>());
String method_name=Util.getNameFromAnnotation(method);
String attributeName=Util.methodNameToAttributeName(method_name);
tmp.put(attributeName, new ResourceDMBean.MethodAccessor(method, obj));
}
};
Util.forAllFieldsAndMethods(prot, FILTER, field_func, method_func);
List