Search in sources :

Example 1 with AdditionalJmxObjects

use of org.jgroups.jmx.AdditionalJmxObjects in project JGroups by belaban.

the class JChannelProbeHandler method handleJmx.

protected void handleJmx(Map<String, String> map, String input) {
    Map<String, Object> tmp_stats;
    int index = input.indexOf('=');
    if (index > -1) {
        List<String> list = null;
        String protocol_name = input.substring(index + 1);
        index = protocol_name.indexOf('.');
        if (index > -1) {
            String rest = protocol_name;
            protocol_name = protocol_name.substring(0, index);
            // e.g. "num_sent,msgs,num_received_msgs"
            String attrs = rest.substring(index + 1);
            list = Util.parseStringList(attrs, ",");
            // check if there are any attribute-sets in the list
            for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
                String tmp = it.next();
                index = tmp.indexOf('=');
                if (index != -1) {
                    String attrname = tmp.substring(0, index);
                    String attrvalue = tmp.substring(index + 1);
                    Object target = ch.getProtocolStack().findProtocol(protocol_name);
                    Field field = target != null ? Util.getField(target.getClass(), attrname) : null;
                    if (field == null && target instanceof AdditionalJmxObjects) {
                        Object[] objs = ((AdditionalJmxObjects) target).getJmxObjects();
                        if (objs != null && objs.length > 0) {
                            for (Object o : objs) {
                                field = o != null ? Util.getField(o.getClass(), attrname) : null;
                                if (field != null) {
                                    target = o;
                                    break;
                                }
                            }
                        }
                    }
                    if (field != null) {
                        Object value = Util.convert(attrvalue, field.getType());
                        if (value != null) {
                            if (target instanceof Protocol)
                                ((Protocol) target).setValue(attrname, value);
                            else
                                Util.setField(field, target, value);
                        }
                    } else {
                        // try to find a setter for X, e.g. x(type-of-x) or setX(type-of-x)
                        ResourceDMBean.Accessor setter = ResourceDMBean.findSetter(target, attrname);
                        if (setter == null && target instanceof AdditionalJmxObjects) {
                            Object[] objs = ((AdditionalJmxObjects) target).getJmxObjects();
                            if (objs != null && objs.length > 0) {
                                for (Object o : objs) {
                                    setter = o != null ? ResourceDMBean.findSetter(o, attrname) : null;
                                    if (setter != null)
                                        break;
                                }
                            }
                        }
                        if (setter != null) {
                            try {
                                Class<?> type = setter instanceof ResourceDMBean.FieldAccessor ? ((ResourceDMBean.FieldAccessor) setter).getField().getType() : setter instanceof ResourceDMBean.MethodAccessor ? ((ResourceDMBean.MethodAccessor) setter).getMethod().getParameterTypes()[0] : null;
                                Object converted_value = Util.convert(attrvalue, type);
                                setter.invoke(converted_value);
                            } catch (Exception e) {
                                log.error("unable to invoke %s() on %s: %s", setter, protocol_name, e);
                            }
                        } else {
                            log.warn(Util.getMessage("FieldNotFound"), attrname, protocol_name);
                            setter = new ResourceDMBean.NoopAccessor();
                        }
                    }
                    it.remove();
                }
            }
        }
        tmp_stats = ch.dumpStats(protocol_name, list);
        if (tmp_stats != null) {
            for (Map.Entry<String, Object> entry : tmp_stats.entrySet()) {
                Map<String, Object> tmp_map = (Map<String, Object>) entry.getValue();
                String key = entry.getKey();
                map.put(key, tmp_map != null ? tmp_map.toString() : null);
            }
        }
    } else {
        tmp_stats = ch.dumpStats();
        if (tmp_stats != null) {
            for (Map.Entry<String, Object> entry : tmp_stats.entrySet()) {
                Map<String, Object> tmp_map = (Map<String, Object>) entry.getValue();
                String key = entry.getKey();
                map.put(key, tmp_map != null ? tmp_map.toString() : null);
            }
        }
    }
}
Also used : ResourceDMBean(org.jgroups.jmx.ResourceDMBean) Field(java.lang.reflect.Field) AdditionalJmxObjects(org.jgroups.jmx.AdditionalJmxObjects) Protocol(org.jgroups.stack.Protocol)

Example 2 with AdditionalJmxObjects

use of org.jgroups.jmx.AdditionalJmxObjects in project JGroups by belaban.

the class JChannelProbeHandler method handleOperation.

/**
 * Invokes an operation and puts the return value into map
 * @param map
 * @param operation Protocol.OperationName[args], e.g. STABLE.foo[arg1 arg2 arg3]
 */
protected void handleOperation(Map<String, String> map, String operation) throws Exception {
    int index = operation.indexOf('.');
    if (index == -1)
        throw new IllegalArgumentException("operation " + operation + " is missing the protocol name");
    String prot_name = operation.substring(0, index);
    Protocol prot = ch.getProtocolStack().findProtocol(prot_name);
    if (prot == null)
        // less drastic than throwing an exception...
        return;
    int args_index = operation.indexOf('[');
    String method_name;
    if (args_index != -1)
        method_name = operation.substring(index + 1, args_index).trim();
    else
        method_name = operation.substring(index + 1).trim();
    String[] args = null;
    if (args_index != -1) {
        int end_index = operation.indexOf(']');
        if (end_index == -1)
            throw new IllegalArgumentException("] not found");
        List<String> 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];
    }
    Object target = prot;
    Method method = MethodCall.findMethod(target.getClass(), method_name, args);
    if (method == null) {
        if (prot instanceof AdditionalJmxObjects) {
            for (Object obj : ((AdditionalJmxObjects) prot).getJmxObjects()) {
                method = MethodCall.findMethod(obj.getClass(), method_name, args);
                if (method != null) {
                    target = obj;
                    break;
                }
            }
        }
        if (method == null) {
            log.warn(Util.getMessage("MethodNotFound"), ch.getAddress(), target.getClass().getSimpleName(), method_name);
            return;
        }
    }
    MethodCall call = new MethodCall(method);
    Object[] converted_args = null;
    if (args != null) {
        converted_args = new Object[args.length];
        Class<?>[] types = method.getParameterTypes();
        for (int i = 0; i < args.length; i++) converted_args[i] = Util.convert(args[i], types[i]);
    }
    Object retval = call.invoke(target, converted_args);
    if (retval != null)
        map.put(prot_name + "." + method_name, retval.toString());
}
Also used : Method(java.lang.reflect.Method) MethodCall(org.jgroups.blocks.MethodCall) AdditionalJmxObjects(org.jgroups.jmx.AdditionalJmxObjects) Protocol(org.jgroups.stack.Protocol)

Aggregations

AdditionalJmxObjects (org.jgroups.jmx.AdditionalJmxObjects)2 Protocol (org.jgroups.stack.Protocol)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 MethodCall (org.jgroups.blocks.MethodCall)1 ResourceDMBean (org.jgroups.jmx.ResourceDMBean)1