From e63fe6fd6ce31c1afb070da58f5b2e99a1085212 Mon Sep 17 00:00:00 2001 From: Rui Gu Date: Sat, 27 May 2017 00:39:31 +0100 Subject: [PATCH] moved parseAttribute method to parser support class --- .../support/RedissonDefinitionParser.java | 46 ++----------------- .../RedissonNamespaceParserSupport.java | 30 ++++++++++++ 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/redisson/src/main/java/org/redisson/spring/support/RedissonDefinitionParser.java b/redisson/src/main/java/org/redisson/spring/support/RedissonDefinitionParser.java index 911574b96..6fa573d11 100644 --- a/redisson/src/main/java/org/redisson/spring/support/RedissonDefinitionParser.java +++ b/redisson/src/main/java/org/redisson/spring/support/RedissonDefinitionParser.java @@ -15,7 +15,6 @@ */ package org.redisson.spring.support; -import java.net.URI; import java.util.List; import org.redisson.Redisson; @@ -124,7 +123,7 @@ public final class RedissonDefinitionParser String id = parserContext.getReaderContext().generateBeanName(bd); helper.registerBeanDefinition(builder, id, helper.parseAliase(element), parserContext); - parseAttributes(element, parserContext, builder); + helper.parseAttributes(element, parserContext, builder); redissonDef.addDependsOn(id); parseChildElements(element, id, null, redissonDef, parserContext); parserContext.getDelegate().parseQualifierElements(element, bd); @@ -140,45 +139,6 @@ public final class RedissonDefinitionParser redissonDef.addDependsOn(id); } - private void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - NamedNodeMap attributes = element.getAttributes(); - for (int x = 0; x < attributes.getLength(); x++) { - Attr attribute = (Attr) attributes.item(x); - if (helper.isEligibleAttribute(attribute)) { - String propertyName - = attribute.getLocalName().endsWith(REF_SUFFIX) - ? attribute.getLocalName() - .substring(0, attribute.getLocalName() - .length() - REF_SUFFIX.length()) - : attribute.getLocalName(); - propertyName = Conventions - .attributeNameToPropertyName(propertyName); - Assert.state(StringUtils.hasText(propertyName), - "Illegal property name returned from" - + " 'extractPropertyName(String)': cannot be" - + " null or empty."); - if (attribute.getLocalName().endsWith(REF_SUFFIX)) { - builder.addPropertyReference(propertyName, - attribute.getValue()); - } else { - Object value = attribute.getValue(); - String localName = helper.getName(element); - if ("masterAddress".equals(propertyName) - && ConfigType.masterSlaveServers.name() - .equals(localName)) { - try { - value = URI.create((String) value); - } catch (Exception e) { - //value may be a placeholder - value = "redis://" + value; - } - } - builder.addPropertyValue(propertyName, value); - } - } - } - } - @Override public BeanDefinition parse(Element element, ParserContext parserContext) { //Sort out the Config Class @@ -186,7 +146,7 @@ public final class RedissonDefinitionParser = helper.createBeanDefinitionBuilder(element, parserContext, Config.class); String configId = helper.getId(null, configBuilder, parserContext); - parseAttributes(element, parserContext, configBuilder); + helper.parseAttributes(element, parserContext, configBuilder); helper.registerBeanDefinition(configBuilder, configId, null, parserContext); @@ -200,7 +160,7 @@ public final class RedissonDefinitionParser parserContext.getDelegate().parseQualifierElements(element, builder.getRawBeanDefinition()); String id = helper.getId(element, builder, parserContext); - parseAttributes(element, parserContext, configBuilder); + helper.parseAttributes(element, parserContext, configBuilder); //Sort out all the nested elements parseChildElements(element, configId, id, builder, parserContext); diff --git a/redisson/src/main/java/org/redisson/spring/support/RedissonNamespaceParserSupport.java b/redisson/src/main/java/org/redisson/spring/support/RedissonNamespaceParserSupport.java index d7b2804aa..be259aeb0 100644 --- a/redisson/src/main/java/org/redisson/spring/support/RedissonNamespaceParserSupport.java +++ b/redisson/src/main/java/org/redisson/spring/support/RedissonNamespaceParserSupport.java @@ -25,9 +25,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.core.Conventions; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.w3c.dom.Attr; import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; /** @@ -39,6 +41,7 @@ public class RedissonNamespaceParserSupport { public final static String REDISSON_NAMESPACE = "http://redisson.org/schema/redisson"; + static final String REF_SUFFIX = "-ref"; static final String API_CLASS_PATH_PREFIX = "org.redisson.api.R"; static final String IMPL_CLASS_PATH_PREFIX = "org.redisson.Redisson"; @@ -93,6 +96,33 @@ public class RedissonNamespaceParserSupport { return aliases; } + public void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + NamedNodeMap attributes = element.getAttributes(); + for (int x = 0; x < attributes.getLength(); x++) { + Attr attribute = (Attr) attributes.item(x); + if (isEligibleAttribute(attribute)) { + String propertyName + = attribute.getLocalName().endsWith(REF_SUFFIX) + ? attribute.getLocalName() + .substring(0, attribute.getLocalName() + .length() - REF_SUFFIX.length()) + : attribute.getLocalName(); + propertyName = Conventions + .attributeNameToPropertyName(propertyName); + Assert.state(StringUtils.hasText(propertyName), + "Illegal property name returned from" + + " 'extractPropertyName(String)': cannot be" + + " null or empty."); + if (attribute.getLocalName().endsWith(REF_SUFFIX)) { + builder.addPropertyReference(propertyName, + attribute.getValue()); + } else { + builder.addPropertyValue(propertyName, attribute.getValue()); + } + } + } + } + public BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, ParserContext parserContext, Class cls) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();