You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/01/11 10:02:32 UTC

[shardingsphere] branch master updated: Namespace load SPI failed when algorithm label is after rule label. (#14570)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new d843443  Namespace load SPI failed when algorithm label is after rule label. (#14570)
d843443 is described below

commit d8434432590c61e84fc4e67e3062e02d8254ce2c
Author: liguoping <xd...@163.com>
AuthorDate: Tue Jan 11 18:01:23 2022 +0800

    Namespace load SPI failed when algorithm label is after rule label. (#14570)
    
    * A way to resolve Namespace load SPI error
    
    * update other scenes
    
    * update
    
    * tag name error fixed
---
 .../database-discovery-application-context.xml     | 11 ++---
 ...hardingSphereAlgorithmBeanDefinitionParser.java | 52 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml
index 6251eb8..aed7d87 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml
@@ -24,11 +24,6 @@
                            http://shardingsphere.apache.org/schema/shardingsphere/database-discovery
                            http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery.xsd
                            ">
-    <database-discovery:discovery-type id="mgr" type="MGR" >
-        <props>
-            <prop key="group-name">92504d5b-6dec-11e8-91ea-246e9612aaf1</prop>
-        </props>
-    </database-discovery:discovery-type>
     
     <database-discovery:rule id="mgrDatabaseDiscoveryRule">
         <database-discovery:data-source-rule id="pr_ds" data-source-names="ds_0,ds_1,ds_2" discovery-heartbeat-name="mgr-heartbeat" discovery-type-name="mgr"/>
@@ -38,4 +33,10 @@
             </props>
         </database-discovery:discovery-heartbeat>
     </database-discovery:rule>
+
+    <database-discovery:discovery-type id="mgr" type="MGR" >
+        <props>
+            <prop key="group-name">92504d5b-6dec-11e8-91ea-246e9612aaf1</prop>
+        </props>
+    </database-discovery:discovery-type>
 </beans>
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-namespace-infra/src/main/java/org/apache/shardingsphere/spring/namespace/parser/ShardingSphereAlgorithmBeanDefinitionParser.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-namespace-infra/src/main/java/org/apache/shardingsphere/spring/namespace/parser/ShardingSphereAlgorithmBeanDefinitionParser.java
index 50ec00c..87cd7a3 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-namespace-infra/src/main/java/org/apache/shardingsphere/spring/namespace/parser/ShardingSphereAlgorithmBeanDefinitionParser.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-namespace-infra/src/main/java/org/apache/shardingsphere/spring/namespace/parser/ShardingSphereAlgorithmBeanDefinitionParser.java
@@ -18,10 +18,21 @@
 package org.apache.shardingsphere.spring.namespace.parser;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dbdiscovery.algorithm.config.AlgorithmProvidedDatabaseDiscoveryRuleConfiguration;
+import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
+import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.algorithm.config.AlgorithmProvidedReadwriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
+import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
 import org.apache.shardingsphere.spring.namespace.factorybean.ShardingSphereAlgorithmFactoryBean;
 import org.apache.shardingsphere.spring.namespace.tag.ShardingSphereAlgorithmBeanDefinitionTag;
+import org.springframework.beans.MutablePropertyValues;
+import org.springframework.beans.PropertyValue;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.RuntimeBeanReference;
 import org.springframework.beans.factory.support.AbstractBeanDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.ManagedMap;
 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.util.xml.DomUtils;
@@ -42,9 +53,50 @@ public final class ShardingSphereAlgorithmBeanDefinitionParser extends AbstractB
         BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(beanClass);
         factory.addConstructorArgValue(element.getAttribute(ShardingSphereAlgorithmBeanDefinitionTag.TYPE_ATTRIBUTE));
         factory.addConstructorArgValue(parsePropsElement(element, parserContext));
+        filledPropToRuleConfig(parserContext, element);
         return factory.getBeanDefinition();
     }
     
+    private void filledPropToRuleConfig(final ParserContext parserContext, final Element element) {
+        switch (element.getTagName()) {
+            case "database-discovery:discovery-type":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedDatabaseDiscoveryRuleConfiguration.class, "discoveryTypes");
+                break;
+            case "encrypt:encrypt-algorithm":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedEncryptRuleConfiguration.class, "encryptors");
+                break;
+            case "readwrite-splitting:load-balance-algorithm":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedReadwriteSplittingRuleConfiguration.class, "loadBalanceAlgorithms");
+                break;
+            case "shadow:shadow-algorithm":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedShadowRuleConfiguration.class, "shadowAlgorithms");
+                break;
+            case "sharding:sharding-algorithm":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedShardingRuleConfiguration.class, "shardingAlgorithms");
+                break;
+            case "sharding:key-generate-algorithm":
+                setPropertyValue(parserContext, element.getAttribute(ID_ATTRIBUTE), AlgorithmProvidedShardingRuleConfiguration.class, "keyGenerators");
+                break;
+            default:
+                break;
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void setPropertyValue(final ParserContext parserContext, final String elementId, final Class<? extends SchemaRuleConfiguration> ruleConfigClass, final String propertyName) {
+        String[] beanDefinitionNames = parserContext.getRegistry().getBeanDefinitionNames();
+        for (String each : beanDefinitionNames) {
+            BeanDefinition beanDefinition = parserContext.getRegistry().getBeanDefinition(each);
+            if (null != beanDefinition.getBeanClassName() && beanDefinition.getBeanClassName().equals(ruleConfigClass.getName())) {
+                MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
+                PropertyValue propertyValue = propertyValues.getPropertyValue(propertyName);
+                if (null != propertyValue && propertyValue.getValue() instanceof ManagedMap) {
+                    ((ManagedMap<String, RuntimeBeanReference>) propertyValue.getValue()).put(elementId, new RuntimeBeanReference(elementId));
+                }
+            }
+        }
+    }
+    
     private Properties parsePropsElement(final Element element, final ParserContext parserContext) {
         Element propsElement = DomUtils.getChildElementByTagName(element, ShardingSphereAlgorithmBeanDefinitionTag.PROPS_TAG);
         return null == propsElement ? new Properties() : parserContext.getDelegate().parsePropsElement(propsElement);