-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClasspathURLProvider implementation that reads environment variab…
…les (#29907) * Add ClasspathURLProvider implementation that reads environment variables * small refactoring
- Loading branch information
1 parent
2cccf62
commit 2133c77
Showing
8 changed files
with
325 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...he/shardingsphere/driver/jdbc/core/driver/spi/classpath/AbstractClasspathURLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.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 typePrefix) { | ||
String configuredFile = url.substring(urlPrefix.length(), url.contains("?") ? url.indexOf('?') : url.length()); | ||
String file = configuredFile.substring(typePrefix.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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...dingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.spi.classpath; | ||
|
||
import com.google.common.base.Strings; | ||
import lombok.SneakyThrows; | ||
|
||
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 { | ||
|
||
private static final String TYPE_PREFIX = "classpath-environment:"; | ||
|
||
private static final String KEY_VALUE_SEPARATOR = "::"; | ||
|
||
@SuppressWarnings("RegExpRedundantEscape") | ||
private static final Pattern PATTERN = Pattern.compile("\\$\\$\\{(.+::.*)\\}$"); | ||
|
||
@Override | ||
public boolean accept(final String url) { | ||
return !Strings.isNullOrEmpty(url) && url.contains(TYPE_PREFIX); | ||
} | ||
|
||
@Override | ||
@SneakyThrows(IOException.class) | ||
public byte[] getContent(final String url, final String urlPrefix) { | ||
String file = getConfigurationFile(url, urlPrefix, TYPE_PREFIX); | ||
try ( | ||
InputStream stream = getResourceAsStream(file); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { | ||
StringBuilder builder = new StringBuilder(); | ||
String line; | ||
while (null != (line = reader.readLine())) { | ||
if (!line.startsWith("#")) { | ||
line = replaceEnvironmentVariables(line); | ||
builder.append(line).append('\n'); | ||
} | ||
} | ||
return builder.toString().getBytes(StandardCharsets.UTF_8); | ||
} | ||
} | ||
|
||
private String replaceEnvironmentVariables(final String line) { | ||
Matcher matcher = PATTERN.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); | ||
} | ||
|
||
/** | ||
* This method is only used for mocking environment variables in unit tests and should not be used under any circumstances. | ||
* | ||
* @param name the name of the environment variable | ||
* @return the string value of the variable, or null if the variable is not defined in the system environment | ||
*/ | ||
String getEnvironmentVariables(final String name) { | ||
return System.getenv(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...sphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithEnvironmentURLProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.spi.classpath; | ||
|
||
import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ClasspathWithEnvironmentURLProviderTest { | ||
|
||
@Test | ||
void assertReplaceEnvironmentVariables() { | ||
final String urlPrefix = "jdbc:shardingsphere:"; | ||
ClasspathWithEnvironmentURLProvider spy = spy(new ClasspathWithEnvironmentURLProvider()); | ||
when(spy.getEnvironmentVariables("FIXTURE_JDBC_URL")).thenReturn("jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); | ||
when(spy.getEnvironmentVariables("FIXTURE_USERNAME")).thenReturn("sa"); | ||
byte[] actual = spy.getContent("jdbc:shardingsphere:classpath-environment:config/driver/foo-driver-environment-variables-fixture.yaml", urlPrefix); | ||
byte[] actualOrigin = ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", urlPrefix); | ||
assertThat(actual.length, is(999)); | ||
assertThat(actual, is(actualOrigin)); | ||
} | ||
} |
Oops, something went wrong.