From f3a2aa0a2d4fe80bf966b7ff809128bbd08a53b3 Mon Sep 17 00:00:00 2001 From: linghengqian Date: Mon, 29 Jan 2024 20:54:30 +0800 Subject: [PATCH] small refactoring --- .../known-implementation/_index.cn.md | 3 ++ .../known-implementation/_index.en.md | 3 ++ .../ClasspathWithEnvironmentURLProvider.java | 28 +++++++++---------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.cn.md b/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.cn.md index aae7d94acbafc..5070252bdc6fa 100644 --- a/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.cn.md +++ b/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.cn.md @@ -67,5 +67,8 @@ ds_1: password: ``` +用例: +- `jdbc:shardingsphere:classpath-environment:config.yaml` + ### 其他实现 具体可参考 https://github.com/apache/shardingsphere-plugin 。 diff --git a/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.en.md b/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.en.md index 4244252b6d907..08bdcc9b49fb7 100644 --- a/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.en.md +++ b/docs/document/content/user-manual/shardingsphere-jdbc/yaml-config/jdbc-driver/known-implementation/_index.en.md @@ -69,5 +69,8 @@ ds_1: password: ``` +Example: +- `jdbc:shardingsphere:classpath-environment:config.yaml` + ### Other implementations For details, please refer to https://github.com/apache/shardingsphere-plugin . diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java index 036086bc5373b..a4ab8c91a7a4f 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java @@ -66,22 +66,20 @@ public byte[] getContent(final String url, final String urlPrefix) { private String replaceEnvironmentVariables(final String line) { Matcher matcher = PATTERN.matcher(line); - if (matcher.find()) { - StringBuffer modifiedLine = new StringBuffer(); - String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2); - String envName = envNameAndDefaultValue[0]; - String envValue = getEnvironmentVariables(envName); - if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) { - matcher.appendReplacement(modifiedLine, ""); - return modifiedLine.substring(0, modifiedLine.length() - 1); - } - if (Strings.isNullOrEmpty(envValue)) { - envValue = envNameAndDefaultValue[1]; - } - matcher.appendReplacement(modifiedLine, envValue); - return modifiedLine.toString(); + if (!matcher.find()) { + return line; + } + String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2); + String envName = envNameAndDefaultValue[0]; + String envValue = getEnvironmentVariables(envName); + if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) { + String modifiedLineWithSpace = matcher.replaceAll(""); + return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1); + } + if (Strings.isNullOrEmpty(envValue)) { + envValue = envNameAndDefaultValue[1]; } - return line; + return matcher.replaceAll(envValue); } /**