Search in sources :

Example 1 with YamlMapping

use of com.hazelcast.internal.yaml.YamlMapping in project hazelcast by hazelcast.

the class YamlClientConfigBuilder method parseAndBuildConfig.

private void parseAndBuildConfig(ClientConfig config) throws Exception {
    YamlMapping yamlRootNode;
    try {
        yamlRootNode = ((YamlMapping) YamlLoader.load(in));
    } catch (Exception ex) {
        throw new InvalidConfigurationException("Invalid YAML configuration", ex);
    }
    YamlNode clientRoot = yamlRootNode.childAsMapping(ClientConfigSections.HAZELCAST_CLIENT.getName());
    if (clientRoot == null) {
        clientRoot = yamlRootNode;
    }
    YamlDomChecker.check(clientRoot);
    Node w3cRootNode = asW3cNode(clientRoot);
    replaceVariables(w3cRootNode);
    importDocuments(clientRoot);
    if (shouldValidateTheSchema()) {
        new YamlConfigSchemaValidator().validate((YamlMapping) clientRoot.parent());
    }
    new YamlClientDomConfigProcessor(true, config).buildConfig(w3cRootNode);
}
Also used : YamlConfigSchemaValidator(com.hazelcast.internal.config.YamlConfigSchemaValidator) YamlNode(com.hazelcast.internal.yaml.YamlNode) W3cDomUtil.asW3cNode(com.hazelcast.internal.config.yaml.W3cDomUtil.asW3cNode) Node(org.w3c.dom.Node) YamlNode(com.hazelcast.internal.yaml.YamlNode) YamlMapping(com.hazelcast.internal.yaml.YamlMapping) YamlClientDomConfigProcessor(com.hazelcast.client.config.impl.YamlClientDomConfigProcessor) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) IOException(java.io.IOException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 2 with YamlMapping

use of com.hazelcast.internal.yaml.YamlMapping in project hazelcast by hazelcast.

the class YamlConfigSchemaValidator method validateAdditionalProperties.

private void validateAdditionalProperties(YamlMapping rootNode, String hzConfigRootNodeName) {
    if (!PERMITTED_ROOT_NODES.contains(hzConfigRootNodeName)) {
        throw new IllegalArgumentException(hzConfigRootNodeName);
    }
    ObjectSchema schema = (ObjectSchema) SCHEMA;
    Set<String> forbiddenRootPropNames = ((ObjectSchema) schema.getPropertySchemas().get(hzConfigRootNodeName)).getPropertySchemas().keySet();
    List<String> misIndentedRootProps = new ArrayList<>();
    rootNode.children().forEach(yamlNode -> {
        if (forbiddenRootPropNames.contains(yamlNode.nodeName())) {
            misIndentedRootProps.add(yamlNode.nodeName());
        }
    });
    if (misIndentedRootProps.isEmpty()) {
        return;
    }
    if (misIndentedRootProps.size() == 1) {
        String propName = misIndentedRootProps.get(0);
        throw createExceptionForMisIndentedConfigProp(propName, true);
    } else {
        List<SchemaViolationConfigurationException> causes = misIndentedRootProps.stream().map(prop -> createExceptionForMisIndentedConfigProp(prop, false)).collect(toList());
        throw new SchemaViolationConfigurationException(withNote(causes.size() + " schema violations found"), "#", "#", causes);
    }
}
Also used : PrimitiveValidationStrategy(org.everit.json.schema.PrimitiveValidationStrategy) ValidationException(org.everit.json.schema.ValidationException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Collections.emptyList(java.util.Collections.emptyList) Validator(org.everit.json.schema.Validator) HazelcastProperties(com.hazelcast.spi.properties.HazelcastProperties) JSONTokener(org.json.JSONTokener) YamlMapping(com.hazelcast.internal.yaml.YamlMapping) Set(java.util.Set) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) YamlToJsonConverter(com.hazelcast.internal.yaml.YamlToJsonConverter) JSONObject(org.json.JSONObject) ObjectSchema(org.everit.json.schema.ObjectSchema) Arrays.asList(java.util.Arrays.asList) HazelcastProperty(com.hazelcast.spi.properties.HazelcastProperty) Schema(org.everit.json.schema.Schema) Versions(com.hazelcast.internal.cluster.Versions) SchemaLoader(org.everit.json.schema.loader.SchemaLoader) ObjectSchema(org.everit.json.schema.ObjectSchema) ArrayList(java.util.ArrayList)

Example 3 with YamlMapping

use of com.hazelcast.internal.yaml.YamlMapping in project hazelcast by hazelcast.

the class AbstractYamlConfigBuilder method fillReplacerProperties.

private void fillReplacerProperties(Node node, Properties properties) {
    YamlMapping propertiesMapping = asMapping(((YamlElementAdapter) node).getYamlNode());
    for (YamlNameNodePair childNodePair : propertiesMapping.childrenPairs()) {
        String childName = childNodePair.nodeName();
        YamlNode child = childNodePair.childNode();
        Object nodeValue = asScalar(child).nodeValue();
        properties.put(childName, nodeValue != null ? nodeValue.toString() : "");
    }
}
Also used : YamlNode(com.hazelcast.internal.yaml.YamlNode) MutableYamlMapping(com.hazelcast.internal.yaml.MutableYamlMapping) YamlMapping(com.hazelcast.internal.yaml.YamlMapping) YamlNameNodePair(com.hazelcast.internal.yaml.YamlNameNodePair)

Example 4 with YamlMapping

use of com.hazelcast.internal.yaml.YamlMapping in project hazelcast by hazelcast.

the class YamlConfigBuilder method parseAndBuildConfig.

private void parseAndBuildConfig(Config config) throws Exception {
    YamlMapping yamlRootNode;
    try {
        yamlRootNode = ((YamlMapping) YamlLoader.load(in));
    } catch (Exception ex) {
        throw new InvalidConfigurationException("Invalid YAML configuration", ex);
    }
    YamlNode imdgRoot = yamlRootNode.childAsMapping(ConfigSections.HAZELCAST.getName());
    if (imdgRoot == null) {
        imdgRoot = yamlRootNode;
    }
    YamlDomChecker.check(imdgRoot);
    Node w3cRootNode = asW3cNode(imdgRoot);
    replaceVariables(w3cRootNode);
    importDocuments(imdgRoot);
    if (shouldValidateTheSchema()) {
        new YamlConfigSchemaValidator().validate((YamlMapping) imdgRoot.parent());
    }
    new YamlMemberDomConfigProcessor(true, config).buildConfig(w3cRootNode);
}
Also used : YamlConfigSchemaValidator(com.hazelcast.internal.config.YamlConfigSchemaValidator) YamlNode(com.hazelcast.internal.yaml.YamlNode) W3cDomUtil.asW3cNode(com.hazelcast.internal.config.yaml.W3cDomUtil.asW3cNode) Node(org.w3c.dom.Node) YamlNode(com.hazelcast.internal.yaml.YamlNode) YamlMemberDomConfigProcessor(com.hazelcast.internal.config.YamlMemberDomConfigProcessor) YamlMapping(com.hazelcast.internal.yaml.YamlMapping) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with YamlMapping

use of com.hazelcast.internal.yaml.YamlMapping in project hazelcast by hazelcast.

the class AbstractYamlConfigBuilder method importDocuments.

/**
 * Imports external YAML documents into the provided main YAML document.
 * <p>
 * Since the YAML configuration uses mappings, in order to keep the
 * configuration defined in the main YAML document the imported
 * document (the source) will be actually merged into the main
 * document (the target). An example to it is defining one map in the
 * main document, and another map in the imported document. In this
 * case the documents should be merged to include both map configurations
 * under the {@code root/map} node.
 *
 * @param imdgRoot The root of the main YAML configuration document
 * @throws Exception If a YAML document to be imported can't be loaded
 * @see #merge(YamlNode, YamlNode)
 */
protected void importDocuments(YamlNode imdgRoot) throws Exception {
    YamlMapping rootAsMapping = asMapping(imdgRoot);
    YamlSequence importSeq = rootAsMapping.childAsSequence(ConfigSections.IMPORT.getName());
    if (importSeq == null || importSeq.childCount() == 0) {
        return;
    }
    for (YamlNode importNode : importSeq.children()) {
        String resource = asScalar(importNode).nodeValue();
        URL url = ConfigLoader.locateConfig(resource);
        if (url == null) {
            throw new InvalidConfigurationException("Failed to load resource: " + resource);
        }
        if (!currentlyImportedFiles.add(url.getPath())) {
            throw new InvalidConfigurationException("Cyclic loading of resource '" + url.getPath() + "' detected!");
        }
        YamlNode rootLoaded;
        try (InputStream inputStream = url.openStream()) {
            rootLoaded = YamlLoader.load(inputStream);
        } catch (Exception ex) {
            throw new InvalidConfigurationException("Loading YAML document from resource " + url.getPath() + " failed", ex);
        }
        YamlNode imdgRootLoaded = asMapping(rootLoaded).child(getConfigRoot());
        if (imdgRootLoaded == null) {
            imdgRootLoaded = rootLoaded;
        }
        replaceVariables(asW3cNode(imdgRootLoaded));
        importDocuments(imdgRootLoaded);
        // we need to merge and not just substitute with the content of the imported document
        // YAML documents define mappings where the name of the nodes should be unique
        merge(imdgRootLoaded, imdgRoot);
    }
    replaceVariables(asW3cNode(imdgRoot));
    ((MutableYamlMapping) rootAsMapping).removeChild(ConfigSections.IMPORT.getName());
}
Also used : YamlSequence(com.hazelcast.internal.yaml.YamlSequence) MutableYamlSequence(com.hazelcast.internal.yaml.MutableYamlSequence) MutableYamlMapping(com.hazelcast.internal.yaml.MutableYamlMapping) YamlNode(com.hazelcast.internal.yaml.YamlNode) InputStream(java.io.InputStream) MutableYamlMapping(com.hazelcast.internal.yaml.MutableYamlMapping) YamlMapping(com.hazelcast.internal.yaml.YamlMapping) URL(java.net.URL)

Aggregations

YamlMapping (com.hazelcast.internal.yaml.YamlMapping)9 YamlNode (com.hazelcast.internal.yaml.YamlNode)7 YamlConfigSchemaValidator (com.hazelcast.internal.config.YamlConfigSchemaValidator)4 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)3 W3cDomUtil.asW3cNode (com.hazelcast.internal.config.yaml.W3cDomUtil.asW3cNode)3 IOException (java.io.IOException)3 Node (org.w3c.dom.Node)3 MutableYamlMapping (com.hazelcast.internal.yaml.MutableYamlMapping)2 YamlNameNodePair (com.hazelcast.internal.yaml.YamlNameNodePair)2 YamlSequence (com.hazelcast.internal.yaml.YamlSequence)2 YamlClientDomConfigProcessor (com.hazelcast.client.config.impl.YamlClientDomConfigProcessor)1 YamlClientFailoverDomConfigProcessor (com.hazelcast.client.config.impl.YamlClientFailoverDomConfigProcessor)1 HazelcastException (com.hazelcast.core.HazelcastException)1 Versions (com.hazelcast.internal.cluster.Versions)1 SchemaViolationConfigurationException (com.hazelcast.internal.config.SchemaViolationConfigurationException)1 YamlMemberDomConfigProcessor (com.hazelcast.internal.config.YamlMemberDomConfigProcessor)1 MutableYamlSequence (com.hazelcast.internal.yaml.MutableYamlSequence)1 YamlScalar (com.hazelcast.internal.yaml.YamlScalar)1 YamlToJsonConverter (com.hazelcast.internal.yaml.YamlToJsonConverter)1 HazelcastProperties (com.hazelcast.spi.properties.HazelcastProperties)1