Skip to content

Commit

Permalink
Refactor ClassPathURLLoader and AbsolutePathURLLoader (#30141)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 16, 2024
1 parent aa74a9f commit 238e570
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,15 +39,18 @@ public final class AbsolutePathURLLoader implements ShardingSphereURLLoader {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final ShardingSphereURL url) {
Collection<String> 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<String> 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:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,16 +41,19 @@ public final class ClassPathURLLoader implements ShardingSphereURLLoader {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final ShardingSphereURL url) {
Collection<String> 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)
private File getResourceFile(final String configurationSubject) {
return new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI().getPath());
}

private Collection<String> 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:";
Expand Down

0 comments on commit 238e570

Please sign in to comment.