Skip to content

Commit

Permalink
Refactor ConfigurationContentReader (#30139)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereURL

* Refactor ConfigurationContentReader
  • Loading branch information
terrymanu authored Feb 16, 2024
1 parent 43438e5 commit 8342977
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
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());
}

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

0 comments on commit 8342977

Please sign in to comment.