You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/09/22 02:43:06 UTC

[shardingsphere] branch master updated: Add try/catch for PropertyUtil.handle and return default value (#21121)

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

duanzhengqiang 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 1d4f16b888a Add try/catch for PropertyUtil.handle and return default value (#21121)
1d4f16b888a is described below

commit 1d4f16b888a2e737fbfdd1c22fbbd46a9c6dd02e
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Thu Sep 22 10:42:58 2022 +0800

    Add try/catch for PropertyUtil.handle and return default value (#21121)
    
    * Add try/catch for PropertyUtil.handle
    
    * Add unit test
    
    * Adjustment error code
---
 .../DataSourceJndiNotFoundServerException.java     |  2 +-
 .../UnsupportedDataSourceTypeServerException.java  | 36 ----------------------
 .../spring/boot/util/PropertyUtil.java             |  9 +++++-
 .../boot/datasource/DataSourceMapSetterTest.java   | 16 +++++++++-
 4 files changed, 24 insertions(+), 39 deletions(-)

diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/DataSourceJndiNotFoundServerException.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/DataSourceJndiNotFoundServerException.java
index 4008cd35c00..de6c2703434 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/DataSourceJndiNotFoundServerException.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/DataSourceJndiNotFoundServerException.java
@@ -28,7 +28,7 @@ public final class DataSourceJndiNotFoundServerException extends ShardingSphereS
     
     private static final String ERROR_CATEGORY = "SPRING";
     
-    private static final int ERROR_CODE = 2;
+    private static final int ERROR_CODE = 1;
     
     public DataSourceJndiNotFoundServerException(final Exception cause) {
         super(ERROR_CATEGORY, ERROR_CODE, "Can not find JNDI data source", cause);
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/UnsupportedDataSourceTypeServerException.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/UnsupportedDataSourceTypeServerException.java
deleted file mode 100644
index 41e6f805b67..00000000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/exception/UnsupportedDataSourceTypeServerException.java
+++ /dev/null
@@ -1,36 +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.spring.boot.exception;
-
-import org.apache.shardingsphere.infra.util.exception.external.server.ShardingSphereServerException;
-
-/**
- * Unsupported data source type exception.
- */
-public final class UnsupportedDataSourceTypeServerException extends ShardingSphereServerException {
-    
-    private static final long serialVersionUID = 8264305279431450585L;
-    
-    private static final String ERROR_CATEGORY = "SPRING";
-    
-    private static final int ERROR_CODE = 1;
-    
-    public UnsupportedDataSourceTypeServerException(final Exception cause) {
-        super(ERROR_CATEGORY, ERROR_CODE, "Can not find data source type", cause);
-    }
-}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/util/PropertyUtil.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/util/PropertyUtil.java
index 50d20b365d6..d8a0fba6eff 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/util/PropertyUtil.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/util/PropertyUtil.java
@@ -27,6 +27,7 @@ import org.springframework.core.env.PropertyResolver;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -75,7 +76,13 @@ public final class PropertyUtil {
      */
     @SuppressWarnings("unchecked")
     public static <T> T handle(final Environment environment, final String prefix, final Class<T> targetClass) {
-        return 1 == springBootVersion ? (T) v1(environment, prefix, true) : (T) v2(environment, prefix, targetClass);
+        try {
+            return 1 == springBootVersion ? (T) v1(environment, prefix, true) : (T) v2(environment, prefix, targetClass);
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            return (T) Collections.emptyMap();
+        }
     }
     
     @SuppressWarnings("unchecked")
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/test/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetterTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/test/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetterTest.java
index e1b1172d4b7..9ac6e034b23 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/test/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetterTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/test/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetterTest.java
@@ -32,7 +32,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 public final class DataSourceMapSetterTest {
     
     @Test
-    public void assetGetDataSourceMap() throws SQLException {
+    public void assertGetDataSourceMap() throws SQLException {
         MockEnvironment mockEnvironment = new MockEnvironment();
         mockEnvironment.setProperty("spring.shardingsphere.datasource.names", "ds0,ds1");
         mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.url", "jdbc:mock://127.0.0.1/ds_0");
@@ -52,4 +52,18 @@ public final class DataSourceMapSetterTest {
         assertThat(dataSourceMap.get("ds0").getConnection().getMetaData().getURL(), is("jdbc:mock://127.0.0.1/ds_0"));
         assertThat(dataSourceMap.get("ds1").getConnection().getMetaData().getURL(), is("jdbc:mock://127.0.0.1/ds_1"));
     }
+    
+    @Test(expected = IllegalStateException.class)
+    public void assertInvalidDataSourceNames() {
+        MockEnvironment mockEnvironment = new MockEnvironment();
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.names", "ds0,ds1");
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.url", "jdbc:mock://127.0.0.1/ds_0");
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.type", MockedDataSource.class.getName());
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.username", "sa");
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.password", "");
+        mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.maxPoolSize", "50");
+        StandardEnvironment standardEnvironment = new StandardEnvironment();
+        standardEnvironment.merge(mockEnvironment);
+        DataSourceMapSetter.getDataSourceMap(standardEnvironment);
+    }
 }