Skip to content

Commit

Permalink
Fix nightly build CI on Windows 11
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Feb 7, 2024
1 parent c2f7dcc commit 22cc582
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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;
}

/**
* 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,15 @@ 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 groupString = matcher.group(1);
String[] envNameAndDefaultValue = groupString.split(ArgsUtils.getKeyValueSeparator(), 2);
String envValue = getEnvironmentVariables(envNameAndDefaultValue[0]);
String defaultValue = envNameAndDefaultValue[1];
return ArgsUtils.replaceArg(envValue, defaultValue, 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,15 @@ 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 groupString = matcher.group(1);
String[] envNameAndDefaultValue = groupString.split(ArgsUtils.getKeyValueSeparator(), 2);
String envValue = getEnvironmentVariables(envNameAndDefaultValue[0]);
String defaultValue = envNameAndDefaultValue[1];
return ArgsUtils.replaceArg(envValue, defaultValue, matcher);
}

/**
Expand Down
Loading

0 comments on commit 22cc582

Please sign in to comment.