org.cattleframework.cloud.discovery.enhancement.context.DataContextHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-cloud-discovery-common Show documentation
Show all versions of cattle-cloud-discovery-common Show documentation
Cattle framework cloud discovery common component pom
/*
* 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 org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.cattleframework.aop.context.SpringContext;
/**
* 数据上下文持有者
*
* @author orange
*
*/
public final class DataContextHolder {
private static final ThreadLocal DATA_CONTEXT = new InheritableThreadLocal();
private static StaticDataManager staticDataManager;
private DataContextHolder() {
}
public static DataContext get() {
if (DATA_CONTEXT.get() != null) {
return DATA_CONTEXT.get();
}
if (staticDataManager == null) {
staticDataManager = SpringContext.get().getBeanFactory().getBean(StaticDataManager.class);
}
DataContext dataContext = new DataContext();
dataContext.setTransitiveMetadata(staticDataManager.getMergedStaticTransitiveMetadata());
dataContext.setDisposableMetadata(staticDataManager.getMergedStaticDisposableMetadata());
String transHeader = staticDataManager.getTransHeader();
if (StringUtils.isNotBlank(transHeader)) {
dataContext.setTransHeaders(transHeader, "");
}
DATA_CONTEXT.set(dataContext);
return DATA_CONTEXT.get();
}
public static void init(Map dynamicTransitiveMetadata,
Map dynamicDisposableMetadata) {
remove();
DataContext dataContext = get();
if (MapUtils.isNotEmpty(dynamicTransitiveMetadata)) {
Map staticTransitiveMetadata = dataContext.getTransitiveMetadata();
Map mergedTransitiveMetadata = new HashMap(0);
mergedTransitiveMetadata.putAll(staticTransitiveMetadata);
mergedTransitiveMetadata.putAll(dynamicTransitiveMetadata);
dataContext.setTransitiveMetadata(Collections.unmodifiableMap(mergedTransitiveMetadata));
}
if (MapUtils.isNotEmpty(dynamicDisposableMetadata)) {
Map mergedUpstreamDisposableMetadata = new HashMap<>(dynamicDisposableMetadata);
dataContext.setUpstreamDisposableMetadata(Collections.unmodifiableMap(mergedUpstreamDisposableMetadata));
}
Map staticDisposableMetadata = dataContext.getDisposableMetadata();
dataContext.setDisposableMetadata(Collections.unmodifiableMap(staticDisposableMetadata));
set(dataContext);
}
public static void set(DataContext dataContext) {
DATA_CONTEXT.set(dataContext);
}
public static void remove() {
DATA_CONTEXT.remove();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy