You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.4 KiB
117 lines
4.4 KiB
package com.fr.env; |
|
|
|
import com.fanruan.repository.ConfigRepository; |
|
import com.fr.config.dao.DaoContext; |
|
import com.fr.config.dao.impl.remote.RemoteConfigOperable; |
|
import com.fr.config.utils.ConfData; |
|
import com.fr.config.utils.ConfigReadUtils; |
|
import com.fr.config.utils.PrefixHandler; |
|
import com.fr.config.utils.SetupDataOperator; |
|
import com.fr.nx.app.web.out.widget.utils.CollectionUtils; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.tenant.context.TenantContext; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
import java.util.stream.Collectors; |
|
|
|
import static com.sun.org.apache.xalan.internal.xsltc.compiler.Constants.STRING; |
|
|
|
/** |
|
* 设计器远程下获取Updata的操作类 |
|
* |
|
* @author Destiny.Lin |
|
* @since 11.0 |
|
* Created on 2024/10/14 |
|
*/ |
|
public class SetupDataDesignerRemoteOperator implements SetupDataOperator { |
|
|
|
private static final SetupDataDesignerRemoteOperator INSTANCE = new SetupDataDesignerRemoteOperator(); |
|
|
|
private static final String EMPTY_STRING = ""; |
|
|
|
/** |
|
* 单例 |
|
*/ |
|
public static SetupDataDesignerRemoteOperator getInstance() { |
|
return INSTANCE; |
|
} |
|
|
|
/** |
|
* 批量拉取配置 |
|
* @param nameSpaces |
|
*/ |
|
public void fetchBatchConfDatas(List<String> nameSpaces) { |
|
if(nameSpaces.isEmpty()){ |
|
return; |
|
} |
|
List<String> namespaceList = nameSpaces.stream() |
|
.filter(namespace -> !checkRemoteConfigCacheExistence(namespace)) |
|
.collect(Collectors.toList()); |
|
if(CollectionUtils.isEmpty(namespaceList)){ |
|
return; |
|
} |
|
ConfigRepository.getInstance().batchGetConfigsByConfigsCache(namespaceList, () -> { |
|
batchSaveConfigs(namespaceList, TenantContext.getCurrentWithException().getId()); |
|
return null; |
|
}); |
|
} |
|
|
|
private void batchSaveConfigs(List<String> nameSpaces, String tenantId) { |
|
nameSpaces.forEach(namespace -> { |
|
String prefix = PrefixHandler.concatPrefix(namespace, EMPTY_STRING); |
|
ConfigReadUtils.getData(prefix, tenantId); |
|
ConfigReadUtils.getClassInfo(prefix, tenantId); |
|
}); |
|
} |
|
|
|
@Override |
|
public ConfData getData(String prefix, String tenantId) { |
|
String configNamespace = getConfigNameSpace(prefix); |
|
if (checkRemoteConfigCacheExistence(configNamespace)) { |
|
return createConfData(prefix, tenantId); |
|
} |
|
return ConfigRepository.getInstance().getConfigByConfigsCache(configNamespace, () -> createConfData(prefix, tenantId)); |
|
} |
|
|
|
private ConfData createConfData(String prefix, String tenantId) { |
|
ConfData data = new ConfData(); |
|
data.setDataMap(ConfigReadUtils.getData(prefix, tenantId)); |
|
data.setMap(ConfigReadUtils.getClassInfo(prefix, tenantId)); |
|
return data; |
|
} |
|
|
|
private String getConfigNameSpace(String id) { |
|
|
|
if (StringUtils.isEmpty(id)) { |
|
throw new IllegalArgumentException("id cannot be null"); |
|
} |
|
int length = id.length(); |
|
for (int i = 0; i < length; i++) { |
|
if (PrefixHandler.SEPERATOR == id.charAt(i)) { |
|
return id.substring(0, i); |
|
} |
|
} |
|
throw new IllegalArgumentException("cannot resolve namespace of " + id); |
|
} |
|
|
|
/** |
|
* 检查远程配置缓存是否存在 |
|
* <p> |
|
* 该方法通过判断三个远程配置相关的DAO(EntityDao、XmlEntityDao、ClassHelperDao)是否在缓存中存在指定的命名空间。 |
|
* 如果DAO实现了RemoteConfigOperable接口,则会依次检查缓存。 |
|
* 如果DAO不支持远程缓存检查,返回false。 |
|
* |
|
* @param nameSpace 命名空间 |
|
* @return 如果三个远程DAO的缓存中都存在指定命名空间的数据,返回true;否则返回false |
|
*/ |
|
private boolean checkRemoteConfigCacheExistence(String nameSpace) { |
|
// 只判断一次,所有远程的Dao都实现了RemoteConfigOperable接口 |
|
if (DaoContext.getEntityDao() instanceof RemoteConfigOperable) { |
|
return ((RemoteConfigOperable) DaoContext.getEntityDao()).checkCacheExistence(nameSpace) && |
|
((RemoteConfigOperable) DaoContext.getXmlEntityDao()).checkCacheExistence(nameSpace) && |
|
((RemoteConfigOperable) DaoContext.getClassHelperDao()).checkCacheExistence(nameSpace); |
|
} |
|
// 非远程配置不支持设计器缓存检查,返回false |
|
return false; |
|
} |
|
}
|
|
|