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

chengzhang 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 2b8e4627563 Add MultiSourceProperties to support Properties with 
defaults. (#31154)
2b8e4627563 is described below

commit 2b8e4627563f76681f0c07b758da890d3e98d5d6
Author: Cong Hu <[email protected]>
AuthorDate: Tue May 7 13:49:31 2024 +0800

    Add MultiSourceProperties to support Properties with defaults. (#31154)
---
 .../api/config/EncryptRuleConfiguration.java       |  1 +
 .../algorithm/standard/AESEncryptAlgorithm.java    |  6 +--
 .../infra/util/props/MultiSourceProperties.java    | 53 ++++++++++++++++++++
 .../util/props/MultiSourcePropertiesTest.java      | 57 ++++++++++++++++++++++
 4 files changed, 114 insertions(+), 3 deletions(-)

diff --git 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
index 41ade93cd10..7ba7c9a2ac0 100644
--- 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
+++ 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
@@ -50,6 +50,7 @@ public final class EncryptRuleConfiguration implements 
DatabaseRuleConfiguration
     private Map<String, AlgorithmConfiguration> 
rebuildEncryptorsWithDefaultProperties(final Map<String, 
AlgorithmConfiguration> encryptors) {
         Map<String, AlgorithmConfiguration> result = new HashMap<>();
         for (Entry<String, AlgorithmConfiguration> entry : 
encryptors.entrySet()) {
+            // todo Replace with MultiSourceProperties, MultiSourceProperties 
need support marshal.
             Properties props = new Properties();
             props.putAll(entry.getValue().getProps());
             Properties defaultProps = 
TypedSPILoader.findUninitedService(EncryptAlgorithm.class, 
entry.getValue().getType()).map(EncryptAlgorithm::getMetaData)
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
index 156b04844f2..fe55f067bf8 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
@@ -27,6 +27,7 @@ import 
org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
 import 
org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.util.props.MultiSourceProperties;
 
 import javax.crypto.Cipher;
 import javax.crypto.spec.SecretKeySpec;
@@ -59,9 +60,8 @@ public final class AESEncryptAlgorithm implements 
EncryptAlgorithm {
     
     @Override
     public void init(final Properties props) {
-        Properties properties = new Properties(metaData.getDefaultProps());
-        properties.putAll(props);
-        secretKey = getSecretKey(properties);
+        Properties multiSourceProperties = new MultiSourceProperties(props, 
metaData.getDefaultProps());
+        secretKey = getSecretKey(multiSourceProperties);
     }
     
     private byte[] getSecretKey(final Properties props) {
diff --git 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/MultiSourceProperties.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/MultiSourceProperties.java
new file mode 100644
index 00000000000..95be05f5171
--- /dev/null
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/props/MultiSourceProperties.java
@@ -0,0 +1,53 @@
+/*
+ * 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.infra.util.props;
+
+import java.util.Properties;
+
+/**
+ * Multi source properties.
+ */
+public final class MultiSourceProperties extends Properties {
+    
+    private final Properties[] defaults;
+    
+    public MultiSourceProperties(final Properties... defaults) {
+        this.defaults = defaults;
+    }
+    
+    @Override
+    public String getProperty(final String key) {
+        String value = super.getProperty(key);
+        if (null != value) {
+            return value;
+        }
+        for (Properties each : defaults) {
+            value = each.getProperty(key);
+            if (null != value) {
+                return value;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public String getProperty(final String key, final String defaultValue) {
+        String value = this.getProperty(key);
+        return (null == value) ? defaultValue : value;
+    }
+}
diff --git 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/props/MultiSourcePropertiesTest.java
 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/props/MultiSourcePropertiesTest.java
new file mode 100644
index 00000000000..27d0d9fdf5a
--- /dev/null
+++ 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/props/MultiSourcePropertiesTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.infra.util.props;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class MultiSourcePropertiesTest {
+    
+    private MultiSourceProperties multiSourceProperties;
+    
+    @BeforeEach
+    void setUp() {
+        Properties propsA = new Properties();
+        propsA.setProperty("keyA", "valueA");
+        Properties defaultProps = new Properties();
+        defaultProps.setProperty("keyA", "valueB");
+        defaultProps.setProperty("keyB", "valueB");
+        Properties propsB = new Properties(defaultProps);
+        multiSourceProperties = new MultiSourceProperties(propsA, propsB);
+    }
+    
+    @Test
+    void assertGetProperty() {
+        assertThat(multiSourceProperties.getProperty("keyA"), is("valueA"));
+        assertThat(multiSourceProperties.getProperty("keyB"), is("valueB"));
+        assertNull(multiSourceProperties.getProperty("keyC"));
+    }
+    
+    @Test
+    void assertGetPropertyWithDefault() {
+        assertThat(multiSourceProperties.getProperty("keyA", "defaultValue"), 
is("valueA"));
+        assertThat(multiSourceProperties.getProperty("keyB", "defaultValue"), 
is("valueB"));
+        assertThat(multiSourceProperties.getProperty("keyC", "defaultValue"), 
is("defaultValue"));
+    }
+}

Reply via email to