Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ConfigurationContentReader #30139

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static String parseSourceType(final String url) {

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
Comment on lines +42 to +44
Copy link
Member

@linghengqian linghengqian Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caused by: java.nio.file.NoSuchFileException: /test-native/yaml/modes/cluster/zookeeper.yaml
        at [email protected]/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:261)
        at [email protected]/java.nio.file.Files.newByteChannel(Files.java:379)
        at [email protected]/java.nio.file.Files.newByteChannel(Files.java:431)
        at [email protected]/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
        at [email protected]/java.nio.file.Files.newInputStream(Files.java:159)
        at [email protected]/java.nio.file.Files.newBufferedReader(Files.java:2897)
        at [email protected]/java.nio.file.Files.readAllLines(Files.java:3392)
        at [email protected]/java.nio.file.Files.readAllLines(Files.java:3433)
        at org.apache.shardingsphere.driver.jdbc.core.driver.url.type.ClassPathURLLoader.load(ClassPathURLLoader.java:39)
        at org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLLoadEngine.loadContent(ShardingSphereURLLoadEngine.java:47)
        at org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache.createDataSource(DriverDataSourceCache.java:55)
        ... 77 common frames omitted

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Loading