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; ...@@ -5,6 +5,7 @@ import com.ctrip.framework.apollo.ConfigService;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.secoo.mall.common.core.exception.BusinessException; import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.util.colletion.CollectionUtil; 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.common.util.string.StringUtil;
import com.secoo.mall.datasource.config.MatrixDataSourceConfig; import com.secoo.mall.datasource.config.MatrixDataSourceConfig;
import com.secoo.mall.datasource.constant.DataSourceConstant; import com.secoo.mall.datasource.constant.DataSourceConstant;
...@@ -42,28 +43,33 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -42,28 +43,33 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
if (CollectionUtil.isEmpty(propertyNames)) { if (CollectionUtil.isEmpty(propertyNames)) {
throw new BusinessException(DataSourceError.APOLLO_CONFIG_NOT_EXIST); throw new BusinessException(DataSourceError.APOLLO_CONFIG_NOT_EXIST);
} }
//用数据源名字为key,进行分组 //用数据源名字为key,进行属性分组
Map<String, List<String>> propertyMap = propertyNames.stream().filter(pro -> pro.contains(MatrixDataSourceProperties.PREFIX)).map(pro -> pro.replace(MatrixDataSourceProperties.PREFIX, "")) 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])); .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 { try {
for (Map.Entry<String, List<String>> entry : propertyMap.entrySet()) { for (Map.Entry<String, List<String>> entry : dsPpropertyMap.entrySet()) {
String dsName = entry.getKey(); String dsName = entry.getKey();
T matrixDataSourceProperties = getEntryClass().newInstance(); T matrixDataSourceProperties = getEntryClass().newInstance();
matrixDataSourceProperties.setName(dsName); matrixDataSourceProperties.setName(dsName);
for (String property : entry.getValue()) { for (String configPropertyKey : entry.getValue()) {
String propertyFullPath = MatrixDataSourceProperties.PREFIX + property; String configPropertyFullPath = MatrixDataSourceProperties.PREFIX + configPropertyKey;
Object value = decryptPropertyItemValue(appConfig.getProperty(propertyFullPath, "")); Object value = decryptPropertyItemValue(appConfig.getProperty(configPropertyFullPath, ""));
//得到实际的属性,首字母大写 //得到实际类的字段
String beanProperty = property.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2]; String beanFieldName = configPropertyKey.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2];
//转换为真实类型数据 if (!fieldMap.containsKey(beanFieldName)) {
if(fieldMap.containsKey(beanProperty)){ notSupportPropertySet.add(beanFieldName);
value = MethodUtils.invokeExactStaticMethod(fieldMap.get(beanProperty), "valueOf", value); 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); log.info("init datasource name:{}", dsName);
list.add(matrixDataSourceProperties); list.add(matrixDataSourceProperties);
...@@ -72,7 +78,10 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -72,7 +78,10 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
log.error("e:", e); log.error("e:", e);
throw new BusinessException(DataSourceError.APOLLO_CONFIG_NOT_EXIST); 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; return list;
} }
...@@ -83,21 +92,5 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -83,21 +92,5 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
SysUtil.setProperty("accessToken", acccessToken); 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; ...@@ -9,7 +9,7 @@ import java.util.Properties;
public class MatrixDruidDataSourceProperties extends MatrixDataSourceProperties { public class MatrixDruidDataSourceProperties extends MatrixDataSourceProperties {
public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".druid"; public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".druid";
private Integer initialSize;
private Long timeBetweenEvictionRunsMillis= DruidAbstractDataSource.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; private Long timeBetweenEvictionRunsMillis= DruidAbstractDataSource.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
private Long timeBetweenLogStatsMillis; private Long timeBetweenLogStatsMillis;
private Integer statSqlMaxSize; 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