You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by qi...@apache.org on 2021/01/29 06:48:58 UTC

[shardingsphere] branch master updated: Add YamlConfigurationConverter for ConfigCenter (#9215)

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

qiulu 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 62b3246  Add YamlConfigurationConverter for ConfigCenter (#9215)
62b3246 is described below

commit 62b32468d21969f866ab2c5883b4f9154b08d7ca
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Fri Jan 29 14:48:03 2021 +0800

    Add YamlConfigurationConverter for ConfigCenter (#9215)
---
 .../governance/core/config/ConfigCenter.java       | 21 ++----
 .../yaml/config/YamlConfigurationConverter.java    | 83 ++++++++++++++++++++++
 2 files changed, 90 insertions(+), 14 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
index 054be2b..eb6fb3c 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
@@ -31,15 +31,14 @@ import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurat
 import org.apache.shardingsphere.governance.core.event.model.rule.SwitchRuleConfigurationEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaPersistEvent;
+import org.apache.shardingsphere.governance.core.yaml.config.YamlConfigurationConverter;
 import org.apache.shardingsphere.governance.core.yaml.config.YamlDataSourceConfiguration;
 import org.apache.shardingsphere.governance.core.yaml.config.YamlDataSourceConfigurationWrap;
-import org.apache.shardingsphere.governance.core.yaml.config.YamlRuleConfigurationWrap;
 import org.apache.shardingsphere.governance.core.yaml.config.schema.YamlSchema;
 import org.apache.shardingsphere.governance.core.yaml.swapper.DataSourceConfigurationYamlSwapper;
 import org.apache.shardingsphere.governance.core.yaml.swapper.SchemaYamlSwapper;
 import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
 import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
 import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
@@ -290,13 +289,8 @@ public final class ConfigCenter {
      * @return data source configurations
      */
     public Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final String schemaName) {
-        if (!hasDataSourceConfiguration(schemaName)) {
-            return new LinkedHashMap<>();
-        }
-        YamlDataSourceConfigurationWrap result = YamlEngine.unmarshalWithFilter(repository.get(node.getDataSourcePath(schemaName)), 
-                YamlDataSourceConfigurationWrap.class);
-        return result.getDataSources().entrySet().stream().collect(Collectors.toMap(Entry::getKey,
-            entry -> new DataSourceConfigurationYamlSwapper().swapToObject(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+        return hasDataSourceConfiguration(schemaName) 
+                ? YamlConfigurationConverter.convertDataSourceConfigurations(repository.get(node.getDataSourcePath(schemaName))) : new LinkedHashMap<>();
     }
     
     /**
@@ -306,9 +300,8 @@ public final class ConfigCenter {
      * @return rule configurations
      */
     public Collection<RuleConfiguration> loadRuleConfigurations(final String schemaName) {
-        return hasRuleConfiguration(schemaName) ? new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
-                YamlEngine.unmarshalWithFilter(repository.get(node.getRulePath(schemaName)), 
-                        YamlRuleConfigurationWrap.class).getRules()) : new LinkedList<>();
+        return hasRuleConfiguration(schemaName) 
+                ? YamlConfigurationConverter.convertRuleConfigurations(repository.get(node.getRulePath(schemaName))) : new LinkedList<>();
     }
     
     /**
@@ -318,7 +311,7 @@ public final class ConfigCenter {
      */
     public DefaultAuthentication loadAuthentication() {
         return hasAuthentication()
-                ? new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(repository.get(node.getAuthenticationPath()), YamlAuthenticationConfiguration.class))
+                ? YamlConfigurationConverter.convertAuthentication(repository.get(node.getAuthenticationPath()))
                 : new DefaultAuthentication();
     }
     
@@ -329,7 +322,7 @@ public final class ConfigCenter {
      */
     public Properties loadProperties() {
         return Strings.isNullOrEmpty(repository.get(node.getPropsPath())) ? new Properties() 
-                : YamlEngine.unmarshalWithFilter(repository.get(node.getPropsPath()), Properties.class);
+                : YamlConfigurationConverter.convertProperties(repository.get(node.getPropsPath()));
     }
     
     /**
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
new file mode 100644
index 0000000..d823062
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.governance.core.yaml.config;
+
+import org.apache.shardingsphere.governance.core.yaml.swapper.DataSourceConfigurationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * Configuration converter for YAML content.
+ */
+public final class YamlConfigurationConverter {
+    
+    /**
+     * Convert data source configurations from YAML content.
+     *
+     * @param yamlContent YAML content
+     * @return data source configurations
+     */
+    public static Map<String, DataSourceConfiguration> convertDataSourceConfigurations(final String yamlContent) {
+        YamlDataSourceConfigurationWrap result = YamlEngine.unmarshalWithFilter(yamlContent, YamlDataSourceConfigurationWrap.class);
+        return result.getDataSources().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> new DataSourceConfigurationYamlSwapper()
+                .swapToObject(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+    }
+    
+    /**
+     * Convert rule configurations from YAML content.
+     *
+     * @param yamlContent YAML content
+     * @return rule configurations
+     */
+    public static Collection<RuleConfiguration> convertRuleConfigurations(final String yamlContent) {
+        return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
+                YamlEngine.unmarshalWithFilter(yamlContent, YamlRuleConfigurationWrap.class).getRules());
+    }
+    
+    /**
+     * Convert authentication from YAML content.
+     *
+     * @param yamlContent YAML content
+     * @return authentication
+     */
+    public static DefaultAuthentication convertAuthentication(final String yamlContent) {
+        return new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAuthenticationConfiguration.class));
+    }
+    
+    /**
+     * Convert properties configuration from YAML content.
+     *
+     * @param yamlContent YAML content
+     * @return properties
+     */
+    public static Properties convertProperties(final String yamlContent) {
+        return YamlEngine.unmarshalWithFilter(yamlContent, Properties.class);
+    }
+}