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

Fix nightly build CI on Windows 11 #30033

Merged
merged 1 commit into from
Feb 7, 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
@@ -0,0 +1,109 @@
/*
* 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;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Arguments Utils.
*/
public class ArgsUtils {

private static final String KEY_VALUE_SEPARATOR = "::";

private static final Pattern PATTERN = Pattern.compile("\\$\\$\\{(.+::.*)}$");

public static String getKeyValueSeparator() {
return KEY_VALUE_SEPARATOR;
}

/**
* Get Pattern.
*
* @return {@link java.util.regex.Pattern}
*/
public static Pattern getPattern() {
return PATTERN;
}

/**
* Get arg name and default value.
*
* @param matcher {@link Matcher}
* @return Argument name and default value.
*/
public static String[] getArgNameAndDefaultValue(final Matcher matcher) {
String groupString = matcher.group(1);
return groupString.split(ArgsUtils.getKeyValueSeparator(), 2);
}

/**
* Replace argument.
*
* @param targetValue the value of the argument
* @param defaultValue the default value of the argument
* @param matcher {@link Matcher}
* @return {@link String}
*/
public static String replaceArg(final String targetValue, final String defaultValue, final Matcher matcher) {
if (Strings.isNullOrEmpty(targetValue) && defaultValue.isEmpty()) {
String modifiedLineWithSpace = matcher.replaceAll("");
return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1);
}
if (Strings.isNullOrEmpty(targetValue)) {
return matcher.replaceAll(defaultValue);
}
return matcher.replaceAll(targetValue);
}

/**
* Get configuration file.
*
* @param url url
* @param urlPrefix url prefix
* @param pathType path type
* @return {@link String}
*/
public static String getConfigurationFile(final String url, final String urlPrefix, final String pathType) {
String configuredFile = url.substring(urlPrefix.length(), url.contains("?") ? url.indexOf('?') : url.length());
String file = configuredFile.substring(pathType.length());
Preconditions.checkArgument(!file.isEmpty(), "Configuration file is required in ShardingSphere URL.");
return file;
}

