Commit 634d315f by 李秋伟

Merge branch 'feature_protocol' into 'master'

优化提示信息

See merge request mall/arch/matrix!44
parents d8e55826 511ef2ca
package com.secoo.mall.common.util.reflect;
import org.apache.commons.lang3.reflect.FieldUtils;
public class FieldUtil extends FieldUtils {
}
......@@ -5,6 +5,7 @@ import com.ctrip.framework.apollo.ConfigService;
import com.google.common.collect.Lists;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.util.colletion.CollectionUtil;
import com.secoo.mall.common.util.reflect.FieldUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.datasource.config.MatrixDataSourceConfig;
import com.secoo.mall.datasource.constant.DataSourceConstant;
......@@ -42,28 +43,33 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
if (CollectionUtil.isEmpty(propertyNames)) {
throw new BusinessException(DataSourceError.APOLLO_CONFIG_NOT_EXIST);
}
//用数据源名字为key,进行分组
Map<String, List<String>> propertyMap = propertyNames.stream().filter(pro -> pro.contains(MatrixDataSourceProperties.PREFIX)).map(pro -> pro.replace(MatrixDataSourceProperties.PREFIX, ""))
//用数据源名字为key,进行属性分组
Map<String, List<String>> dsPpropertyMap = propertyNames.stream().filter(pro -> pro.contains(MatrixDataSourceProperties.PREFIX)).map(pro -> pro.replace(MatrixDataSourceProperties.PREFIX, ""))
.collect(Collectors.groupingBy(str -> str.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[1]));
Map<String, Class> fieldMap = getFieldMap();
//获取对象的所有字段的信息用于反射
Map<String, Class> fieldMap = FieldUtil.getAllFieldsList(getEntryClass()).stream().collect(Collectors.toMap(Field::getName, Field::getType, (key1, key2) -> key1));
Set<String> notSupportPropertySet = new HashSet<>();
try {
for (Map.Entry<String, List<String>> entry : propertyMap.entrySet()) {
for (Map.Entry<String, List<String>> entry : dsPpropertyMap.entrySet()) {
String dsName = entry.getKey();
T matrixDataSourceProperties = getEntryClass().newInstance();
matrixDataSourceProperties.setName(dsName);
for (String property : entry.getValue()) {
String propertyFullPath = MatrixDataSourceProperties.PREFIX + property;
Object value = decryptPropertyItemValue(appConfig.getProperty(propertyFullPath, ""));
//得到实际的属性,首字母大写
String beanProperty = property.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2];
//转换为真实类型数据
if(fieldMap.containsKey(beanProperty)){
value = MethodUtils.invokeExactStaticMethod(fieldMap.get(beanProperty), "valueOf", value);
for (String configPropertyKey : entry.getValue()) {
String configPropertyFullPath = MatrixDataSourceProperties.PREFIX + configPropertyKey;
Object value = decryptPropertyItemValue(appConfig.getProperty(configPropertyFullPath, ""));
//得到实际类的字段
String beanFieldName = configPropertyKey.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2];
if (!fieldMap.containsKey(beanFieldName)) {
notSupportPropertySet.add(beanFieldName);
continue;
}
//非String类型的需要进行真实类型数据转换
if (!fieldMap.get(beanFieldName).isAssignableFrom(String.class)) {
value = MethodUtils.invokeExactStaticMethod(fieldMap.get(beanFieldName), "valueOf", value);
}
MethodUtils.invokeExactMethod(matrixDataSourceProperties, "set" + StringUtil.capitalize(beanProperty), value);
MethodUtils.invokeExactMethod(matrixDataSourceProperties, "set" + StringUtil.capitalize(beanFieldName), value);
}
log.info("init datasource name:{}", dsName);
list.add(matrixDataSourceProperties);
......@@ -72,7 +78,10 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
log.error("e:", e);
throw new BusinessException(DataSourceError.APOLLO_CONFIG_NOT_EXIST);
}
//存在不支持的属性
if (CollectionUtil.isNotEmpty(notSupportPropertySet)) {
log.warn("Matrix not support the property:{}. See for details:http://confluence.siku.cn/pages/viewpage.action?pageId=18910893", notSupportPropertySet);
}
return list;
}
......@@ -83,21 +92,5 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
SysUtil.setProperty("accessToken", acccessToken);
}
private Map<String, Class> getFieldMap() {
List<Field> fieldList = new ArrayList<>();
Class tempClass = getEntryClass();
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass();
}
Map<String, Class> map = new HashMap<>();
//过滤String
fieldList = fieldList.stream().filter(field -> !field.getType().getSimpleName().contains("String")).collect(Collectors.toList());
for (Field field : fieldList) {
map.put(field.getName(), field.getType());
}
return map;
}
}
......@@ -9,7 +9,7 @@ import java.util.Properties;
public class MatrixDruidDataSourceProperties extends MatrixDataSourceProperties {
public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".druid";
private Integer initialSize;
private Long timeBetweenEvictionRunsMillis= DruidAbstractDataSource.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
private Long timeBetweenLogStatsMillis;
private Integer statSqlMaxSize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment