Search in sources :

Example 16 with Property

use of org.jgroups.annotations.Property in project JGroups by belaban.

the class Configurator method addPropertyToDependencyList.

/**
 *  DFS of dependency graph formed by Property annotations and dependsUpon parameter
 *  This is used to create a list of Properties in dependency order
 */
static void addPropertyToDependencyList(List<AccessibleObject> orderedList, Map<String, AccessibleObject> props, Stack<AccessibleObject> stack, AccessibleObject obj) {
    if (orderedList.contains(obj))
        return;
    if (stack.search(obj) > 0) {
        throw new RuntimeException("Deadlock in @Property dependency processing");
    }
    // record the fact that we are processing obj
    stack.push(obj);
    // process dependencies for this object before adding it to the list
    Property annotation = obj.getAnnotation(Property.class);
    String dependsClause = annotation.dependsUpon();
    StringTokenizer st = new StringTokenizer(dependsClause, ",");
    while (st.hasMoreTokens()) {
        String token = st.nextToken().trim();
        AccessibleObject dep = props.get(token);
        // if null, throw exception
        addPropertyToDependencyList(orderedList, props, stack, dep);
    }
    // indicate we're done with processing dependencies
    stack.pop();
    // we can now add in dependency order
    orderedList.add(obj);
}
Also used : Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Example 17 with Property

use of org.jgroups.annotations.Property in project JGroups by belaban.

the class Configurator method checkDependencyReferencesPresent.

/*
     * Checks that for every dependency referred, there is a matching property
     */
static void checkDependencyReferencesPresent(List<AccessibleObject> objects, Map<String, AccessibleObject> props) {
    // iterate overall properties marked by @Property
    for (int i = 0; i < objects.size(); i++) {
        // get the Property annotation
        AccessibleObject ao = objects.get(i);
        Property annotation = ao.getAnnotation(Property.class);
        if (annotation == null) {
            throw new IllegalArgumentException("@Property annotation is required for checking dependencies;" + " annotation is missing for Field/Method " + ao.toString());
        }
        String dependsClause = annotation.dependsUpon();
        if (dependsClause.trim().isEmpty())
            continue;
        // split dependsUpon specifier into tokens; trim each token; search for token in list
        StringTokenizer st = new StringTokenizer(dependsClause, ",");
        while (st.hasMoreTokens()) {
            String token = st.nextToken().trim();
            // check that the string representing a property name is in the list
            boolean found = false;
            Set<String> keyset = props.keySet();
            for (Iterator<String> iter = keyset.iterator(); iter.hasNext(); ) {
                if (iter.next().equals(token)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new IllegalArgumentException("@Property annotation " + annotation.name() + " has an unresolved dependsUpon property: " + token);
            }
        }
    }
}
Also used : Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Example 18 with Property

use of org.jgroups.annotations.Property in project JGroups by belaban.

the class Configurator method resolveAndAssignField.

public static void resolveAndAssignField(Object obj, Field field, Map<String, String> props) throws Exception {
    Property annotation = field.getAnnotation(Property.class);
    if (annotation != null) {
        String propertyName = PropertyHelper.getPropertyName(field, props);
        String propertyValue = props.get(propertyName);
        // only do this if the property value hasn't yet been set
        if (propertyValue == null) {
            String tmp = grabSystemProp(field.getAnnotation(Property.class));
            if (tmp != null)
                propertyValue = tmp;
        }
        if (propertyName != null && propertyValue != null) {
            String deprecated_msg = annotation.deprecatedMessage();
            if (deprecated_msg != null && !deprecated_msg.isEmpty()) {
                log.warn(Util.getMessage("Deprecated"), field.getDeclaringClass().getSimpleName() + "." + field.getName(), deprecated_msg);
            }
        }
        if (propertyValue != null || !PropertyHelper.usesDefaultConverter(field)) {
            Object converted = null;
            try {
                converted = PropertyHelper.getConvertedValue(obj, field, props, propertyValue, true);
                if (converted != null)
                    Util.setField(field, obj, converted);
            } catch (Exception e) {
                String name = obj instanceof Protocol ? ((Protocol) obj).getName() : obj.getClass().getName();
                throw new Exception("Property assignment of " + propertyName + " in " + name + " with original property value " + propertyValue + " and converted to " + converted + " could not be assigned", e);
            }
        }
        props.remove(propertyName);
    }
}
Also used : Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Example 19 with Property

use of org.jgroups.annotations.Property in project JGroups by belaban.

the class Configurator method setDefaultValues.

/*
    * Method which processes @Property.default() values, associated with the annotation
    * using the defaultValue= attribute. This method does the following:
    * - locate all properties which have no user value assigned
    * - if the defaultValue attribute is not "", generate a value for the field using the
    * property converter for that property and assign it to the field
    */
public static void setDefaultValues(List<ProtocolConfiguration> protocol_configs, List<Protocol> protocols, StackType ip_version) throws Exception {
    InetAddress default_ip_address = Util.getNonLoopbackAddress();
    if (default_ip_address == null) {
        log.warn(Util.getMessage("OnlyLoopbackFound"), ip_version);
        default_ip_address = Util.getLocalhost(ip_version);
    }
    for (int i = 0; i < protocol_configs.size(); i++) {
        ProtocolConfiguration protocol_config = protocol_configs.get(i);
        Protocol protocol = protocols.get(i);
        String protocolName = protocol.getName();
        // regenerate the Properties which were destroyed during basic property processing
        Map<String, String> properties = new HashMap<>(protocol_config.getProperties());
        Method[] methods = Util.getAllDeclaredMethodsWithAnnotations(protocol.getClass(), Property.class);
        for (int j = 0; j < methods.length; j++) {
            if (isSetPropertyMethod(methods[j], protocol.getClass())) {
                String propertyName = PropertyHelper.getPropertyName(methods[j]);
                Object propertyValue = getValueFromProtocol(protocol, propertyName);
                if (propertyValue == null) {
                    // if propertyValue is null, check if there is a we can use
                    Property annotation = methods[j].getAnnotation(Property.class);
                    // get the default value for the method- check for InetAddress types
                    String defaultValue = null;
                    if (InetAddressInfo.isInetAddressRelated(methods[j])) {
                        defaultValue = ip_version == StackType.IPv4 ? annotation.defaultValueIPv4() : annotation.defaultValueIPv6();
                        if (defaultValue != null && !defaultValue.isEmpty()) {
                            Object converted = null;
                            try {
                                if (defaultValue.equalsIgnoreCase(Global.NON_LOOPBACK_ADDRESS))
                                    converted = default_ip_address;
                                else
                                    converted = PropertyHelper.getConvertedValue(protocol, methods[j], properties, defaultValue, true);
                                methods[j].invoke(protocol, converted);
                            } catch (Exception e) {
                                throw new Exception("default could not be assigned for method " + propertyName + " in " + protocolName + " with default " + defaultValue, e);
                            }
                            log.debug("set property " + protocolName + "." + propertyName + " to default value " + converted);
                        }
                    }
                }
            }
        }
        // traverse class hierarchy and find all annotated fields and add them to the list if annotated
        Field[] fields = Util.getAllDeclaredFieldsWithAnnotations(protocol.getClass(), Property.class);
        for (int j = 0; j < fields.length; j++) {
            String propertyName = PropertyHelper.getPropertyName(fields[j], properties);
            Object propertyValue = getValueFromProtocol(protocol, fields[j]);
            if (propertyValue == null) {
                // add to collection of @Properties with no user specified value
                Property annotation = fields[j].getAnnotation(Property.class);
                // get the default value for the field - check for InetAddress types
                String defaultValue = null;
                if (InetAddressInfo.isInetAddressRelated(fields[j])) {
                    defaultValue = ip_version == StackType.IPv4 ? annotation.defaultValueIPv4() : annotation.defaultValueIPv6();
                    if (defaultValue != null && !defaultValue.isEmpty()) {
                        // condition for invoking converter
                        if (defaultValue != null || !PropertyHelper.usesDefaultConverter(fields[j])) {
                            Object converted = null;
                            try {
                                if (defaultValue.equalsIgnoreCase(Global.NON_LOOPBACK_ADDRESS))
                                    converted = default_ip_address;
                                else
                                    converted = PropertyHelper.getConvertedValue(protocol, fields[j], properties, defaultValue, true);
                                if (converted != null)
                                    Util.setField(fields[j], protocol, converted);
                            } catch (Exception e) {
                                throw new Exception("default could not be assigned for field " + propertyName + " in " + protocolName + " with default value " + defaultValue, e);
                            }
                            log.debug("set property " + protocolName + "." + propertyName + " to default value " + converted);
                        }
                    }
                }
            }
        }
    }
}
Also used : ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) InetAddress(java.net.InetAddress) Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Example 20 with Property

