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

io.streamthoughts.kafka.specs.GlobalSpecsContext Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021 StreamThoughts.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.streamthoughts.kafka.specs;

import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.streamthoughts.kafka.specs.internal.PropertiesUtils.fromProperties;

public class GlobalSpecsContext {

    private final Map system;

    private final Map labels;

    /**
     * Creates a new {@link GlobalSpecsContext} instance.
     */
    public GlobalSpecsContext() {
        this(new LinkedHashMap<>());
    }

    /**
     * Creates a new {@link GlobalSpecsContext} instance.
     *
     * @param labels    a list of labels.
     */
    public GlobalSpecsContext(@NotNull final Map labels) {
        this.system = new HashMap<>();
        this.system.put("props", fromProperties(System.getProperties()));
        this.system.put("env",  System.getenv());
        this.labels = labels;
    }

    public GlobalSpecsContext labels(final Map labels) {
        this.labels.putAll(labels);
        return this;
    }

    public Map getLabels() {
        return explode(labels);
    }

    public Map getSystem() {
        return system;
    }


    @Override
    public String toString() {
        return "[" +
                ", system=" + system +
                ", labels=" + labels +
                ']';
    }

    private static Map explode(final Map map) {
        final Stream> stream = map.entrySet()
                .stream()
                .map(entry -> {
                    if (entry.getKey().contains(".")) {
                        final String[] split = splitPath(entry.getKey());
                        final  Map nested = explode(Collections.singletonMap(split[1], entry.getValue()));
                        return Map.entry(split[0], nested);
                    }
                    else if (entry.getValue() instanceof Map) {
                        return Map.entry(entry.getKey(), explode((Map)entry.getValue()));
                    }
                    return entry;
                });

        return stream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    private static String[] splitPath(final String key) {
        return key.split("\\.", 2);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy