From 6a4958cfff117471276e01b04a77db09af49bb98 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 16 Feb 2024 11:48:26 +0800 Subject: [PATCH] Refactor ConfigurationContentReader --- .../reader/ConfigurationContentReader.java | 23 +++++++++++-------- .../url/type/AbsolutePathURLLoader.java | 10 ++++---- .../driver/url/type/ClassPathURLLoader.java | 17 ++++++-------- .../ConfigurationContentReaderTest.java | 15 ++++++------ 4 files changed, 32 insertions(+), 33 deletions(-) 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 dd4849830a16d..af6333d10066f 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 @@ 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 argLine = URLArgumentPlaceholderType.NONE == type ? Optional.empty() : URLArgumentLine.parse(line); - builder.append(argLine.map(optional -> optional.replaceArgument(type)).orElse(line)).append(System.lineSeparator()); + Optional 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 cee6e1a965e38..ddfa256cd4c6a 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 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 26674b9ea3257..cf75871a7eb6e 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 cf5575695382b..a7b51436736c8 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.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 @@ static void afterAll() { } @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); } }