/** * 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. 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 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, ConfigurationDefinition configurationDefinition) { return validateConfiguration(configuration, null, configurationDefinition); }
/** * Given a configuration definition, this will build and return a "default configuration" that * can be validated with the definition. All required properties are set and all properties * that define a default value are also set. If a required property does not have a default * value defined in the definition, the property value will be set to <code>null</code>. * * Use this to help create the definition's default template. * * @param configurationDefinition the configuration definition whose default configuration is to be created * @return configuration the default configuration * * @deprecated use the utility found in the domain module -org.rhq.core.domain.configuration.ConfigurationUtility */ public static Configuration createDefaultConfiguration(ConfigurationDefinition configurationDefinition) { return org.rhq.core.domain.configuration.ConfigurationUtility .createDefaultConfiguration(configurationDefinition); }
/** * "Normalize" the given configuration according to the given configuration definition. That is, for any optional * properties that are not defined in the top-level configuration Map or any sub-Maps, set them. Simple properties * are set with a null value, Map properties as an empty Map, and List properties as an empty List. * * @param configuration the configuration to be normalized * @param configurationDefinition the configuration definition to normalize the configuration against * * @deprecated use the utility found in the domain module -org.rhq.core.domain.configuration.ConfigurationUtility */ public static void normalizeConfiguration(@NotNull Configuration configuration, @Nullable ConfigurationDefinition configurationDefinition) { org.rhq.core.domain.configuration.ConfigurationUtility.normalizeConfiguration(configuration, configurationDefinition, false, false); }
private static void adaptProperty(Property source, Property target, PropertyDefinition def, Object parent, boolean adaptReadonlyProperties) { PropertyType sourceType = conforms(source, def); PropertyType targetType = conforms(target, def); if (sourceType != null && sourceType != PropertyType.UNKNOWN && (sourceType == targetType || targetType == PropertyType.UNKNOWN)) { switch (sourceType) { case MAP: adaptPropertyMap((AbstractPropertyMap) source, (AbstractPropertyMap) target, (PropertyDefinitionMap) def, parent, adaptReadonlyProperties); break; case LIST: adaptPropertyList((PropertyList) source, (PropertyList) target, (PropertyDefinitionList) def, parent, adaptReadonlyProperties); break; case SIMPLE: adaptPropertySimple((PropertySimple) source, (PropertySimple) target, (PropertyDefinitionSimple) def, parent, adaptReadonlyProperties); break; case DYNAMIC: //TODO } } // the types of the properties don't match... let's just leave target as it is because it comes // from the config definition we want... }
private BundleDeployment createBundleDeploymentImpl(Subject subject, BundleVersion bundleVersion, BundleDestination bundleDestination, String name, String description, Configuration configuration) throws Exception { ConfigurationDefinition configDef = bundleVersion.getConfigurationDefinition(); if (null != configDef) { if (null == configuration) { throw new IllegalArgumentException( "Missing Configuration. Configuration is required when the specified BundleVersion defines Configuration Properties."); } // passing in the default configuration will make sure that the readonly properties with the non-null // default values defined in the config def cannot be overridden by the caller. // Those properties are meant to be set by the bundle plugins, not by the user. Configuration defaultConfig = ConfigurationUtility.createDefaultConfiguration(configDef); List<String> errors = ConfigurationUtility.validateConfiguration(configuration, defaultConfig, configDef); if (!errors.isEmpty()) { throw new IllegalArgumentException("Invalid Configuration: " + errors.toString()); } } BundleDeployment deployment = new BundleDeployment(bundleVersion, bundleDestination, name); deployment.setDescription(description); deployment.setConfiguration(configuration); deployment.setSubjectName(subject.getName()); PropertySimple discoveryDelayProperty = configuration.getSimple("org.rhq.discoveryDelay"); if(discoveryDelayProperty != null) { deployment.setDiscoveryDelay(discoveryDelayProperty.getIntegerValue()); } entityManager.persist(deployment); return deployment; }
ConfigurationUtility.normalizeConfiguration(liveConfiguration, configurationDefinition, true, true); List<String> errorMessages = ConfigurationUtility.validateConfiguration( liveConfiguration, configurationDefinition); for (String errorMessage : errorMessages) {
ConfigurationDefinition definition, boolean adaptReadonlyProperties) { Configuration targetConfig = createDefaultConfiguration(definition); if (source != null) { Property target = targetConfig.get(name); adaptProperty(source, target, def, targetConfig, adaptReadonlyProperties);
private static void adaptPropertyList(PropertyList source, PropertyList target, PropertyDefinitionList definition, Object parent, boolean adaptReadonlyProperties) { if ((adaptReadonlyProperties || !definition.isReadOnly())) { if (target == null) { target = new PropertyList(definition.getName()); add(parent, target); } if (target.getList().isEmpty()) { PropertyDefinition memberDef = definition.getMemberDefinition(); for (Property p : source.getList()) { PropertyType type = conforms(p, memberDef); if (type != null && type != PropertyType.UNKNOWN) { Property targetMember = instantiateDefaultProperty(memberDef); target.add(targetMember); adaptProperty(p, targetMember, memberDef, target, adaptReadonlyProperties); } } } } }
private static void adaptPropertyMap(AbstractPropertyMap source, AbstractPropertyMap target, PropertyDefinitionMap definition, Object parent, boolean adaptReadonlyProperties) { if ((adaptReadonlyProperties || !definition.isReadOnly()) && target == null) { target = new PropertyMap(definition.getName()); add(parent, (PropertyMap) target); } for (Map.Entry<String, PropertyDefinition> e : definition.getPropertyDefinitions().entrySet()) { String name = e.getKey(); PropertyDefinition def = e.getValue(); Property sourceChild = source.get(name); // only bother if we have something to adapt from if (sourceChild != null) { Property targetChild = target.get(name); adaptProperty(sourceChild, targetChild, def, target, adaptReadonlyProperties); } } }
/** * 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. 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 * @param configurationDefinition the configuration definition to validate the configuration against * * @return a list of messages describing any errors that were found * * @deprecated use the utility found in the domain module -org.rhq.core.domain.configuration.ConfigurationUtility */ @NotNull public static List<String> validateConfiguration(@NotNull Configuration configuration, @Nullable ConfigurationDefinition configurationDefinition) { return org.rhq.core.domain.configuration.ConfigurationUtility.validateConfiguration(configuration, configurationDefinition); } }
/** * This will populate the given configuration definition with a default template. * A default template will only be created if one or more properties are required * or have default values. If no property definition is required or has a default value, * the default template will remain <code>null</code> in the given config definition. * * Note that if the given configuration definition already has a default template defined * for it, this method is a no-op and will return immediately. * * @param configDef the configuration definition whose default template is to be created and set */ public static void initializeDefaultTemplate(ConfigurationDefinition configDef) { ConfigurationTemplate defaultTemplate = configDef.getDefaultTemplate(); if (defaultTemplate == null) { Configuration defaultConfig = createDefaultConfiguration(configDef); // not everything should have a default template - only stuff that has default values if (!defaultConfig.getProperties().isEmpty()) { defaultTemplate = new ConfigurationTemplate(ConfigurationTemplate.DEFAULT_TEMPLATE_NAME, ConfigurationTemplate.DEFAULT_TEMPLATE_NAME); defaultTemplate.setDefault(true); defaultTemplate.setConfiguration(defaultConfig); configDef.putTemplate(defaultTemplate); } } return; }
ConfigurationUtility.normalizeConfiguration(resource.getPluginConfiguration(), type.getPluginConfigurationDefinition(), false, false);
private OperationDefinition validateOperationNameAndParameters(ResourceType resourceType, String operationName, Configuration parameters) { Set<OperationDefinition> operationDefinitions = resourceType.getOperationDefinitions(); OperationDefinition matchingOperationDefinition = null; for (OperationDefinition operationDefinition : operationDefinitions) { if (operationDefinition.getName().equals(operationName)) { matchingOperationDefinition = operationDefinition; break; } } if (matchingOperationDefinition == null) { throw new IllegalArgumentException("[" + operationName + "] is not a valid operation name for Resources of type [" + resourceType.getName() + "]."); } ConfigurationDefinition parametersDefinition = matchingOperationDefinition .getParametersConfigurationDefinition(); List<String> errors = ConfigurationUtility.validateConfiguration(parameters, parametersDefinition); if (!errors.isEmpty()) { throw new IllegalArgumentException("Parameters for [" + operationName + "] on Resource of type [" + resourceType.getName() + "] are not valid: " + errors); } return matchingOperationDefinition; }
private Configuration mergeExistingResourcePluginConfiguration(Resource resource, Configuration pluginConfig) { // If there is no update necessary just return the current plugin config of the existing resource Configuration result = resource.getPluginConfiguration(); ConfigurationDefinition configDef = resource.getResourceType().getPluginConfigurationDefinition(); if (null == configDef) { return result; } Configuration existingPluginConfig = resource.getPluginConfiguration().deepCopy(false); Configuration defaultPluginConfig = ConfigurationUtility.createDefaultConfiguration(configDef); boolean configChanged = false; // for each property, update the existing plugin config if discovery has set a non-default value for (String propertyName : pluginConfig.getAllProperties().keySet()) { Property discoveredProp = pluginConfig.get(propertyName); Property defaultProp = defaultPluginConfig.get(propertyName); if (!discoveredProp.equals(defaultProp)) { if (log.isDebugEnabled()) { log.debug("Discovery reported a new version of " + resource + ". Updating value of config property" + " from [" + existingPluginConfig.get(propertyName) + "] to [" + discoveredProp + "]."); } existingPluginConfig.put(discoveredProp); configChanged = true; } } if (configChanged) { result = mergeExistingResourcePluginConfigurationOnServer(resource, existingPluginConfig); } return result; }
static Resource createNewResource(DiscoveredResourceDetails details) { // Use a CopyOnWriteArraySet for childResources to allow the field to be concurrently accessed safely // (i.e. to avoid ConcurrentModificationExceptions). Set<Resource> childResources = new CopyOnWriteArraySet<Resource>(); Resource resource = new Resource(childResources); resource.setUuid(UUID.randomUUID().toString()); resource.setResourceKey(details.getResourceKey()); resource.setName(details.getResourceName()); resource.setVersion(details.getResourceVersion()); resource.setDescription(details.getResourceDescription()); resource.setResourceType(details.getResourceType()); Configuration pluginConfiguration = details.getPluginConfiguration(); ConfigurationUtility.normalizeConfiguration(details.getPluginConfiguration(), details.getResourceType() .getPluginConfigurationDefinition(), false, false); resource.setPluginConfiguration(pluginConfiguration); return resource; }
List<String> validationErrors = ConfigurationUtility.validateConfiguration(newPluginConfiguration, existingPluginConfiguration, pluginConfigDef); if (!validationErrors.isEmpty()) {
List<String> validationErrors = ConfigurationUtility.validateConfiguration(newResourceConfiguration, existingResourceConfiguration, resourceConfigDef); if (!validationErrors.isEmpty()) {
config.remove(ENABLED_ATTRIBUTE); List<String> validationErrors = ConfigurationUtility.validateConfiguration(config, configDef);