From c2806b6a1c4a75db7d641cf3aa6f207c74bf29ef Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Fri, 11 Jan 2019 15:43:18 +0800 Subject: [PATCH] refactor static final field name --- .../custom/SentinelDataSourceHandler.java | 132 +++++++++--------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelDataSourceHandler.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelDataSourceHandler.java index f9776d27..d73996e2 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelDataSourceHandler.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelDataSourceHandler.java @@ -54,9 +54,9 @@ public class SentinelDataSourceHandler { private List dataSourceBeanNameList = Collections .synchronizedList(new ArrayList<>()); - private final String DATATYPE_FIELD = "dataType"; - private final String CUSTOM_DATATYPE = "custom"; - private final String CONVERTERCLASS_FIELD = "converterClass"; + private final String DATA_TYPE_FIELD = "dataType"; + private final String CUSTOM_DATA_TYPE = "custom"; + private final String CONVERTER_CLASS_FIELD = "converterClass"; @Autowired private SentinelProperties sentinelProperties; @@ -132,8 +132,8 @@ public class SentinelDataSourceHandler { e); } }, HashMap::putAll); - propertyMap.put(CONVERTERCLASS_FIELD, dataSourceProperties.getConverterClass()); - propertyMap.put(DATATYPE_FIELD, dataSourceProperties.getDataType()); + propertyMap.put(CONVERTER_CLASS_FIELD, dataSourceProperties.getConverterClass()); + propertyMap.put(DATA_TYPE_FIELD, dataSourceProperties.getDataType()); BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition(dataSourceProperties.getFactoryBeanName()); @@ -141,76 +141,72 @@ public class SentinelDataSourceHandler { propertyMap.forEach((propertyName, propertyValue) -> { Field field = ReflectionUtils.findField(dataSourceProperties.getClass(), propertyName); - if (field != null) { - if (DATATYPE_FIELD.equals(propertyName)) { - String dataType = StringUtils - .trimAllWhitespace(propertyValue.toString()); - if (CUSTOM_DATATYPE.equals(dataType)) { - try { - if (StringUtils - .isEmpty(dataSourceProperties.getConverterClass())) { - throw new RuntimeException( - "[Sentinel Starter] DataSource " + dataSourceName - + "dataType is custom, please set converter-class " - + "property"); - } - // construct custom Converter with 'converterClass' - // configuration and register - String customConvertBeanName = "sentinel-" - + dataSourceProperties.getConverterClass(); - if (!beanFactory.containsBean(customConvertBeanName)) { - beanFactory.registerBeanDefinition(customConvertBeanName, - BeanDefinitionBuilder - .genericBeanDefinition( - Class.forName(dataSourceProperties - .getConverterClass())) - .getBeanDefinition()); - } - builder.addPropertyReference("converter", - customConvertBeanName); - } - catch (ClassNotFoundException e) { - logger.error("[Sentinel Starter] DataSource " + dataSourceName - + " handle " - + dataSourceProperties.getClass().getSimpleName() - + " error, class name: " - + dataSourceProperties.getConverterClass()); - throw new RuntimeException( - "[Sentinel Starter] DataSource " + dataSourceName - + " handle " - + dataSourceProperties.getClass() - .getSimpleName() - + " error, class name: " - + dataSourceProperties.getConverterClass(), - e); - } - } - else { - if (!dataTypeList.contains(StringUtils - .trimAllWhitespace(propertyValue.toString()))) { + if (null == field) { + return; + } + if (DATA_TYPE_FIELD.equals(propertyName)) { + String dataType = StringUtils.trimAllWhitespace(propertyValue.toString()); + if (CUSTOM_DATA_TYPE.equals(dataType)) { + try { + if (StringUtils + .isEmpty(dataSourceProperties.getConverterClass())) { throw new RuntimeException("[Sentinel Starter] DataSource " - + dataSourceName + " dataType: " + propertyValue - + " is not support now. please using these types: " - + dataTypeList.toString()); + + dataSourceName + + "dataType is custom, please set converter-class " + + "property"); } - // converter type now support xml or json. - // The bean name of these converters wrapped by - // 'sentinel-{converterType}-{ruleType}-converter' - builder.addPropertyReference("converter", - "sentinel-" + propertyValue.toString() + "-" - + dataSourceProperties.getRuleType().getName() - + "-converter"); + // construct custom Converter with 'converterClass' + // configuration and register + String customConvertBeanName = "sentinel-" + + dataSourceProperties.getConverterClass(); + if (!beanFactory.containsBean(customConvertBeanName)) { + beanFactory.registerBeanDefinition(customConvertBeanName, + BeanDefinitionBuilder + .genericBeanDefinition( + Class.forName(dataSourceProperties + .getConverterClass())) + .getBeanDefinition()); + } + builder.addPropertyReference("converter", customConvertBeanName); + } + catch (ClassNotFoundException e) { + logger.error("[Sentinel Starter] DataSource " + dataSourceName + + " handle " + + dataSourceProperties.getClass().getSimpleName() + + " error, class name: " + + dataSourceProperties.getConverterClass()); + throw new RuntimeException("[Sentinel Starter] DataSource " + + dataSourceName + " handle " + + dataSourceProperties.getClass().getSimpleName() + + " error, class name: " + + dataSourceProperties.getConverterClass(), e); } - } - else if (CONVERTERCLASS_FIELD.equals(propertyName)) { - return; } else { - // wired properties - Optional.ofNullable(propertyValue) - .ifPresent(v -> builder.addPropertyValue(propertyName, v)); + if (!dataTypeList.contains( + StringUtils.trimAllWhitespace(propertyValue.toString()))) { + throw new RuntimeException("[Sentinel Starter] DataSource " + + dataSourceName + " dataType: " + propertyValue + + " is not support now. please using these types: " + + dataTypeList.toString()); + } + // converter type now support xml or json. + // The bean name of these converters wrapped by + // 'sentinel-{converterType}-{ruleType}-converter' + builder.addPropertyReference("converter", + "sentinel-" + propertyValue.toString() + "-" + + dataSourceProperties.getRuleType().getName() + + "-converter"); } } + else if (CONVERTER_CLASS_FIELD.equals(propertyName)) { + return; + } + else { + // wired properties + Optional.ofNullable(propertyValue) + .ifPresent(v -> builder.addPropertyValue(propertyName, v)); + } }); beanFactory.registerBeanDefinition(dataSourceName, builder.getBeanDefinition());