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:40:55 UTC

[shardingsphere-ui] branch master updated: Refactor load configurations

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-ui.git


The following commit(s) were added to refs/heads/master by this push:
     new 42d453b  Refactor load configurations
     new 3dde24e  Merge pull request #77 from menghaoranss/dev-2
42d453b is described below

commit 42d453bb806504093ea4f3f773bc31c07b3df022
Author: menghaoranss <lo...@163.com>
AuthorDate: Fri Jan 29 11:13:31 2021 +0800

    Refactor load configurations
---
 .../ui/servcie/impl/GovernanceServiceImpl.java     |  20 +++-
 .../impl/ProxyAuthenticationServiceImpl.java       |   4 +-
 .../impl/ShardingPropertiesServiceImpl.java        |   4 +-
 .../ui/servcie/impl/ShardingSchemaServiceImpl.java |   6 +-
 .../ui/util/ConfigurationYamlConverter.java        | 105 ---------------------
 5 files changed, 24 insertions(+), 115 deletions(-)

diff --git a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/GovernanceServiceImpl.java b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/GovernanceServiceImpl.java
index 27fb628..3454e81 100644
--- a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/GovernanceServiceImpl.java
+++ b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/GovernanceServiceImpl.java
@@ -18,7 +18,9 @@
 package org.apache.shardingsphere.ui.servcie.impl;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
+import org.apache.shardingsphere.governance.core.yaml.config.YamlConfigurationConverter;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration;
 import org.apache.shardingsphere.replicaquery.api.config.rule.ReplicaQueryDataSourceRuleConfiguration;
@@ -27,14 +29,15 @@ import org.apache.shardingsphere.ui.common.dto.ReplicaDataSourceDTO;
 import org.apache.shardingsphere.ui.servcie.GovernanceService;
 import org.apache.shardingsphere.ui.servcie.RegistryCenterService;
 import org.apache.shardingsphere.ui.servcie.ShardingSchemaService;
