Skip to content

Commit

Permalink
Rename ConfigurationContentReader to URLArgumentLineRender (#30140)
Browse files Browse the repository at this point in the history
* Refactor ConfigurationContentReader

* Refactor ConfigurationContentReader

* Refactor ConfigurationContentReader

* Rename ConfigurationContentReader to URLArgumentLineRender
  • Loading branch information
terrymanu authored Feb 16, 2024
1 parent 8342977 commit aa74a9f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> lines, final URLArgumentPlaceholderType placeholderType) {
StringBuilder result = new StringBuilder();
for (String each : lines) {
Optional<URLArgumentLine> 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);
}
}

This file was deleted.

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

private File getAbsoluteFile(final String configurationSubject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<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()));
}

@SneakyThrows(URISyntaxException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
* 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;

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

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

0 comments on commit aa74a9f

Please sign in to comment.