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

wuweijie 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 8342977e062 Refactor ConfigurationContentReader (#30139)
8342977e062 is described below

commit 8342977e062a9cec3be0d8bb019807a5ed055d02
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Feb 16 13:21:48 2024 +0800

    Refactor ConfigurationContentReader (#30139)
    
    * Refactor ShardingSphereURL
    
    * Refactor ConfigurationContentReader
---
 .../jdbc/core/driver/url/ShardingSphereURL.java    |  2 +-
 .../url/reader/ConfigurationContentReader.java     | 23 ++++++++++++----------
 .../driver/url/type/AbsolutePathURLLoader.java     | 10 +++++-----
 .../core/driver/url/type/ClassPathURLLoader.java   | 17 +++++++---------
 .../url/reader/ConfigurationContentReaderTest.java | 15 +++++++-------
 5 files changed, 33 insertions(+), 34 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURL.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURL.java
index d0a08f079cc..016cd77ffd0 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURL.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURL.java
@@ -60,7 +60,7 @@ public final class ShardingSphereURL {
     
     private static String parseConfigurationSubject(final String url) {
         String result = url.substring(0, url.contains("?") ? url.indexOf('?') 
: url.length());
-        Preconditions.checkArgument(!result.isEmpty(), "Configuration subject 
is required in driver URL.");
+        Preconditions.checkArgument(!result.isEmpty(), "Configuration subject 
is required in URL.");
         return result;
     }
     
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 dd4849830a1..af6333d1006 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.arg.URLArgumentPlaceholderType;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentLine;
+import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentPlaceholderType;
 
 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.Optional;
 
 /**
@@ -37,20 +38,22 @@ public final class ConfigurationContentReader {
     
     /**
      * Read content.
-     * 
-     * @param inputStream input stream
-     * @param type configuration content placeholder type
+     *
+     * @param file file to be read
+     * @param placeholderType configuration content placeholder type
      * @return content
      * @throws IOException IO exception
      */
-    public static byte[] read(final InputStream inputStream, final 
URLArgumentPlaceholderType type) throws IOException {
-        try (BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
+    public static byte[] read(final File file, final 
URLArgumentPlaceholderType placeholderType) throws IOException {
+        try (
+                InputStreamReader inputStreamReader = new 
InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
+                BufferedReader bufferedReader = new 
BufferedReader(inputStreamReader)) {
             StringBuilder builder = new StringBuilder();
             String line;
-            while (null != (line = reader.readLine())) {
+            while (null != (line = bufferedReader.readLine())) {
                 if (!line.startsWith("#")) {
-                    Optional<URLArgumentLine> argLine = 
URLArgumentPlaceholderType.NONE == type ? Optional.empty() : 
URLArgumentLine.parse(line);
-                    builder.append(argLine.map(optional -> 
optional.replaceArgument(type)).orElse(line)).append(System.lineSeparator());
+                    Optional<URLArgumentLine> argLine = 
URLArgumentPlaceholderType.NONE == placeholderType ? Optional.empty() : 
URLArgumentLine.parse(line);
+                    builder.append(argLine.map(optional -> 
optional.replaceArgument(placeholderType)).orElse(line)).append(System.lineSeparator());
                 }
             }
             return builder.toString().getBytes(StandardCharsets.UTF_8);
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLLoader.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLLoader.java
index cee6e1a965e..ddfa256cd4c 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLLoader.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/AbsolutePathURLLoader.java
@@ -25,8 +25,6 @@ import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.reader.Configuratio
 
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
 
 /**
  * Absolute path URL loader.
@@ -36,9 +34,11 @@ public final class AbsolutePathURLLoader implements 
ShardingSphereURLLoader {
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final ShardingSphereURL url) {
-        try (InputStream inputStream = Files.newInputStream(new 
File(url.getConfigurationSubject()).toPath())) {
-            return ConfigurationContentReader.read(inputStream, 
URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters()));
-        }
+        return 
ConfigurationContentReader.read(getAbsoluteFile(url.getConfigurationSubject()), 
URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters()));
+    }
+    
+    private File getAbsoluteFile(final String configurationSubject) {
+        return new File(configurationSubject);
     }
     
     @Override
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClassPathURLLoader.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClassPathURLLoader.java
index 26674b9ea32..cf75871a7eb 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClassPathURLLoader.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/type/ClassPathURLLoader.java
@@ -17,15 +17,16 @@
 
 package org.apache.shardingsphere.driver.jdbc.core.driver.url.type;
 
-import com.google.common.base.Preconditions;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURL;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLLoader;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentPlaceholderTypeFactory;
 import 
org.apache.shardingsphere.driver.jdbc.core.driver.url.reader.ConfigurationContentReader;
 
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.util.Objects;
 
 /**
  * Class path URL loader.
@@ -35,16 +36,12 @@ public final class ClassPathURLLoader implements 
ShardingSphereURLLoader {
     @Override
     @SneakyThrows(IOException.class)
     public byte[] getContent(final ShardingSphereURL url) {
-        try (InputStream inputStream = 
getResourceAsStreamFromClasspath(url.getConfigurationSubject())) {
-            return ConfigurationContentReader.read(inputStream, 
URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters()));
-        }
+        return 
ConfigurationContentReader.read(getResourceFile(url.getConfigurationSubject()), 
URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters()));
     }
     
-    private InputStream getResourceAsStreamFromClasspath(final String 
resource) {
-        InputStream result = 
Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
-        result = null == result ? 
Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + 
resource) : result;
-        Preconditions.checkNotNull(result, "Can not find configuration file 
`%s`.", resource);
-        return result;
+    @SneakyThrows(URISyntaxException.class)
+    private File getResourceFile(final String configurationSubject) {
+        return new 
File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI().getPath());
     }
     
     @Override
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 cf557569538..a7b51436736 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
@@ -22,8 +22,9 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
-import java.io.FileInputStream;
+import java.io.File;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.util.Objects;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -48,23 +49,21 @@ class ConfigurationContentReaderTest {
     }
     
     @Test
-    void assertReadWithNonePlaceholder() throws IOException {
+    void assertReadWithNonePlaceholder() throws IOException, 
URISyntaxException {
         byte[] actual = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
URLArgumentPlaceholderType.NONE);
         byte[] expected = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
URLArgumentPlaceholderType.NONE);
         assertThat(new String(actual), is(new String(expected)));
     }
     
     @Test
-    void assertReadWithSystemPropertiesPlaceholder() throws IOException {
+    void assertReadWithSystemPropertiesPlaceholder() throws IOException, 
URISyntaxException {
         byte[] actual = 
readContent("config/driver/foo-driver-to-be-replaced-fixture.yaml", 
URLArgumentPlaceholderType.SYSTEM_PROPS);
         byte[] expected = readContent("config/driver/foo-driver-fixture.yaml", 
URLArgumentPlaceholderType.SYSTEM_PROPS);
         assertThat(new String(actual), is(new String(expected)));
     }
     
-    private byte[] readContent(final String name, final 
URLArgumentPlaceholderType placeholderType) throws IOException {
-        String path = 
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(name)).getPath();
-        try (FileInputStream inputStream = new FileInputStream(path)) {
-            return ConfigurationContentReader.read(inputStream, 
placeholderType);
-        }
+    private byte[] readContent(final String name, final 
URLArgumentPlaceholderType placeholderType) throws IOException, 
URISyntaxException {
+        File file = new 
File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(name)).toURI().getPath());
+        return ConfigurationContentReader.read(file, placeholderType);
     }
 }

Reply via email to