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

org.cattleframework.cloud.discovery.enhancement.context.StaticDataManager Maven / Gradle / Ivy

There is a newer version: 1.0.1-M5
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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 org.cattleframework.cloud.discovery.enhancement.context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.cattleframework.cloud.discovery.DiscoveryInfoProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 静态数据管理器
 * 
 * @author orange
 *
 */
public class StaticDataManager {

    private static final Logger logger = LoggerFactory.getLogger(StaticDataManager.class);

    private static final String ENV_METADATA_PREFIX = "METADATA_CONTENT_";

    private static final int ENV_METADATA_PREFIX_LENGTH = ENV_METADATA_PREFIX.length();

    private static final String ENV_METADATA_CONTENT_TRANSITIVE = "METADATA_CONTENT_TRANSITIVE";

    private static final String ENV_METADATA_CONTENT_DISPOSABLE = "METADATA_CONTENT_DISPOSABLE";

    private static final String ENV_TRANSHEADERS = "TRANSHEADERS";

    private Map envMetadata;

    private Map envTransitiveMetadata;

    private Map envDisposableMetadata;

    private String envTransHeaders;

    private Map configMetadata;

    private Map configTransitiveMetadata;

    private Map configDisposableMetadata;

    private String configTransHeaders;

    private Map mergedStaticMetadata;

    private Map mergedStaticTransitiveMetadata;

    private Map mergedStaticDisposableMetadata;

    public StaticDataManager(DiscoveryInfoProperties discoveryInfoProperties) {
	parseConfigMetadata(discoveryInfoProperties);
	parseEnvMetadata();
	merge();
	logger.info("Loaded static metadata info. {}", this);
    }

    private void parseEnvMetadata() {
	Map allEnvs = System.getenv();
	envMetadata = new HashMap(0);
	for (Map.Entry entry : allEnvs.entrySet()) {
	    String key = entry.getKey();
	    if (StringUtils.isBlank(key) || ENV_METADATA_CONTENT_TRANSITIVE.equals(key)
		    || ENV_METADATA_CONTENT_DISPOSABLE.equals(key)) {
		continue;
	    }
	    String value = entry.getValue();
	    if (key.startsWith(ENV_METADATA_PREFIX) || ENV_TRANSHEADERS.equals(key)) {
		String sourceKey = "";
		if (ENV_TRANSHEADERS.equals(key)) {
		    if (StringUtils.isNotBlank(value)) {
			envTransHeaders = value;
		    }
		} else {
		    sourceKey = StringUtils.substring(key, ENV_METADATA_PREFIX_LENGTH);
		    envMetadata.put(sourceKey, value);
		}
		logger.info("resolve metadata from env. key = {}, value = {}", sourceKey, value);
	    }
	}
	envMetadata = Collections.unmodifiableMap(envMetadata);
	envTransitiveMetadata = new HashMap(0);
	// parse transitive metadata
	String transitiveKeys = allEnvs.get(ENV_METADATA_CONTENT_TRANSITIVE);
	if (StringUtils.isNotBlank(transitiveKeys)) {
	    String[] keyArr = StringUtils.split(transitiveKeys, ",");
	    if (keyArr != null && keyArr.length > 0) {
		for (String key : keyArr) {
		    String value = envMetadata.get(key);
		    if (StringUtils.isNotBlank(value)) {
			envTransitiveMetadata.put(key, value);
		    }
		}
	    }
	}
	envTransitiveMetadata = Collections.unmodifiableMap(envTransitiveMetadata);
	envDisposableMetadata = new HashMap(0);
	String disposableKeys = allEnvs.get(ENV_METADATA_CONTENT_DISPOSABLE);
	if (StringUtils.isNotBlank(disposableKeys)) {
	    String[] keyArr = StringUtils.split(disposableKeys, ",");
	    if (keyArr != null && keyArr.length > 0) {
		for (String key : keyArr) {
		    String value = envMetadata.get(key);
		    if (StringUtils.isNotBlank(value)) {
			envDisposableMetadata.put(key, value);
		    }
		}
	    }
	}
	envDisposableMetadata = Collections.unmodifiableMap(envDisposableMetadata);
    }

    private void parseConfigMetadata(DiscoveryInfoProperties discoveryInfoProperties) {
	Map allMetadata = discoveryInfoProperties.getMetadata().getContent();
	List transitiveKeys = discoveryInfoProperties.getMetadata().getTransitive();
	List disposableKeys = discoveryInfoProperties.getMetadata().getDisposable();
	List headers = discoveryInfoProperties.getHeaders();
	Map transitiveResult = new HashMap(0);
	for (String key : transitiveKeys) {
	    if (allMetadata.containsKey(key)) {
		transitiveResult.put(key, allMetadata.get(key));
	    }
	}
	Map disposableResult = new HashMap(0);
	for (String key : disposableKeys) {
	    if (allMetadata.containsKey(key)) {
		disposableResult.put(key, allMetadata.get(key));
	    }
	}
	configTransitiveMetadata = Collections.unmodifiableMap(transitiveResult);
	configDisposableMetadata = Collections.unmodifiableMap(disposableResult);
	configTransHeaders = CollectionUtils.isEmpty(headers) ? null : String.join(",", headers);
	configMetadata = Collections.unmodifiableMap(allMetadata);
    }

    private void merge() {
	Map mergedMetadataResult = new HashMap(0);
	mergedMetadataResult.putAll(configMetadata);
	mergedMetadataResult.putAll(envMetadata);
	mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult);
	Map mergedTransitiveMetadataResult = new HashMap(0);
	mergedTransitiveMetadataResult.putAll(configTransitiveMetadata);
	mergedTransitiveMetadataResult.putAll(envTransitiveMetadata);
	mergedStaticTransitiveMetadata = Collections.unmodifiableMap(mergedTransitiveMetadataResult);
	Map mergedDisposableMetadataResult = new HashMap(0);
	mergedDisposableMetadataResult.putAll(configDisposableMetadata);
	mergedDisposableMetadataResult.putAll(envDisposableMetadata);
	mergedStaticDisposableMetadata = Collections.unmodifiableMap(mergedDisposableMetadataResult);
    }

    public Map getMergedStaticTransitiveMetadata() {
	return mergedStaticTransitiveMetadata;
    }

    public Map getMergedStaticDisposableMetadata() {
	return mergedStaticDisposableMetadata;
    }

    public String getTransHeader() {
	Set transHeaderSet = new HashSet();
	Set transHeaderFromEnvSet = StringUtils.isNotBlank(envTransHeaders)
		? Arrays.stream(envTransHeaders.split(",")).collect(Collectors.toSet())
		: Collections.emptySet();
	Set transHeaderFromConfigSet = StringUtils.isNotBlank(configTransHeaders)
		? Arrays.stream(configTransHeaders.split(",")).collect(Collectors.toSet())
		: Collections.emptySet();
	transHeaderSet.addAll(transHeaderFromEnvSet);
	transHeaderSet.addAll(transHeaderFromConfigSet);
	return new ArrayList(transHeaderSet).stream().sorted().collect(Collectors.joining(","));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy