Search in sources :

Example 1 with Backend

use of com.torodb.stampede.config.model.backend.Backend in project torodb by torodb.

the class CliConfigUtils method uncatchedReadConfig.

private static Config uncatchedReadConfig(final CliConfig cliConfig) throws Exception {
    ObjectMapper objectMapper = ConfigUtils.mapper();
    Config defaultConfig = new Config();
    ObjectNode configNode = (ObjectNode) objectMapper.valueToTree(defaultConfig);
    if (cliConfig.hasConfFile() || cliConfig.hasXmlConfFile()) {
        ObjectMapper mapper = null;
        InputStream inputStream = null;
        if (cliConfig.hasConfFile()) {
            mapper = ConfigUtils.yamlMapper();
            inputStream = cliConfig.getConfInputStream();
        } else if (cliConfig.hasXmlConfFile()) {
            mapper = ConfigUtils.xmlMapper();
            inputStream = cliConfig.getXmlConfInputStream();
        }
        if (inputStream != null) {
            Config config = mapper.readValue(inputStream, Config.class);
            configNode = mapper.valueToTree(config);
        }
    }
    if (cliConfig.getBackend() != null) {
        Backend backend = new Backend(CliConfig.getBackendClass(cliConfig.getBackend()).newInstance());
        ObjectNode backendNode = (ObjectNode) objectMapper.valueToTree(backend);
        configNode.set("backend", backendNode);
    }
    if (cliConfig.getParams() != null) {
        YAMLMapper yamlMapper = ConfigUtils.yamlMapper();
        for (String paramPathValue : cliConfig.getParams()) {
            int paramPathValueSeparatorIndex = paramPathValue.indexOf('=');
            String pathAndProp = paramPathValue.substring(0, paramPathValueSeparatorIndex);
            if (pathAndProp.startsWith("/")) {
                pathAndProp = pathAndProp.substring(1);
            }
            pathAndProp = "/" + pathAndProp;
            String value = paramPathValue.substring(paramPathValueSeparatorIndex + 1);
            configNode = ConfigUtils.mergeParam(yamlMapper, configNode, pathAndProp, value);
        }
    }
    Config config = objectMapper.treeToValue(configNode, Config.class);
    validateBean(config);
    return config;
}
Also used : Backend(com.torodb.stampede.config.model.backend.Backend) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Config(com.torodb.stampede.config.model.Config) InputStream(java.io.InputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 Config (com.torodb.stampede.config.model.Config)1 Backend (com.torodb.stampede.config.model.backend.Backend)1 InputStream (java.io.InputStream)1