Search in sources :

Example 1 with PropertyConverter

use of org.jgroups.conf.PropertyConverter in project JGroups by belaban.

the class PropertyConvertersTest method testPrimitiveTypes.

public static void testPrimitiveTypes() throws Exception {
    PropertyConverter conv = new PropertyConverters.Default();
    check(null, Boolean.TYPE, "true", true, conv);
    check(null, Integer.TYPE, "322649", 322649, conv);
    check(null, Long.TYPE, "322649", 322649L, conv);
}
Also used : PropertyConverter(org.jgroups.conf.PropertyConverter)

Example 2 with PropertyConverter

use of org.jgroups.conf.PropertyConverter in project JGroups by belaban.

the class ProtocolStack method getProps.

private static Map<String, String> getProps(Protocol prot) {
    Map<String, String> retval = new HashMap<>();
    for (Class<?> clazz = prot.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        // copy all fields marked with @Property
        Field[] fields = clazz.getDeclaredFields();
        Property annotation;
        for (Field field : fields) {
            if (field.isAnnotationPresent(Property.class)) {
                Object value = Util.getField(field, prot);
                if (value != null) {
                    annotation = field.getAnnotation(Property.class);
                    Class<?> conv_class = annotation.converter();
                    PropertyConverter conv = null;
                    try {
                        conv = (PropertyConverter) conv_class.newInstance();
                    } catch (Exception e) {
                    }
                    String tmp = conv != null ? conv.toString(value) : value.toString();
                    retval.put(field.getName(), tmp);
                }
            }
        }
        // copy all setters marked with @Property
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            String methodName = method.getName();
            if (method.isAnnotationPresent(Property.class) && Configurator.isSetPropertyMethod(method, clazz)) {
                annotation = method.getAnnotation(Property.class);
                List<String> possible_names = new LinkedList<>();
                if (annotation.name() != null)
                    possible_names.add(annotation.name());
                possible_names.add(Util.methodNameToAttributeName(methodName));
                Field field = Util.findField(prot, possible_names);
                if (field != null) {
                    Object value = Util.getField(field, prot);
                    if (value != null) {
                        Class<?> conv_class = annotation.converter();
                        PropertyConverter conv = null;
                        try {
                            conv = (PropertyConverter) conv_class.newInstance();
                        } catch (Exception e) {
                        }
                        String tmp = conv != null ? conv.toString(value) : value.toString();
                        retval.put(field.getName(), tmp);
                    }
                }
            }
        }
    }
    return retval;
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) PropertyConverter(org.jgroups.conf.PropertyConverter) Property(org.jgroups.annotations.Property)

Example 3 with PropertyConverter

use of org.jgroups.conf.PropertyConverter in project JGroups by belaban.

the class PropertyConvertersTest method testLongArray.

public static void testLongArray() throws Exception {
    PropertyConverter conv = new PropertyConverters.LongArray();
    long[] array = { 1, 2, 3, 4, 5 };
    checkArray(null, array.getClass(), "1,2,3,4,5", array, conv);
}
Also used : PropertyConverter(org.jgroups.conf.PropertyConverter)

Example 4 with PropertyConverter

use of org.jgroups.conf.PropertyConverter in project JGroups by belaban.

the class PropertyConvertersTest method testStringProperties.

public static void testStringProperties() throws Exception {
    PropertyConverter c = new PropertyConverters.StringProperties();
    String value = "com.sun.security.sasl.digest.realm=MyRealm,qop=true";
    Map<String, String> map = (Map<String, String>) c.convert(null, Map.class, "props", value, false);
    assert map.size() == 2;
    assert map.get("qop").equals("true");
    assert map.get("com.sun.security.sasl.digest.realm").equals("MyRealm");
}
Also used : PropertyConverter(org.jgroups.conf.PropertyConverter) Map(java.util.Map)

Example 5 with PropertyConverter

use of org.jgroups.conf.PropertyConverter in project JGroups by belaban.

the class PropertyConvertersTest method testNetworkList.

/**
 * Cannot really test list of eth0,eth1,lo, because the list differs from host to host
 *
 * @throws Exception
 */
public static void testNetworkList() throws Exception {
    PropertyConverter conv = new PropertyConverters.NetworkInterfaceList();
    String loopback_name = getLoopbackName();
    if (loopback_name == null)
        loopback_name = "lo";
    Object tmp;
    try {
        tmp = conv.convert(null, List.class, "bela", loopback_name, false);
    } catch (Throwable t) {
        // when running on Mac OS
        tmp = conv.convert(null, List.class, "bela", "lo0", false);
    }
    Object str = conv.toString(tmp);
    System.out.println("str = " + str);
    assert str.equals(loopback_name) || str.equals("lo0");
}
Also used : PropertyConverter(org.jgroups.conf.PropertyConverter) List(java.util.List)

Aggregations

PropertyConverter (org.jgroups.conf.PropertyConverter)5 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Map (java.util.Map)1 Property (org.jgroups.annotations.Property)1