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

panjuan 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 11a901df897 Add ShardingSphereURLArgument (#30122)
11a901df897 is described below

commit 11a901df89757e8ca16c7a0eb513726229ade311
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 14 20:04:54 2024 +0800

    Add ShardingSphereURLArgument (#30122)
---
 .../core/driver/url/ShardingSphereURLManager.java  |  1 +
 .../jdbc/core/driver/url/{ => arg}/ArgsUtils.java  | 34 ++-----------
 .../driver/url/arg/ShardingSphereURLArgument.java  | 58 ++++++++++++++++++++++
 .../url/reader/ConfigurationContentReader.java     | 47 +++++++++---------
 .../driver/url/type/AbsolutePathURLProvider.java   |  2 +-
 .../core/driver/url/type/ClasspathURLProvider.java |  2 +-
 .../url/reader/ConfigurationContentReaderTest.java |  4 +-
 7 files changed, 89 insertions(+), 59 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
index 667dc14d9a2..1cb154aa181 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.driver.jdbc.core.driver.url;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.ArgsUtils;
 import 
org.apache.shardingsphere.driver.jdbc.exception.syntax.URLProviderNotFoundException;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ArgsUtils.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ArgsUtils.java
similarity index 84%
rename from 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ArgsUtils.java
rename to 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ArgsUtils.java
index 33bb50066fd..58e790cddf2 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ArgsUtils.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ArgsUtils.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.driver.jdbc.core.driver.url;
+package org.apache.shardingsphere.driver.jdbc.core.driver.url.arg;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
@@ -36,38 +36,10 @@ import java.util.regex.Pattern;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ArgsUtils {
     
-    private static final String KEY_VALUE_SEPARATOR = "::";
-    
-    private static final Pattern PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
-    
-    /**
-     * Get key value separator.
-     * 
-     * @return key value separator
-     */
-    public static String getKeyValueSeparator() {
-        return KEY_VALUE_SEPARATOR;
-    }
-    
     /**
-     * Get pattern.
-     *
-     * @return got pattern
+     * Placeholder pattern.
      */
-    public static Pattern getPattern() {
-        return PATTERN;
-    }
-    
-    /**
-     * Get arg name and default value.
-     *
-     * @param matcher matcher
-     * @return argument name and default value
-     */
-    public static String[] getArgNameAndDefaultValue(final Matcher matcher) {
-        String groupString = matcher.group(1);
-        return groupString.split(ArgsUtils.getKeyValueSeparator(), 2);
-    }
+    public static final Pattern PLACEHOLDER_PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
     
     /**
      * Replace argument.
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ShardingSphereURLArgument.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ShardingSphereURLArgument.java
new file mode 100644
index 00000000000..ef11f0c4245
--- /dev/null
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/ShardingSphereURLArgument.java
@@ -0,0 +1,58 @@
+/*
+ * 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.driver.jdbc.core.driver.url.arg;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * ShardingSphere URL argument.
+ */
+@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
+@Getter
+public final class ShardingSphereURLArgument {
+    
+    public static final Pattern PLACEHOLDER_PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
+    
+    private static final String KV_SEPARATOR = "::";
+    
+    private final String name;
+    
+    private final String defaultValue;
+    
+    /**
+     * Parse ShardingSphere URL argument.
+     *
+     * @param line line
+     * @return parsed argument
+     */
+    public static Optional<ShardingSphereURLArgument> parse(final String line) 
{
+        Matcher matcher = ArgsUtils.PLACEHOLDER_PATTERN.matcher(line);
+        if (!matcher.find()) {
+            return Optional.empty();
+        }
+        String[] parsedArg = matcher.group(1).split(KV_SEPARATOR, 2);
+        return Optional.of(new ShardingSphereURLArgument(parsedArg[0], 
parsedArg[1]));
+    }
+    
+}
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java
index ea40215360f..0a33ba76d51 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java
@@ -19,14 +19,15 @@ package 
org.apache.shardingsphere.driver.jdbc.core.driver.url.reader;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.driver.jdbc.core.driver.url.ArgsUtils;
+import org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.ArgsUtils;
+import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.ShardingSphereURLArgument;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
-import java.util.regex.Matcher;
+import java.util.Optional;
 
 /**
  * Configuration content reader.
@@ -47,36 +48,34 @@ public final class ConfigurationContentReader {
             StringBuilder builder = new StringBuilder();
             String line;
             while (null != (line = reader.readLine())) {
-                if (!line.startsWith("#")) {
-                    if (ConfigurationContentPlaceholderType.ENVIRONMENT == 
type) {
-                        line = replaceEnvironmentVariables(line);
-                    } else if 
(ConfigurationContentPlaceholderType.SYSTEM_PROPS == type) {
-                        line = replaceSystemProperties(line);
-                    }
-                    builder.append(line).append(System.lineSeparator());
+                if (line.startsWith("#")) {
+                    continue;
                 }
+                Optional<ShardingSphereURLArgument> arg = 
ConfigurationContentPlaceholderType.NONE == type ? Optional.empty() : 
ShardingSphereURLArgument.parse(line);
+                if (arg.isPresent()) {
+                    line = ArgsUtils.replaceArg(getArgumentValue(type, 
arg.get()), arg.get().getDefaultValue(), 
ArgsUtils.PLACEHOLDER_PATTERN.matcher(line));
+                }
+                builder.append(line).append(System.lineSeparator());
             }
             return builder.toString().getBytes(StandardCharsets.UTF_8);
         }
     }
     
-    private static String replaceSystemProperties(final String line) {
-        Matcher matcher = ArgsUtils.getPattern().matcher(line);
-        if (!matcher.find()) {
-            return line;
+    private static String getArgumentValue(final 
ConfigurationContentPlaceholderType type, final ShardingSphereURLArgument arg) {
+        if (ConfigurationContentPlaceholderType.ENVIRONMENT == type) {
+            return getEnvironmentVariable(arg.getName());
+        }
+        if (ConfigurationContentPlaceholderType.SYSTEM_PROPS == type) {
+            return getSystemProperty(arg.getName());
         }
-        String[] systemPropNameAndDefaultValue = 
ArgsUtils.getArgNameAndDefaultValue(matcher);
-        String systemPropValue = 
System.getProperty(systemPropNameAndDefaultValue[0]);
-        return ArgsUtils.replaceArg(systemPropValue, 
systemPropNameAndDefaultValue[1], matcher);
+        return null;
     }
     
-    private static String replaceEnvironmentVariables(final String line) {
-        Matcher matcher = ArgsUtils.getPattern().matcher(line);
-        if (!matcher.find()) {
-            return line;
-        }
-        String[] envNameAndDefaultValue = 
ArgsUtils.getArgNameAndDefaultValue(matcher);
-        String envValue = System.getenv(envNameAndDefaultValue[0]);
-        return ArgsUtils.replaceArg(envValue, envNameAndDefaultValue[1], 
matcher);
+    private static String getEnvironmentVariable(final String argName) {
+        return System.getenv(argName);
+    }
+    
+    private static String getSystemProperty(final String argName) {
+        return System.getProperty(argName);
     }
 }
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLProvider.java
index eda5b7b044d..1e43b3e1cfc 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLProvider.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.driver.jdbc.core.driver.url.type;
 
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.driver.jdbc.core.driver.url.ArgsUtils;
+import org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.ArgsUtils;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLProvider;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.reader.ConfigurationContentReader;
 
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClasspathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClasspathURLProvider.java
index 717283583cf..1941d5ff90f 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClasspathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClasspathURLProvider.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.driver.jdbc.core.driver.url.type;
 
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.driver.jdbc.core.driver.url.ArgsUtils;
+import org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.ArgsUtils;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLProvider;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.reader.ConfigurationContentReader;
 
diff --git 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReaderTest.java
 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReaderTest.java
index a0ff69de43b..6678cb201b4 100644
--- 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReaderTest.java
+++ 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReaderTest.java
@@ -50,14 +50,14 @@ class ConfigurationContentReaderTest {
     void assertReadWithNonePlaceholder() throws IOException {
         byte[] actual = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
ConfigurationContentPlaceholderType.NONE);
         byte[] expected = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
ConfigurationContentPlaceholderType.NONE);
-        assertThat(actual, is(expected));
+        assertThat(new String(actual), is(new String(expected)));
     }
     
     @Test
     void assertReadWithSystemPropertiesPlaceholder() throws IOException {
         byte[] actual = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
ConfigurationContentPlaceholderType.SYSTEM_PROPS);
         byte[] expected = readContent("config/driver/foo-driver-fixture.yaml", 
ConfigurationContentPlaceholderType.SYSTEM_PROPS);
-        assertThat(actual, is(expected));
+        assertThat(new String(actual), is(new String(expected)));
     }
     
     private byte[] readContent(final String name, final 
ConfigurationContentPlaceholderType placeholderType) throws IOException {

Reply via email to