All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.config.hocon.HoconConfigIncluder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2022 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.config.hocon;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import io.helidon.config.spi.ConfigParserException;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigIncludeContext;
import com.typesafe.config.ConfigIncluder;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigParseOptions;

import static java.lang.System.Logger.Level.TRACE;

class HoconConfigIncluder implements ConfigIncluder {
    private static final System.Logger LOGGER = System.getLogger(HoconConfigIncluder.class.getName());
    private static final String HOCON_EXTENSION = ".conf";

    private ConfigParseOptions parseOptions;
    private Function> relativeResourceFunction;
    private Charset charset;

    HoconConfigIncluder() {
    }

    @Override
    public ConfigIncluder withFallback(ConfigIncluder fallback) {
        return this;
    }

    @Override
    public ConfigObject include(ConfigIncludeContext context, String what) {
        LOGGER.log(TRACE, String.format("Received request to include resource %s, %s",
                                        what, context.parseOptions().getOriginDescription()));
        Optional maybeStream = relativeResourceFunction.apply(patchName(what));
        if (maybeStream.isEmpty()) {
            if (Objects.nonNull(context.parseOptions()) && !context.parseOptions().getAllowMissing()) {
                throw new ConfigParserException(what + " is missing");
            }
            return ConfigFactory.empty().root();
        }

        try (InputStreamReader readable = new InputStreamReader(maybeStream.get(), charset)) {
            return ConfigFactory.parseReader(readable, parseOptions).root();
        } catch (IOException e) {
            throw new ConfigParserException("Failed to read from source: " + what, e);
        }
    }

    void charset(Charset charset) {
        this.charset = charset;
    }

    void parseOptions(ConfigParseOptions parseOptions) {
        this.parseOptions = parseOptions;
    }

    void relativeResourceFunction(Function> relativeResourceFunction) {
        this.relativeResourceFunction = relativeResourceFunction;
    }

    /**
     * Adds default Hocon extension if not present.
     *
     * @param what file name
     * @return file name with extension
     */
    static String patchName(String what) {
        Optional base = Optional.of(what)
                .filter(f -> f.contains(File.separator))
                .map(f -> f.substring(f.lastIndexOf(File.separator) + 1));
        Optional ext = Optional.of(base.orElse(what))
                .filter(f -> f.contains("."))
                .map(f -> f.substring(f.lastIndexOf(".") + 1));
        return ext.isPresent() ? what : what + HOCON_EXTENSION;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy