Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.jgroups.jmx;
import org.jgroups.annotations.ManagedOperation;
import org.jgroups.blocks.MethodCall;
import org.jgroups.util.Ref;
import org.jgroups.util.Util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* Methods to get/set attributes and invoke operations of a given instance.
* @author Bela Ban
* @since 5.2
*/
public class ReflectUtils {
/** Handles an attribute read or write */
public static void handleAttributeAccess(Map map, String attrs, Object target) {
if(target == null)
return;
List list=attrs != null? Util.parseStringList(attrs, ",") : null;
// check if there are any attribute-sets in the list
if(list != null) {
for(Iterator it=list.iterator(); it.hasNext(); ) {
String tmp=it.next();
int index=tmp.indexOf('=');
if(index > -1) { // an attribute write
it.remove();
String attrname=tmp.substring(0, index);
String attrvalue=tmp.substring(index + 1);
try {
handleAttrWrite(target, attrname, attrvalue);
}
catch(Exception e) {
throw new IllegalArgumentException(String.format("failed writing: %s", e));
}
}
}
}
Map> tmp_stats=dumpSelectedAttributes(target, list);
convert(tmp_stats, map);
}
/**
* Reads all attributes and values of the given target (annotated with
* {@link org.jgroups.annotations.ManagedAttribute} and filters them if a non-null list of attributes is given
* @param target The object to read the attributes from
* @param attrs A list of attributes to match against. Example:
bind
with match
bind_addr
,
*
bind_port
etc. If
null
, all attributes will be read.
* @return A map of attributes and values, keyed by the object's class
*/
public static Map> dumpSelectedAttributes(Object target, List attrs) {
Map> retval=new HashMap<>();
Map tmp=new TreeMap<>();
dumpStats(target, tmp);
if(attrs != null && !attrs.isEmpty()) {
// weed out attrs not in list
for(Iterator it=tmp.keySet().iterator(); it.hasNext(); ) {
String attrname=it.next();
boolean found=false;
for(String attr : attrs) {
if(attrname.startsWith(attr)) {
found=true;
break; // found
}
}
if(!found)
it.remove();
}
}
String pname=target.getClass().getSimpleName();
retval.put(pname, tmp);
return retval;
}
public static void dumpStats(Object obj, Map map) {
ResourceDMBean.dumpStats(obj, map); // dumps attrs and operations into tmp
Util.forAllComponents(obj, (o,prefix) -> ResourceDMBean.dumpStats(o, prefix, map));
}
/** Prints all operations of clazz annotated with {@link ManagedOperation} */
public static String listOperations(Class> clazz) {
if(clazz == null)
return "";
StringBuilder sb=new StringBuilder(clazz.getSimpleName()).append(":\n");
Method[] methods=Util.getAllDeclaredMethodsWithAnnotations(clazz, ManagedOperation.class);
for(Method m: methods)
sb.append(" ").append(methodToString(m)).append("\n");
Util.forAllComponentTypes(clazz, (cl, prefix) -> {
Method[] meths=Util.getAllDeclaredMethodsWithAnnotations(cl, ManagedOperation.class);
for(Method m: meths)
sb.append(" ").append(prefix).append(".").append(methodToString(m)).append("\n");
});
return sb.toString();
}
/**
* Invokes an operation and puts the return value into map
* @param map
* @param operation Protocol.OperationName[args], e.g. STABLE.foo[arg1 arg2 arg3]
* @param target The target object on which to invoke the operation
*/
public static void invokeOperation(Map map, String operation, Object target) throws Exception {
if(target == null)
throw new IllegalArgumentException("target object must not be null");
int args_index=operation.indexOf('[');
String method_name;
if(args_index != -1)
method_name=operation.substring(0, args_index).trim();
else
method_name=operation.trim();
String[] args=null;
if(args_index != -1) {
int end_index=operation.indexOf(']');
if(end_index == -1)
throw new IllegalArgumentException("] not found");
List str_args=Util.parseCommaDelimitedStrings(operation.substring(args_index + 1, end_index));
Object[] strings=str_args.toArray();
args=new String[strings.length];
for(int i=0; i < strings.length; i++)
args[i]=(String)strings[i];
}
Ref