Search in sources :

Example 1 with Ref

use of org.jgroups.util.Ref in project JGroups by belaban.

the class ReflectUtils method invokeOperation.

/**
 * 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<String, String> 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<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];
    }
    Ref<Object> t = new Ref<>(target);
    Ref<Method> method = new Ref<>(Util.findMethod(target.getClass(), method_name, args));
    if (!method.isSet()) {
        final String[] arguments = args;
        // check if any of the components in this class (if it has any) has the method
        Util.forAllComponents(target, (o, prefix) -> {
            if (!method.isSet() && method_name.startsWith(prefix + ".")) {
                String m = method_name.substring(prefix.length() + 1);
                try {
                    Method meth = Util.findMethod(o.getClass(), m, arguments);
                    if (meth != null) {
                        method.set(meth);
                        t.set(o);
                    }
                } catch (Exception e) {
                }
            }
        });
    }
    if (!method.isSet())
        throw new IllegalArgumentException(String.format("method %s not found in %s", method_name, target.getClass().getSimpleName()));
    Object retval = invoke(method.get(), t.get(), args);
    if (retval != null)
        map.put(target.getClass().getSimpleName() + "." + method_name, retval.toString());
}
Also used : Ref(org.jgroups.util.Ref) Method(java.lang.reflect.Method)

Example 2 with Ref

use of org.jgroups.util.Ref in project JGroups by belaban.

the class ProtocolStack method initComponents.

public static void initComponents(Protocol p, ProtocolConfiguration cfg) throws Exception {
    // copy the props and weed out properties from non-components
    Map<String, String> properties = cfg != null ? cfg.getProperties() : new HashMap<>();
    Map<String, String> props = new HashMap<>();
    // first a bit of sanity checking: no duplicate components in p
    final Map<String, Class<?>> prefixes = new HashMap<>();
    final Ref<Exception> ex = new Ref<>(null);
    Util.forAllComponentTypes(p.getClass(), (cl, prefix) -> {
        if (ex.isSet())
            return;
        if (prefix == null || prefix.trim().isEmpty()) {
            ex.set(new IllegalArgumentException(String.format("component (class=%s) in %s must have a prefix", cl.getSimpleName(), p.getName())));
            return;
        }
        if (prefixes.containsKey(prefix))
            ex.set(new IllegalArgumentException(String.format("multiple components (class=%s) in %s have same prefix '%s'", cl.getSimpleName(), p.getName(), prefix)));
        else
            prefixes.put(prefix, cl);
    });
    if (ex.isSet())
        throw ex.get();
    Util.forAllComponentTypes(p.getClass(), (c, prefix) -> {
        String key = prefix + ".";
        properties.entrySet().stream().filter(e -> e.getKey().startsWith(key)).forEach(e -> props.put(e.getKey(), e.getValue()));
    });
    ex.set(null);
    // StackType ip_version=Util.getIpStackType();
    InetAddress resolved_addr = p.getTransport() != null ? p.getTransport().getBindAddress() : null;
    final StackType ip_version = resolved_addr instanceof Inet6Address ? StackType.IPv6 : StackType.IPv4;
    Util.forAllComponents(p, (comp, prefix) -> {
        try {
            if (ex.isSet())
                return;
            Map<String, String> m = new HashMap<>();
            String key = prefix + ".";
            props.entrySet().stream().filter(e -> e.getKey().startsWith(key)).forEach(e -> m.put(e.getKey().substring(prefix.length() + 1), e.getValue()));
            props.keySet().removeIf(k -> k.startsWith(key));
            if (!m.isEmpty()) {
                Configurator.initializeAttrs(comp, m, ip_version);
                if (!m.isEmpty()) {
                    String fmt = "the following properties in %s:%s (%s) are not recognized: %s";
                    ex.set(new IllegalArgumentException(String.format(fmt, p.getName(), prefix, comp.getClass().getSimpleName(), m)));
                }
            }
            Configurator.setDefaultAddressValues(comp, ip_version);
            if (comp instanceof Lifecycle)
                ((Lifecycle) comp).init();
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("failed initializing component %s in protocol %s: %s", comp.getClass().getSimpleName(), p, e));
        }
    });
    if (ex.isSet())
        throw ex.get();
    if (!props.isEmpty()) {
        String fmt = "configuration error: the following component properties in %s are not recognized: %s";
        throw new IllegalArgumentException(String.format(fmt, p.getName(), props));
    }
}
Also used : Property(org.jgroups.annotations.Property) MessageBatch(org.jgroups.util.MessageBatch) java.util(java.util) Util(org.jgroups.util.Util) ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) Field(java.lang.reflect.Field) InetAddress(java.net.InetAddress) Inet6Address(java.net.Inet6Address) ReflectUtils(org.jgroups.jmx.ReflectUtils) Entry(java.util.Map.Entry) org.jgroups(org.jgroups) ClassConfigurator(org.jgroups.conf.ClassConfigurator) StackType(org.jgroups.util.StackType) Pattern(java.util.regex.Pattern) PropertyConverter(org.jgroups.conf.PropertyConverter) Method(java.lang.reflect.Method) TP(org.jgroups.protocols.TP) Ref(org.jgroups.util.Ref) Inet6Address(java.net.Inet6Address) Ref(org.jgroups.util.Ref) StackType(org.jgroups.util.StackType) InetAddress(java.net.InetAddress)

Aggregations

Method (java.lang.reflect.Method)2 Ref (org.jgroups.util.Ref)2 Field (java.lang.reflect.Field)1 Inet6Address (java.net.Inet6Address)1 InetAddress (java.net.InetAddress)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 Pattern (java.util.regex.Pattern)1 org.jgroups (org.jgroups)1 Property (org.jgroups.annotations.Property)1 ClassConfigurator (org.jgroups.conf.ClassConfigurator)1 PropertyConverter (org.jgroups.conf.PropertyConverter)1 ProtocolConfiguration (org.jgroups.conf.ProtocolConfiguration)1 ReflectUtils (org.jgroups.jmx.ReflectUtils)1 TP (org.jgroups.protocols.TP)1 MessageBatch (org.jgroups.util.MessageBatch)1 StackType (org.jgroups.util.StackType)1 Util (org.jgroups.util.Util)1