Search in sources :

Example 1 with OptionBehaviourAttribute

use of org.ovirt.engine.core.common.config.OptionBehaviourAttribute in project ovirt-engine by oVirt.

the class ConfigUtilsBase method getValue.

/**
 * Returns the typed value of the given option. returns default value if option.option_value is null
 */
protected Object getValue(VdcOption option) {
    Object result = option.getOptionValue();
    EnumValue enumValue = parseEnumValue(option.getOptionName());
    if (enumValue != null) {
        final OptionBehaviourAttribute optionBehaviour = enumValue.getOptionBehaviour();
        final Class<?> fieldType = enumValue.getFieldType();
        result = parseValue(option.getOptionValue(), option.getOptionName(), fieldType);
        if (optionBehaviour != null) {
            Map<String, Object> values = null;
            switch(optionBehaviour.behaviour()) {
                // split string by comma for List<string> constructor
                case CommaSeparatedStringArray:
                    result = Arrays.asList(((String) result).split("[,]", -1));
                    break;
                case Password:
                    try {
                        result = EngineEncryptionUtils.decrypt((String) result);
                    } catch (Exception e) {
                        log.error("Failed to decrypt value for property '{}', encrypted value will be used. Error: {} ", option.getOptionName(), e.getMessage());
                        log.debug("Exception", e);
                    }
                    break;
                case ValueDependent:
                    // get the config that this value depends on
                    String prefix = getValue(optionBehaviour.dependentOn(), ConfigCommon.defaultConfigurationVersion);
                    // combine the prefix with the 'real value'
                    if (prefix != null) {
                        String realName = String.format("%1$s%2$s", prefix, optionBehaviour.realValue());
                        result = getValue(ConfigValues.valueOf(realName), ConfigCommon.defaultConfigurationVersion);
                    }
                    break;
                case CommaSeparatedVersionArray:
                    HashSet<Version> versions = new HashSet<>();
                    for (String ver : result.toString().split("[,]", -1)) {
                        try {
                            versions.add(new Version(ver));
                        } catch (Exception e) {
                            log.error("Could not parse version '{}' for config value '{}'", ver, option.getOptionName());
                        }
                    }
                    result = versions;
                    break;
            }
        }
    }
    return result;
}
Also used : Version(org.ovirt.engine.core.compat.Version) OptionBehaviourAttribute(org.ovirt.engine.core.common.config.OptionBehaviourAttribute) HashSet(java.util.HashSet)

Example 2 with OptionBehaviourAttribute

use of org.ovirt.engine.core.common.config.OptionBehaviourAttribute in project ovirt-engine by oVirt.

the class ConfigUtilsBase method parseEnumValue.

/**
 * parse the enum value by its attributes and return the type, default value, and option behaviour (if any) return
 * false if cannot find value in enum or cannot get type
 */
protected static EnumValue parseEnumValue(String name) {
    // get field from enum for its attributes
    Field fi = null;
    try {
        fi = ConfigValues.class.getField(name);
    } catch (Exception ex) {
    // eat this exception. it is not fatal and will be handled like
    // fi==null;
    }
    if (fi == null) {
        // flags.
        if (!name.startsWith(TEMP)) {
            log.warn("Could not find enum value for option: '{}'", name);
        }
        return null;
    } else {
        // get type
        if (fi.isAnnotationPresent(TypeConverterAttribute.class)) {
            final Class<?> fieldType = fi.getAnnotation(TypeConverterAttribute.class).value();
            OptionBehaviourAttribute optionBehaviour = null;
            // get the attribute for default behaviour
            if (fi.isAnnotationPresent(OptionBehaviourAttribute.class)) {
                optionBehaviour = fi.getAnnotation(OptionBehaviourAttribute.class);
            }
            return new EnumValue(fieldType, optionBehaviour);
        } else {
            // if could not get type then cannot continue
            return null;
        }
    }
}
Also used : Field(java.lang.reflect.Field) ConfigValues(org.ovirt.engine.core.common.config.ConfigValues) OptionBehaviourAttribute(org.ovirt.engine.core.common.config.OptionBehaviourAttribute) TypeConverterAttribute(org.ovirt.engine.core.common.config.TypeConverterAttribute)

Aggregations

OptionBehaviourAttribute (org.ovirt.engine.core.common.config.OptionBehaviourAttribute)2 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 ConfigValues (org.ovirt.engine.core.common.config.ConfigValues)1 TypeConverterAttribute (org.ovirt.engine.core.common.config.TypeConverterAttribute)1 Version (org.ovirt.engine.core.compat.Version)1