From 238e570668c92735448991d793a4901cdecba62e Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 16 Feb 2024 15:31:32 +0800 Subject: [PATCH] Refactor ClassPathURLLoader and AbsolutePathURLLoader (#30141) --- .../core/driver/url/type/AbsolutePathURLLoader.java | 10 +++++++--- .../jdbc/core/driver/url/type/ClassPathURLLoader.java | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) 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 b15554469e1fa..f5fedd229288c 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 @@ -27,6 +27,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.stream.Collectors; @@ -38,15 +39,18 @@ public final class AbsolutePathURLLoader implements ShardingSphereURLLoader { @Override @SneakyThrows(IOException.class) public byte[] getContent(final ShardingSphereURL url) { - Collection lines = Files.readAllLines( - getAbsoluteFile(url.getConfigurationSubject()).toPath(), StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()); - return URLArgumentLineRender.render(lines, URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); + Path path = getAbsoluteFile(url.getConfigurationSubject()).toPath(); + return URLArgumentLineRender.render(readLines(path), URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); } private File getAbsoluteFile(final String configurationSubject) { return new File(configurationSubject); } + private Collection readLines(final Path path) throws IOException { + return Files.readAllLines(path, StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()); + } + @Override public String getType() { return "absolutepath:"; 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 2d691d9b944ee..72b37c71f734b 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 @@ -28,6 +28,7 @@ import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.Objects; import java.util.stream.Collectors; @@ -40,9 +41,8 @@ public final class ClassPathURLLoader implements ShardingSphereURLLoader { @Override @SneakyThrows(IOException.class) public byte[] getContent(final ShardingSphereURL url) { - Collection lines = Files.readAllLines( - getResourceFile(url.getConfigurationSubject()).toPath(), StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()); - return URLArgumentLineRender.render(lines, URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); + Path path = getResourceFile(url.getConfigurationSubject()).toPath(); + return URLArgumentLineRender.render(readLines(path), URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); } @SneakyThrows(URISyntaxException.class) @@ -50,6 +50,10 @@ private File getResourceFile(final String configurationSubject) { return new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI().getPath()); } + private Collection readLines(final Path path) throws IOException { + return Files.readAllLines(path, StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()); + } + @Override public String getType() { return "classpath:";