/**
* Get resource as stream from classpath.
*
* @param resource resource
* @return {@link InputStream}
* @throws IllegalArgumentException Can not find configuration file.
*/
public static InputStream getResourceAsStreamFromClasspath(final String resource) {
InputStream result = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
result = null == result ? Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + resource) : result;
if (null != result) {
return result;
}
throw new IllegalArgumentException(String.format("Can not find configuration file `%s`.", resource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.driver.ArgsUtils;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -31,7 +32,7 @@
/**
* Absolute path URL provider.
*/
public final class AbsolutePathURLProvider extends AbstractAbsolutePathURLProvider {
public final class AbsolutePathURLProvider implements AbstractAbsolutePathURLProvider {

private static final String PATH_TYPE = "absolutepath:";

Expand All @@ -43,7 +44,7 @@ public boolean accept(final String url) {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final String url, final String urlPrefix) {
String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
String file = ArgsUtils.getConfigurationFile(url, urlPrefix, PATH_TYPE);
try (
InputStream stream = Files.newInputStream(new File(file).toPath());
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.driver.ArgsUtils;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -28,19 +29,14 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Absolute path with environment variables URL provider.
*/
public class AbsolutePathWithEnvironmentURLProvider extends AbstractAbsolutePathURLProvider {
public class AbsolutePathWithEnvironmentURLProvider implements AbstractAbsolutePathURLProvider {

private static final String PATH_TYPE = "absolutepath-environment:";

private static final String KEY_VALUE_SEPARATOR = "::";

private static final Pattern PATTERN = Pattern.compile("\\$\\$\\{(.+::.*)}$");

@Override
public boolean accept(final String url) {
return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
Expand All @@ -49,7 +45,7 @@ public boolean accept(final String url) {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final String url, final String urlPrefix) {
String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
String file = ArgsUtils.getConfigurationFile(url, urlPrefix, PATH_TYPE);
try (
InputStream stream = Files.newInputStream(new File(file).toPath());
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
Expand All @@ -66,21 +62,13 @@ public byte[] getContent(final String url, final String urlPrefix) {
}

private String replaceEnvironmentVariables(final String line) {
Matcher matcher = PATTERN.matcher(line);
Matcher matcher = ArgsUtils.getPattern().matcher(line);
if (!matcher.find()) {
return line;
}
String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2);
String envName = envNameAndDefaultValue[0];
String envValue = getEnvironmentVariables(envName);
if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) {
String modifiedLineWithSpace = matcher.replaceAll("");
return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1);
}
if (Strings.isNullOrEmpty(envValue)) {
envValue = envNameAndDefaultValue[1];
}
return matcher.replaceAll(envValue);
String[] envNameAndDefaultValue = ArgsUtils.getArgNameAndDefaultValue(matcher);
String envValue = getEnvironmentVariables(envNameAndDefaultValue[0]);
return ArgsUtils.replaceArg(envValue, envNameAndDefaultValue[1], matcher);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@

package org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath;

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider;

/**
* Abstract absolute path URL provider.
*/
public abstract class AbstractAbsolutePathURLProvider implements ShardingSphereURLProvider {

String getConfigurationFile(final String url, final String urlPrefix, final String pathType) {
String configuredFile = url.substring(urlPrefix.length(), url.contains("?") ? url.indexOf('?') : url.length());
String file = configuredFile.substring(pathType.length());
Preconditions.checkArgument(!file.isEmpty(), "Configuration file is required in ShardingSphere URL.");
return file;
}
public interface AbstractAbsolutePathURLProvider extends ShardingSphereURLProvider {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,10 @@

package org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath;

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider;

import java.io.InputStream;

/**
* Abstract classpath URL provider.
*/
public abstract class AbstractClasspathURLProvider implements ShardingSphereURLProvider {

String getConfigurationFile(final String url, final String urlPrefix, final String pathType) {
String configuredFile = url.substring(urlPrefix.length(), url.contains("?") ? url.indexOf('?') : url.length());
String file = configuredFile.substring(pathType.length());
Preconditions.checkArgument(!file.isEmpty(), "Configuration file is required in ShardingSphere URL.");
return file;
}

InputStream getResourceAsStream(final String resource) {
InputStream result = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
result = null == result ? Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + resource) : result;
if (null != result) {
return result;
}
throw new IllegalArgumentException(String.format("Can not find configuration file `%s`.", resource));
}
public interface AbstractClasspathURLProvider extends ShardingSphereURLProvider {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.driver.ArgsUtils;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -29,7 +30,7 @@
/**
* Classpath URL provider.
*/
public final class ClasspathURLProvider extends AbstractClasspathURLProvider {
public final class ClasspathURLProvider implements AbstractClasspathURLProvider {

private static final String PATH_TYPE = "classpath:";

Expand All @@ -41,9 +42,9 @@ public boolean accept(final String url) {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final String url, final String urlPrefix) {
String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
String file = ArgsUtils.getConfigurationFile(url, urlPrefix, PATH_TYPE);
try (
InputStream stream = getResourceAsStream(file);
InputStream stream = ArgsUtils.getResourceAsStreamFromClasspath(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
StringBuilder builder = new StringBuilder();
String line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@

import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.driver.ArgsUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Classpath with environment variables URL provider.
*/
public final class ClasspathWithEnvironmentURLProvider extends AbstractClasspathURLProvider {
public final class ClasspathWithEnvironmentURLProvider implements AbstractClasspathURLProvider {

private static final String PATH_TYPE = "classpath-environment:";

private static final String KEY_VALUE_SEPARATOR = "::";

private static final Pattern PATTERN = Pattern.compile("\\$\\$\\{(.+::.*)}$");

@Override
public boolean accept(final String url) {
return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
Expand All @@ -47,9 +43,9 @@ public boolean accept(final String url) {
@Override
@SneakyThrows(IOException.class)
public byte[] getContent(final String url, final String urlPrefix) {
String file = getConfigurationFile(url, urlPrefix, PATH_TYPE);
String file = ArgsUtils.getConfigurationFile(url, urlPrefix, PATH_TYPE);
try (
InputStream stream = getResourceAsStream(file);
InputStream stream = ArgsUtils.getResourceAsStreamFromClasspath(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
StringBuilder builder = new StringBuilder();
String line;
Expand All @@ -64,21 +60,13 @@ public byte[] getContent(final String url, final String urlPrefix) {
}

private String replaceEnvironmentVariables(final String line) {
Matcher matcher = PATTERN.matcher(line);
Matcher matcher = ArgsUtils.getPattern().matcher(line);
if (!matcher.find()) {
return line;
}
String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2);
String envName = envNameAndDefaultValue[0];
String envValue = getEnvironmentVariables(envName);
if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) {
String modifiedLineWithSpace = matcher.replaceAll("");
return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1);
}
if (Strings.isNullOrEmpty(envValue)) {
envValue = envNameAndDefaultValue[1];
}
return matcher.replaceAll(envValue);
String[] envNameAndDefaultValue = ArgsUtils.getArgNameAndDefaultValue(matcher);
String envValue = getEnvironmentVariables(envNameAndDefaultValue[0]);
return ArgsUtils.replaceArg(envValue, envNameAndDefaultValue[1], matcher);
}

/**
Expand Down
Loading
Loading