/** * Validate the given configuration according to the given configuration definition. That is, check that any * required properties in the top-level configuration Map or any sub-Maps, are defined and, in the case of simple * properties, check that they have a non-null value. Optionally, ensure configuration does not alter readOnly * properties already defined in an existingConfiguration. A list of messages describing any errors that were found * is returned. Additionally, any undefined or null simple properties will be assigned a value of "". * * @param configuration the configuration to be validated (must not be null) * @param currentConfiguration if supplied, validate that readOnly properties do not differ between * configuration and existingConfiguration. Ignored if null. * @param configurationDefinition the configuration definition to validate the configuration against (may be null) * * @return a list of messages describing any errors that were found - will be empty if there are no messages */ public static List<String> validateConfiguration(Configuration configuration, Configuration currentConfiguration, ConfigurationDefinition configurationDefinition) { List<String> errorMessages = new ArrayList<String>(); if (configurationDefinition != null) { Map<String, PropertyDefinition> childPropertyDefinitions = configurationDefinition.getPropertyDefinitions(); for (PropertyDefinition childPropertyDefinition : childPropertyDefinitions.values()) { validateProperty(childPropertyDefinition, configuration, currentConfiguration, errorMessages); } } return errorMessages; }
private static void validatePropertyMap(AbstractPropertyMap propertyMap, AbstractPropertyMap currentPropertyMap, PropertyDefinitionMap propertyDefinitionMap, List<String> errorMessages) { // if the entire map is read-only then the new map must match the current map if the current map is non-empty if (propertyDefinitionMap.isReadOnly() && null != currentPropertyMap && !currentPropertyMap.getMap().isEmpty()) { if (!propertyMap.getMap().equals(currentPropertyMap.getMap())) { errorMessages.add("ReadOnly property '" + propertyDefinitionMap.getName() + "' has a value " + propertyMap.getMap() + " different than the current value " + currentPropertyMap.getMap() + "]. It is not allowed to change."); return; } } Map<String, PropertyDefinition> childPropertyDefinitions = propertyDefinitionMap.getMap(); for (PropertyDefinition childPropertyDefinition : childPropertyDefinitions.values()) { validateProperty(childPropertyDefinition, propertyMap, currentPropertyMap, errorMessages); } }