use of org.jgroups.annotations.Property in project JGroups by belaban.

the class Configurator method resolveAndInvokePropertyMethod.

public static void resolveAndInvokePropertyMethod(Object obj, Method method, Map<String, String> props) throws Exception {
    String methodName = method.getName();
    Property annotation = method.getAnnotation(Property.class);
    if (annotation != null && isSetPropertyMethod(method, obj.getClass())) {
        String propertyName = PropertyHelper.getPropertyName(method);
        String propertyValue = props.get(propertyName);
        // if there is a systemProperty attribute defined in the annotation, set the property value from the system property
        String tmp = grabSystemProp(method.getAnnotation(Property.class));
        if (tmp != null)
            propertyValue = tmp;
        if (propertyName != null && propertyValue != null) {
            String deprecated_msg = annotation.deprecatedMessage();
            if (deprecated_msg != null && !deprecated_msg.isEmpty()) {
                log.warn(Util.getMessage("Deprecated"), method.getDeclaringClass().getSimpleName() + "." + methodName, deprecated_msg);
            }
        }
        if (propertyValue != null) {
            Object converted = null;
            try {
                converted = PropertyHelper.getConvertedValue(obj, method, props, propertyValue, true);
                method.invoke(obj, converted);
            } catch (Exception e) {
                String name = obj instanceof Protocol ? ((Protocol) obj).getName() : obj.getClass().getName();
                throw new Exception("Could not assign property " + propertyName + " in " + name + ", method is " + methodName + ", converted value is " + converted, e);
            }
        }
        props.remove(propertyName);
    }
}
Also used : Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Aggregations

Property (org.jgroups.annotations.Property)21 Field (java.lang.reflect.Field)7 DeprecatedProperty (org.jgroups.annotations.DeprecatedProperty)7 Method (java.lang.reflect.Method)5 InetAddress (java.net.InetAddress)3 ManagedAttribute (org.jgroups.annotations.ManagedAttribute)3 Protocol (org.jgroups.stack.Protocol)3 ProtocolConfiguration (org.jgroups.conf.ProtocolConfiguration)2 InetSocketAddress (java.net.InetSocketAddress)1 StringTokenizer (java.util.StringTokenizer)1 XmlAttribute (org.jgroups.annotations.XmlAttribute)1 XmlElement (org.jgroups.annotations.XmlElement)1 PropertyConverter (org.jgroups.conf.PropertyConverter)1 Element (org.w3c.dom.Element)1