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

org.cattleframework.cloud.discovery.enhancement.context.DataContext 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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据上下文
 * 
 * @author orange
 *
 */
public class DataContext {

    private static final String FRAGMENT_TRANSITIVE = "transitive";

    private static final String FRAGMENT_DISPOSABLE = "disposable";

    private static final String FRAGMENT_UPSTREAM_DISPOSABLE = "upstream-disposable";

    private static final String FRAGMENT_RAW_TRANSHEADERS = "trans-headers";

    private static final String FRAGMENT_RAW_TRANSHEADERS_KV = "trans-headers-kv";

    private final Map> fragmentContexts;

    private final Map loadbalancerData;

    public DataContext() {
	fragmentContexts = new ConcurrentHashMap>();
	loadbalancerData = new ConcurrentHashMap();
    }

    public Map getCustomMetadata() {
	Map transitiveMetadata = getTransitiveMetadata();
	Map disposableMetadata = getDisposableMetadata();
	Map customMetadata = new HashMap(0);
	transitiveMetadata.forEach((key, value) -> {
	    if (!disposableMetadata.containsKey(key)) {
		customMetadata.put(key, value);
	    }
	});
	return Collections.unmodifiableMap(customMetadata);
    }

    public void setTransitiveMetadata(Map transitiveMetadata) {
	putFragmentContext(FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(transitiveMetadata));
    }

    public void setDisposableMetadata(Map disposableMetadata) {
	this.putFragmentContext(FRAGMENT_DISPOSABLE, Collections.unmodifiableMap(disposableMetadata));
    }

    public Map getDisposableMetadata() {
	return getFragmentContext(FRAGMENT_DISPOSABLE);
    }

    public Map getTransitiveMetadata() {
	return getFragmentContext(FRAGMENT_TRANSITIVE);
    }

    public Map getFragmentContext(String fragment) {
	Map fragmentContext = fragmentContexts.get(fragment);
	if (fragmentContext == null) {
	    return Collections.emptyMap();
	}
	return Collections.unmodifiableMap(fragmentContext);
    }

    public void putFragmentContext(String fragment, Map context) {
	fragmentContexts.put(fragment, context);
    }

    public void setUpstreamDisposableMetadata(Map upstreamDisposableMetadata) {
	putFragmentContext(FRAGMENT_UPSTREAM_DISPOSABLE, Collections.unmodifiableMap(upstreamDisposableMetadata));
    }

    public void setTransHeader(String key, String value) {
	this.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, key, value);
    }

    public Map getTransHeaders() {
	return this.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
    }

    public void setTransHeaders(String key, String value) {
	this.putContext(FRAGMENT_RAW_TRANSHEADERS, key, value);
    }

    public void putContext(String fragment, String key, String value) {
	Map fragmentContext = fragmentContexts.get(fragment);
	if (fragmentContext == null) {
	    fragmentContext = new ConcurrentHashMap<>(0);
	    fragmentContexts.put(fragment, fragmentContext);
	}
	fragmentContext.put(key, value);
    }

    public Map getTransHeaderMap() {
	return getFragmentContext(FRAGMENT_RAW_TRANSHEADERS_KV);
    }

    public Map getLoadbalancerDataMap() {
	return loadbalancerData;
    }

    public void setLoadbalancerData(String key, Object value) {
	loadbalancerData.put(key, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy