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 8646c49de58 Add AbsolutePathURLProvider implementation that reads 
environment variables (#29909)
8646c49de58 is described below

commit 8646c49de58b01f49efbd334bf99e5b2adc2c8b1
Author: Ling Hengqian <[email protected]>
AuthorDate: Tue Jan 30 12:15:20 2024 +0800

    Add AbsolutePathURLProvider implementation that reads environment variables 
(#29909)
---
 .../jdbc-driver/known-implementation/_index.cn.md  |  9 +++++
 .../jdbc-driver/known-implementation/_index.en.md  | 10 +++++
 .../AbsolutePathURLProvider.java                   | 10 ++---
 .../AbsolutePathWithEnvironmentURLProvider.java}   | 19 +++++-----
 .../AbstractAbsolutePathURLProvider.java}          | 23 +++--------
 .../classpath/AbstractClasspathURLProvider.java    |  4 +-
 .../driver/spi/classpath/ClasspathURLProvider.java |  6 +--
 .../ClasspathWithEnvironmentURLProvider.java       |  9 ++---
 ...iver.jdbc.core.driver.ShardingSphereURLProvider |  3 +-
 ...AbsolutePathWithEnvironmentURLProviderTest.java | 44 ++++++++++++++++++++++
 10 files changed, 93 insertions(+), 44 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 5070252bdc6..5e708f028e3 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
@@ -70,5 +70,14 @@ ds_1:
 用例:
 - `jdbc:shardingsphere:classpath-environment:config.yaml`
 
+### 从绝对路径中加载包含环境变量的配置文件
+
+加载绝对路径中包含环境变量的 config.yaml 配置文件的 JDBC URL,通过 
`jdbc:shardingsphere:absolutepath-environment:` 前缀识别。
+配置文件为 `xxx.yaml`,配置文件格式与`jdbc:shardingsphere:classpath-environment:`一致。
+与 `jdbc:shardingsphere:classpath-environment:` 的区别仅在于 YAML 文件的加载位置。
+
+用例:
+- `jdbc:shardingsphere:absolutepath-environment:/path/to/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 08bdcc9b49f..f896c247342 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
@@ -72,5 +72,15 @@ ds_1:
 Example:
 - `jdbc:shardingsphere:classpath-environment:config.yaml`
 
+### Load configuration file containing environment variables from absolute path
+
+JDBC URL to load the config.yaml configuration file that contains environment 
variables in absolute path, 
+identified by the `jdbc:shardingsphere:absolutepath-environment:` prefix.
+The configuration file is `xxx.yaml`, and the configuration file format is 
consistent with `jdbc:shardingsphere:classpath-environment:`.
+The difference from `jdbc:shardingsphere:classpath-environment:` is only where 
the YAML file is loaded.
+
+Example:
+- `jdbc:shardingsphere:absolutepath-environment:/path/to/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/AbsolutePathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathURLProvider.java
similarity index 78%
rename from 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java
rename to 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathURLProvider.java
index 3d95c8fda88..5915ac737a3 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathURLProvider.java
@@ -15,12 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.driver.jdbc.core.driver.spi;
+package org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.SneakyThrows;
-import 
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -33,7 +31,7 @@ import java.nio.file.Files;
 /**
  * Absolute path URL provider.
  */
-public final class AbsolutePathURLProvider implements 
ShardingSphereURLProvider {
+public final class AbsolutePathURLProvider extends 
AbstractAbsolutePathURLProvider {
     
     private static final String PATH_TYPE = "absolutepath:";
     
@@ -45,9 +43,7 @@ public final class AbsolutePathURLProvider implements 
ShardingSphereURLProvider
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final String url, final String urlPrefix) {
-        String configuredFile = url.substring(urlPrefix.length(), 
url.contains("?") ? url.indexOf('?') : url.length());
-        String file = configuredFile.substring(PATH_TYPE.length());
-        Preconditions.checkArgument(!file.isEmpty(), "Configuration file is 
required in ShardingSphere URL.");
+        String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
         try (
                 InputStream stream = Files.newInputStream(new 
File(file).toPath());
                 BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream, StandardCharsets.UTF_8))) {
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/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
similarity index 84%
copy from 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java
copy to 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
index a4ab8c91a7a..fdaa76d7a43 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/absolutepath/AbsolutePathWithEnvironmentURLProvider.java
@@ -15,42 +15,43 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath;
+package org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath;
 
 import com.google.common.base.Strings;
 import lombok.SneakyThrows;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
- * Classpath with environment variables URL provider.
+ * Absolute path with environment variables URL provider.
  */
-public final class ClasspathWithEnvironmentURLProvider extends 
AbstractClasspathURLProvider {
+public class AbsolutePathWithEnvironmentURLProvider extends 
AbstractAbsolutePathURLProvider {
     
-    private static final String TYPE_PREFIX = "classpath-environment:";
+    private static final String PATH_TYPE = "absolutepath-environment:";
     
     private static final String KEY_VALUE_SEPARATOR = "::";
     
-    @SuppressWarnings("RegExpRedundantEscape")
-    private static final Pattern PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)\\}$");
+    private static final Pattern PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
     
     @Override
     public boolean accept(final String url) {
-        return !Strings.isNullOrEmpty(url) && url.contains(TYPE_PREFIX);
+        return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
     }
     
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final String url, final String urlPrefix) {
-        String file = getConfigurationFile(url, urlPrefix, TYPE_PREFIX);
+        String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
         try (
-                InputStream stream = getResourceAsStream(file);
+                InputStream stream = Files.newInputStream(new 
File(file).toPath());
                 BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream, StandardCharsets.UTF_8))) {
             StringBuilder builder = new StringBuilder();
             String line;
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbstractAbsolutePathURLProvider.java
similarity index 56%
copy from 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
copy to 
jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbstractAbsolutePathURLProvider.java
index ce9cc175260..bfc63d7176a 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbstractAbsolutePathURLProvider.java
@@ -15,31 +15,20 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath;
+package org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath;
 
 import com.google.common.base.Preconditions;
-import 
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider;
-
-import java.io.InputStream;
+import 
org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.AbstractClasspathURLProvider;
 
 /**
- * Abstract classpath URL provider.
+ * Abstract absolute path URL provider.
  */
-public abstract class AbstractClasspathURLProvider implements 
ShardingSphereURLProvider {
+public abstract class AbstractAbsolutePathURLProvider extends 
AbstractClasspathURLProvider {
     
-    String getConfigurationFile(final String url, final String urlPrefix, 
final String typePrefix) {
+    String getConfigurationFile(final String url, final String urlPrefix, 
final String pathType) {
         String configuredFile = url.substring(urlPrefix.length(), 
url.contains("?") ? url.indexOf('?') : url.length());
-        String file = configuredFile.substring(typePrefix.length());
+        String file = configuredFile.substring(pathType.length());
         Preconditions.checkArgument(!file.isEmpty(), "Configuration file is 
required in ShardingSphere URL.");
         return file;
     }
-    
-    InputStream getResourceAsStream(final String resource) {
-        InputStream result = 
Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
-        result = null == result ? 
Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + 
resource) : result;
-        if (null != result) {
-            return result;
-        }
-        throw new IllegalArgumentException(String.format("Can not find 
configuration file `%s`.", resource));
-    }
 }
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
index ce9cc175260..5e5f77ae367 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
@@ -27,9 +27,9 @@ import java.io.InputStream;
  */
 public abstract class AbstractClasspathURLProvider implements 
ShardingSphereURLProvider {
     
-    String getConfigurationFile(final String url, final String urlPrefix, 
final String typePrefix) {
+    String getConfigurationFile(final String url, final String urlPrefix, 
final String pathType) {
         String configuredFile = url.substring(urlPrefix.length(), 
url.contains("?") ? url.indexOf('?') : url.length());
-        String file = configuredFile.substring(typePrefix.length());
+        String file = configuredFile.substring(pathType.length());
         Preconditions.checkArgument(!file.isEmpty(), "Configuration file is 
required in ShardingSphere URL.");
         return file;
     }
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathURLProvider.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathURLProvider.java
index 4f9e9fd9e8a..9b488f6f5e5 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathURLProvider.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathURLProvider.java
@@ -31,17 +31,17 @@ import java.nio.charset.StandardCharsets;
  */
 public final class ClasspathURLProvider extends AbstractClasspathURLProvider {
     
-    private static final String TYPE_PREFIX = "classpath:";
+    private static final String PATH_TYPE = "classpath:";
     
     @Override
     public boolean accept(final String url) {
-        return !Strings.isNullOrEmpty(url) && url.contains(TYPE_PREFIX);
+        return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
     }
     
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final String url, final String urlPrefix) {
-        String file = getConfigurationFile(url, urlPrefix, TYPE_PREFIX);
+        String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
         try (
                 InputStream stream = getResourceAsStream(file);
                 BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream, StandardCharsets.UTF_8))) {
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 a4ab8c91a7a..43c51a8b32a 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
@@ -33,22 +33,21 @@ import java.util.regex.Pattern;
  */
 public final class ClasspathWithEnvironmentURLProvider extends 
AbstractClasspathURLProvider {
     
-    private static final String TYPE_PREFIX = "classpath-environment:";
+    private static final String PATH_TYPE = "classpath-environment:";
     
     private static final String KEY_VALUE_SEPARATOR = "::";
     
-    @SuppressWarnings("RegExpRedundantEscape")
-    private static final Pattern PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)\\}$");
+    private static final Pattern PATTERN = 
Pattern.compile("\\$\\$\\{(.+::.*)}$");
     
     @Override
     public boolean accept(final String url) {
-        return !Strings.isNullOrEmpty(url) && url.contains(TYPE_PREFIX);
+        return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
     }
     
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final String url, final String urlPrefix) {
-        String file = getConfigurationFile(url, urlPrefix, TYPE_PREFIX);
+        String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
         try (
                 InputStream stream = getResourceAsStream(file);
                 BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream, StandardCharsets.UTF_8))) {
diff --git 
a/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider
 
b/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider
index e1f3b18d8b5..87ba2a946ab 100644
--- 
a/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider
+++ 
b/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider
@@ -15,6 +15,7 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.driver.jdbc.core.driver.spi.AbsolutePathURLProvider
+org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath.AbsolutePathURLProvider
 
org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.ClasspathURLProvider
 
org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.ClasspathWithEnvironmentURLProvider
+org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath.AbsolutePathWithEnvironmentURLProvider
diff --git 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
new file mode 100644
index 00000000000..5d078e3b46a
--- /dev/null
+++ 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/absolutepath/AbsolutePathWithEnvironmentURLProviderTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.spi.absolutepath;
+
+import 
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager;
+import org.junit.jupiter.api.Test;
+
+import java.util.Objects;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+public class AbsolutePathWithEnvironmentURLProviderTest {
+    
+    @Test
+    void assertReplaceEnvironmentVariables() {
+        final String urlPrefix = "jdbc:shardingsphere:";
+        AbsolutePathWithEnvironmentURLProvider spy = spy(new 
AbsolutePathWithEnvironmentURLProvider());
+        
when(spy.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+        when(spy.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa");
+        String absoluteOriginPath = 
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
+        byte[] actualOrigin = 
ShardingSphereURLManager.getContent("jdbc:shardingsphere:absolutepath:" + 
absoluteOriginPath, urlPrefix);
+        String absolutePath = 
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-environment-variables-fixture.yaml")).getPath();
+        byte[] actual = 
spy.getContent("jdbc:shardingsphere:absolutepath-environment:" + absolutePath, 
urlPrefix);
+        assertThat(actual, is(actualOrigin));
+    }
+}

Reply via email to