From aa74a9fa3f8edb6e33b909bfb5e1deeb125a708e Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 16 Feb 2024 15:14:55 +0800 Subject: [PATCH] Rename ConfigurationContentReader to URLArgumentLineRender (#30140) * Refactor ConfigurationContentReader * Refactor ConfigurationContentReader * Refactor ConfigurationContentReader * Rename ConfigurationContentReader to URLArgumentLineRender --- .../driver/url/arg/URLArgumentLineRender.java | 48 ++++++++++++++ .../reader/ConfigurationContentReader.java | 62 ------------------- .../url/type/AbsolutePathURLLoader.java | 10 ++- .../driver/url/type/ClassPathURLLoader.java | 10 ++- .../URLArgumentLineRenderTest.java} | 10 +-- 5 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/URLArgumentLineRender.java delete mode 100644 jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java rename jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/{reader/ConfigurationContentReaderTest.java => arg/URLArgumentLineRenderTest.java} (87%) diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/URLArgumentLineRender.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/URLArgumentLineRender.java new file mode 100644 index 0000000000000..a7e1e370170e7 --- /dev/null +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/URLArgumentLineRender.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.driver.jdbc.core.driver.url.arg; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.Optional; + +/** + * URL argument line render. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class URLArgumentLineRender { + + /** + * Render argument. + * + * @param lines lines to be rendered + * @param placeholderType configuration content placeholder type + * @return rendered content + */ + public static byte[] render(final Collection lines, final URLArgumentPlaceholderType placeholderType) { + StringBuilder result = new StringBuilder(); + for (String each : lines) { + Optional argLine = URLArgumentPlaceholderType.NONE == placeholderType ? Optional.empty() : URLArgumentLine.parse(each); + result.append(argLine.map(optional -> optional.replaceArgument(placeholderType)).orElse(each)).append(System.lineSeparator()); + } + return result.toString().getBytes(StandardCharsets.UTF_8); + } +} 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 deleted file mode 100644 index af6333d10066f..0000000000000 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReader.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.driver.jdbc.core.driver.url.reader; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -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.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.util.Optional; - -/** - * Configuration content reader. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class ConfigurationContentReader { - - /** - * Read content. - * - * @param file file to be read - * @param placeholderType configuration content placeholder type - * @return content - * @throws IOException IO exception - */ - 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 = bufferedReader.readLine())) { - if (!line.startsWith("#")) { - 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 ddfa256cd4c6a..b15554469e1fa 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 @@ -21,10 +21,14 @@ 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 org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentLineRender; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collection; +import java.util.stream.Collectors; /** * Absolute path URL loader. @@ -34,7 +38,9 @@ public final class AbsolutePathURLLoader implements ShardingSphereURLLoader { @Override @SneakyThrows(IOException.class) public byte[] getContent(final ShardingSphereURL url) { - return ConfigurationContentReader.read(getAbsoluteFile(url.getConfigurationSubject()), URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); + 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())); } private File getAbsoluteFile(final String configurationSubject) { 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 cf75871a7eb6e..2d691d9b944ee 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 @@ -21,12 +21,16 @@ 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 org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentLineRender; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collection; import java.util.Objects; +import java.util.stream.Collectors; /** * Class path URL loader. @@ -36,7 +40,9 @@ public final class ClassPathURLLoader implements ShardingSphereURLLoader { @Override @SneakyThrows(IOException.class) public byte[] getContent(final ShardingSphereURL url) { - return ConfigurationContentReader.read(getResourceFile(url.getConfigurationSubject()), URLArgumentPlaceholderTypeFactory.valueOf(url.getParameters())); + 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())); } @SneakyThrows(URISyntaxException.class) 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/arg/URLArgumentLineRenderTest.java similarity index 87% rename from jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/reader/ConfigurationContentReaderTest.java rename to jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/arg/URLArgumentLineRenderTest.java index a7b51436736c8..2f9d971846f1f 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/arg/URLArgumentLineRenderTest.java @@ -15,9 +15,8 @@ * limitations under the License. */ -package org.apache.shardingsphere.driver.jdbc.core.driver.url.reader; +package org.apache.shardingsphere.driver.jdbc.core.driver.url.arg; -import org.apache.shardingsphere.driver.jdbc.core.driver.url.arg.URLArgumentPlaceholderType; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -25,12 +24,15 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Objects; +import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -class ConfigurationContentReaderTest { +class URLArgumentLineRenderTest { private static final String FIXTURE_JDBC_URL_KEY = "fixture.config.driver.jdbc-url"; @@ -64,6 +66,6 @@ void assertReadWithSystemPropertiesPlaceholder() throws IOException, URISyntaxEx 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); + return URLArgumentLineRender.render(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8).stream().filter(each -> !each.startsWith("#")).collect(Collectors.toList()), placeholderType); } }