-import org.apache.shardingsphere.ui.util.ConfigurationYamlConverter;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -71,6 +74,9 @@ public final class GovernanceServiceImpl implements GovernanceService {
         Collection<ReplicaDataSourceDTO> result = new ArrayList<>();
         for (String schemaName : shardingSchemaService.getAllSchemaNames()) {
             String configData = shardingSchemaService.getRuleConfiguration(schemaName);
+            if (StringUtils.isEmpty(configData)) {
+                continue;
+            }
             if (configData.contains("!SHARDING")) {
                 handleShardingRuleConfiguration(result, configData, schemaName);
             } else if (configData.contains("!REPLICA_QUERY")) {
@@ -92,7 +98,7 @@ public final class GovernanceServiceImpl implements GovernanceService {
     }
     
     private void handleShardingRuleConfiguration(final Collection<ReplicaDataSourceDTO> replicaDataSourceDTOS, final String configData, final String schemaName) {
-        Collection<RuleConfiguration> configurations = ConfigurationYamlConverter.loadRuleConfigurations(configData);
+        Collection<RuleConfiguration> configurations = YamlConfigurationConverter.convertRuleConfigurations(configData);
         Collection<ReplicaQueryRuleConfiguration> replicaQueryRuleConfigurations = configurations.stream().filter(
             config -> config instanceof ReplicaQueryRuleConfiguration).map(config -> (ReplicaQueryRuleConfiguration) config).collect(Collectors.toList());
         for (ReplicaQueryRuleConfiguration replicaQueryRuleConfiguration : replicaQueryRuleConfigurations) {
@@ -101,10 +107,18 @@ public final class GovernanceServiceImpl implements GovernanceService {
     }
     
     private void handleMasterSlaveRuleConfiguration(final Collection<ReplicaDataSourceDTO> replicaDataSourceDTOS, final String configData, final String schemaName) {
-        ReplicaQueryRuleConfiguration replicaQueryRuleConfiguration = ConfigurationYamlConverter.loadPrimaryReplicaRuleConfiguration(configData);
+        ReplicaQueryRuleConfiguration replicaQueryRuleConfiguration = loadPrimaryReplicaRuleConfiguration(configData);
         addSlaveDataSource(replicaDataSourceDTOS, replicaQueryRuleConfiguration, schemaName);
     }
     
+    private ReplicaQueryRuleConfiguration loadPrimaryReplicaRuleConfiguration(final String configData) {
+        Collection<RuleConfiguration> ruleConfigurations = YamlConfigurationConverter.convertRuleConfigurations(configData);
+        Optional<ReplicaQueryRuleConfiguration> result = ruleConfigurations.stream().filter(
+                each -> each instanceof ReplicaQueryRuleConfiguration).map(ruleConfiguration -> (ReplicaQueryRuleConfiguration) ruleConfiguration).findFirst();
+        Preconditions.checkState(result.isPresent());
+        return result.get();
+    }
+    
     private void addSlaveDataSource(final Collection<ReplicaDataSourceDTO> replicaDataSourceDTOS, final ReplicaQueryRuleConfiguration replicaQueryRuleConfiguration, final String schemaName) {
         Collection<String> disabledSchemaDataSourceNames = getDisabledSchemaDataSourceNames();
         for (ReplicaQueryDataSourceRuleConfiguration each : replicaQueryRuleConfiguration.getDataSources()) {
diff --git a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ProxyAuthenticationServiceImpl.java b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ProxyAuthenticationServiceImpl.java
index 53f1628..065e656 100644
--- a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ProxyAuthenticationServiceImpl.java
+++ b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ProxyAuthenticationServiceImpl.java
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.ui.servcie.impl;
 
+import org.apache.shardingsphere.governance.core.yaml.config.YamlConfigurationConverter;
 import org.apache.shardingsphere.ui.servcie.ConfigCenterService;
 import org.apache.shardingsphere.ui.servcie.ProxyAuthenticationService;
-import org.apache.shardingsphere.ui.util.ConfigurationYamlConverter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -46,7 +46,7 @@ public final class ProxyAuthenticationServiceImpl implements ProxyAuthentication
     
     private void checkAuthenticationConfiguration(final String data) {
         try {
-            ConfigurationYamlConverter.loadAuthentication(data);
+            YamlConfigurationConverter.convertAuthentication(data);
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
diff --git a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingPropertiesServiceImpl.java b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingPropertiesServiceImpl.java
index 976db1c..bd2aee0 100644
--- a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingPropertiesServiceImpl.java
+++ b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingPropertiesServiceImpl.java
@@ -17,10 +17,10 @@
 
 package org.apache.shardingsphere.ui.servcie.impl;
 
+import org.apache.shardingsphere.governance.core.yaml.config.YamlConfigurationConverter;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.ui.servcie.ConfigCenterService;
 import org.apache.shardingsphere.ui.servcie.ShardingPropertiesService;
-import org.apache.shardingsphere.ui.util.ConfigurationYamlConverter;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -48,7 +48,7 @@ public final class ShardingPropertiesServiceImpl implements ShardingPropertiesSe
     
     private void checkShardingProperties(final String configData) {
         try {
-            Properties props = ConfigurationYamlConverter.loadProperties(configData);
+            Properties props = YamlConfigurationConverter.convertProperties(configData);
             new ConfigurationProperties(props);
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
diff --git a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingSchemaServiceImpl.java b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingSchemaServiceImpl.java
index b65a568..7a05e86 100644
--- a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingSchemaServiceImpl.java
+++ b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ShardingSchemaServiceImpl.java
@@ -21,11 +21,11 @@ import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
+import org.apache.shardingsphere.governance.core.yaml.config.YamlConfigurationConverter;
 import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import org.apache.shardingsphere.ui.servcie.ConfigCenterService;
 import org.apache.shardingsphere.ui.servcie.ShardingSchemaService;
-import org.apache.shardingsphere.ui.util.ConfigurationYamlConverter;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -99,7 +99,7 @@ public final class ShardingSchemaServiceImpl implements ShardingSchemaService {
 
     private void checkRuleConfiguration(final String configData) {
         try {
-            ConfigurationYamlConverter.loadRuleConfigurations(configData);
+            YamlConfigurationConverter.convertRuleConfigurations(configData);
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
@@ -113,7 +113,7 @@ public final class ShardingSchemaServiceImpl implements ShardingSchemaService {
     
     private void checkDataSourceConfiguration(final String configData) {
         try {
-            Map<String, DataSourceConfiguration> dataSourceConfigs = ConfigurationYamlConverter.loadDataSourceConfigurations(configData);
+            Map<String, DataSourceConfiguration> dataSourceConfigs = YamlConfigurationConverter.convertDataSourceConfigurations(configData);
             Preconditions.checkState(!dataSourceConfigs.isEmpty(), "data source configuration is invalid.");
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
diff --git a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/util/ConfigurationYamlConverter.java b/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/util/ConfigurationYamlConverter.java
deleted file mode 100644
index ad48109..0000000
--- a/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/util/ConfigurationYamlConverter.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * 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.ui.util;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.governance.core.yaml.config.YamlDataSourceConfiguration;
-import org.apache.shardingsphere.governance.core.yaml.swapper.DataSourceConfigurationYamlSwapper;
-import org.apache.shardingsphere.infra.auth.Authentication;
-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.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
-import org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Properties;
-
-/**
- * YAML converter for configuration.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ConfigurationYamlConverter {
-    
-    /**
-     * Load data source configurations.
-     *
-     * @param data data
-     * @return data source configurations
-     */
-    @SuppressWarnings("unchecked")
-    public static Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final String data) {
-        Map<String, YamlDataSourceConfiguration> result = (Map) YamlEngine.unmarshal(data, Collections.emptyList());
-        Preconditions.checkState(null != result && !result.isEmpty(), "No available data sources to load for governance.");
-        return Maps.transformValues(result, new DataSourceConfigurationYamlSwapper()::swapToObject);
-    }
-    
-    /**
-     * Load rule configurations.
-     *
-     * @param data data
-     * @return rule configurations
-     */
-    public static Collection<RuleConfiguration> loadRuleConfigurations(final String data) {
-        return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(YamlEngine.unmarshal(data, YamlRootRuleConfigurations.class, true).getRules());
-    }
-    
-    /**
-     * Load replica query rule configuration.
-     *
-     * @param data data
-     * @return replica query rule configuration
-     */
-    public static ReplicaQueryRuleConfiguration loadPrimaryReplicaRuleConfiguration(final String data) {
-        Collection<RuleConfiguration> ruleConfigurations = loadRuleConfigurations(data);
-        Optional<ReplicaQueryRuleConfiguration> result = ruleConfigurations.stream().filter(
-            each -> each instanceof ReplicaQueryRuleConfiguration).map(ruleConfiguration -> (ReplicaQueryRuleConfiguration) ruleConfiguration).findFirst();
-        Preconditions.checkState(result.isPresent());
-        return result.get();
-    }
-    
-    /**
-     * Load authentication.
-     *
-     * @param data data
-     * @return authentication
-     */
-    public static Authentication loadAuthentication(final String data) {
-        return new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(data, YamlAuthenticationConfiguration.class));
-    }
-    
-    /**
-     * Load properties configuration.
-     *
-     * @param data data
-     * @return properties
-     */
-    public static Properties loadProperties(final String data) {
-        return YamlEngine.unmarshalProperties(data, Arrays.asList(Properties.class));
-    }
-}